精选文章

Android下使用TCPDUMP抓包Wireshark分析数据 如果想分析Android下某个APP的网络数据交互,需要在Android手机上抓包,最常用的抓包工具非tcpdump莫属,用tcpdump生成Wireshark识别的pcap文件,然后将pcap文件下载到电脑上,用电脑上的Wireshark加载pcap文件,通过Wireshark分析tcpdump抓取的数据。...

继续阅读

Mac下部署Android开发环境附加NDK 作为开发者,我们深有体会,不管是进行什么开发,为了部署开发环境,我们往往需要折腾很长时间、查阅很多资料才能完成,而且这次折腾完了,下次到了另一台新电脑上又得重新来过,整个部署过程记得还好,要是不记得又得重新开始,而且遇到Android这种GFW阻隔了开发资源下载链接的环境部署,又尤其浪费时间。所以这也是我写下这篇教程的初衷跟动力源泉,希望大家参考了这篇教程以后可以轻轻松松在Mac系统下将Android环境部署好。...

继续阅读

稍顯嚴肅的台中 坦白說,留在腦海中的台中影像並不多,來台灣之前在Booking上只訂到了台中的一家青旅,第一次住青旅有些不習慣,幹什麼都放不開。 同屋的一個男生是台灣人,不過一年中四分之三的時間在上海跟北京,這麼說來跟我還是比較有共同話題的。得之我準備花15天的時間環島,覺得太倉促了,他們大學時期花一個半月的時間也不見得能將台灣島給逛完。我只能無奈地表示,兩岸允許的簽證時間有限,自己的空閒時間更有限,只能用打卡式的旅行了,我深知正真地旅行應該慢下來,融入當地的環境,感受他們的風土人情,但第一次只能這樣作罷,以後換成民進黨上台,形勢會變成怎樣還不得而知,能否再過來還是個未知數。而我一向信奉的人生格言是秉燭夜遊,活在當下,所以,理解自己吧。...

继续阅读

為之留戀的新竹 來新竹之前本沒有對她有過高的期待,慢慢對她加分要從桃園火車站出發前往新竹開始。 在桃園火車站的候車月台上,有醒目的旅遊資料發放處,這上面的擺放的全是新竹的旅遊宣傳資料,關鍵的是資料做得非常簡潔易懂,而接下來一天的新竹之行就全部是依據這份寶典的指引來完成的。...

继续阅读

從桃園開始台灣之行 初到台灣恰逢華夏銀行系統升級,特意準備的華夏銀聯卡在桃園機場沒能派上用場,只好用建行在機場5000塊,算下來是很不划算的,但是沒辦法,誰叫我出機場就得花錢呢。 從機場打車到桃園的酒店,花了將近六百塊新台幣,到酒店時五點多,天已經漸亮了,洗漱完等到七點吃過早餐就開始補覺囉,一覺醒來已是中午,帶著換下來的衣服外出找自助洗衣店,順便覓食。...

继续阅读

  • Prev
  • Next

Vector成员函数三

文章分类 : C++, 标准模板库

1、push_back——将某个元素从容器后端入栈

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std; 

template <class T>
class Name
{
    public:
        Name(T t) : name(t) {}
        void print()
        {
            cout << name << " ";
        }
    private:
        T name;
};

//=============================

int main ()
{
    typedef Name<string> N;
    typedef vector<N> V;
    V v;
    N n1("Robert");
    N n2("Alex");
    v.push_back(n1);
    v.push_back(n2);
    v.push_back(N("Linda"));
    V::iterator It = v.begin();
    while ( It != v.end() )
        (It++)->print();
    cout << endl; 

    return 0;
}
运行结果:
// Robert Alex Linda

2、rbegin、rend——容器逆序输出的头和尾

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std; 

class ID
{
    friend bool operator < ( const ID&, const ID& );
        public:
        ID(string name,int score) : name(name), score(score) {}
        void display ()
        {
            cout.setf(ios::left);
            cout << setw(3) << score << name << endl;
        }
    private:
        string name; int score;
};

//----------------------------------------------------- 
// 比较排序函数
bool operator < ( const ID& a, const ID& b )
{
    return a.score < b.score;
}

//----------------------------------------------------- 
typedef vector<ID> Vector; // 为已存在的类型重新定义一个名字

int main ()
{
    Vector v;
    Vector::iterator Iter; 

    v.push_back(ID("Smith A",96));
    v.push_back(ID("Amstrong B.",91));
    v.push_back(ID("Watson D.",82)); 

    for ( Iter = v.begin(); Iter != v.end(); Iter++ )
        Iter->display(); 
    sort(v.begin(),v.end()); // 排序算法
    cout << endl << "Sorted by Score" << endl;
    cout << "===============" << endl; 
    for ( Iter = v.begin(); Iter != v.end(); Iter++ )
        Iter->display();
    cout << endl << "Reverse output" << endl;
    cout << "===============" << endl;
    Vector::reverse_iterator r = v.rbegin();   
    while ( r != v.rend() )
        cout << r->display();
    cout << endl; 

    return 0;
}
运行结果:
// 96 Smith A.
// 91 Amstrong B.
// 82 Watson D.
//
// Sorted by Score
// ===============
// 82 Watson D.
// 91 Amstrong B.
// 96 Smith A.
//
// Reverse output
// ===============
// 96 Smith A.
// 91 Amstrong B.
// 82 Watson D.

3、reserve——扩充容器的容量

#include <iostream>
#include <vector>    
using namespace std; 

int main () 
{
    vector<int> v(5,0); // 5个元素并初始化为0
    /*------------------------------------------------*/
    cout << "Size of v  = " << v.size() << endl;
    cout << "Capacity v = " << v.capacity() << endl;
    cout << "Value of each element is - ";
    for ( int i = 0; i < v.size(); i++ )
        cout << v[i] << "  ";
    cout << endl; 

    v[0] = 5;       // 改变第一个元素的值
    v[1] = 8;
    v.push_back(3); // 为容器新建第六个元素  
    v.push_back(7); // 为容器新建第七个元素 
    cout << endl;   // 容器满了以后,capacity自动增加为原来的两倍

    cout << "Size of v  = " << v.size() << endl;
    cout << "Capacity v = " << v.capacity() << endl;
    cout << "Value of each element is - ";
    for ( int i = 0; i < v.size(); i++ )
        cout << v[i] << "  ";
    cout << endl << endl; 

    v.reserve(100); // 将容器容量capacity增加到100
    cout << "Size of v1_int  = " << v.size() << endl;
    cout << "Capacity v1_int = " << v.capacity() << endl;

    int size = sizeof(v); //vector容器大小
    cout << "sizeof v   = " << size << endl;

    return 0;
}
运行结果:
// Size of v  = 5
// Capacity v = 5
// Value of each element is - 0  0  0  0  0  
// 
// Size of v  = 7
// Capacity v = 10
// Value of each element is - 5  8  0  0  0  3  7  
// 
// Size of v  = 7
// Capacity v = 100
// sizeof v   = 12

4、resize——减小或者扩大容器的容量

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std; 

int main ()
{
    vector<int> v(5);
    for ( int i=0; i<5; i++ )
        v[i] = i*2;

    copy(v.begin(),v.end(),  
            ostream_iterator<int>(cout," "));
    cout << endl; 

    v.resize(7,100);  
    copy(v.begin(),v.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    v.resize(4);
    copy(v.begin(),v.end(),
            ostream_iterator<int>(cout," "));
    cout << endl;

    return 0;
}
运行结果:
// 0 2 4 6 8
// 0 2 4 6 8 100 100
// 0 2 4 6

5、size——返回容器内元素的个数

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

template <class T>
class Print
{
    public:
        void operator () (T& t)
        {
            cout << t << " ";
        }
};
//=============================
int main ()
{
    vector<char> v(5);
    Print<char> print;

    cout << "Size of v = " << v.size() << endl; 

    fill(v.begin(),v.end(),'*');

    for_each(v.begin(),v.end(),print);
    cout << endl;

    for ( int i=0; i < v.size(); i++ )
        cout << v[i] << " ";
    cout << endl; 

    for ( int i=0; i<5; i++ )
    {
        cout << "Size of v = ";
        for_each(v.begin(),v.end(),print);
        cout << endl; 

        v.pop_back();
    } 

    return 0;
}
运行结果:
// Size of v = 5
// * * * * *
// * * * * *
// Size of v = * * * * *
// Size of v = * * * *
// Size of v = * * *
// Size of v = * *
// Size of v = *

6、swap——将自身容器的内容与指定容器的内容交换

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; 

template <class T>
class Print
{
    public:
        void operator () (T& t)
        {
            cout << t << " ";
        }
};
//=============================
int main ()
{
    int ary[] = {1,2,3,4,5,6,7,8,9,10};
    Print print; 

    vector<int> v1(ary,ary+7);
    vector<int> v2(ary+7,ary+10); 

    cout << "Vector v1 : ";
    for_each(v1.begin(),v1.end(),print);
    cout << endl;
    cout << "Size of v1 = " << v1.size()
         << endl << endl; 

    cout << "Vector v2 : ";
    for_each(v2.begin(),v2.end(),print);
    cout << endl;
    cout << "Size of v2 = " << v2.size()
         << endl << endl; 

    v1.swap(v2);
    cout << "After swapping:" << endl; 

    cout << "Vector v1 : ";
    for_each(v1.begin(),v1.end(),print);
    cout << endl;
    cout << "Size of v1 = " << v1.size()
         << endl << endl;
    cout << "Vector v2 : ";
    for_each(v2.begin(),v2.end(),print);
    cout << endl;
    cout << "Size of v2 = " << v2.size()
         << endl << endl; 

    return 0;
}
运行结果:
// Vector v1 : 1 2 3 4 5 6 7
// Size of v1 = 7
//
// Vector v2 : 8 9 10
// Size of v2 = 3
//
// After swapping:
// Vector v1 : 8 9 10
// Size of v1 = 3
//
// Vector v2 : 1 2 3 4 5 6 7
// Size of v2 = 7

除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。

本文地址:https://www.cpplive.com/html/426.html

这里因为你的留言而存在!!!

You must be logged in to post a comment.