vector容器

#include"string.h"
#include"vector.h"

class people {
public:
    people(int age1, string name1) {
        age = age1;
        name = name1;

    }
    int  age;
    string  name;

};


void test1() {
    vector<int>s9;
    int* p = NULL;
    int num = 0;
    s9.reserve(1000000);
    for (int i = 0; i < 1000000; i++) {
        s9.push_back(i);
        if (p != &s9[0])
        {
            p = &s9[0];   //由于动态扩展机制,当容量不够的时候会进行动态扩展,这时候p就不等于&s9[0],这时候就相当于进行了一次动态扩展
            num++;        //这段代码可以查看动态扩展的次数
        }
    }
    cout << num << endl;
}
int  main()
{
    string s1;
    s1 = "gouwangqi";
    string s2(s1);
    cout << s1 << endl;
    cout << s2 << endl;
    vector<int>s3;
    s3 = {1,2,3,4,5,6};
    vector<int>s4(s3);
    for (auto iter = s3.begin(); iter != s3.cend(); iter++)
    {
        cout << *iter << endl;
    
    }
    for (auto iter = s4.begin(); iter != s4.cend(); iter++)
    {
        cout << *iter << endl;

    }
    s3.push_back(7);  //尾插法
    s3.push_back(8);  
    s3.push_back(9);
    s3.pop_back();  //尾删法
    s3.pop_back();
    s4.push_back(7);
    s4.push_back(8);
    s4.push_back(9);
    s4.pop_back();
    s4.pop_back();
    for (auto iter = s3.begin(); iter != s3.cend(); iter++)
    {
        cout << *iter << endl;

    }
    for (auto iter = s4.begin(); iter != s4.cend(); iter++)
    {
        cout << *iter << endl;

    }
    cout << s3.front() << endl;//front()返回第一个元素的值
    cout << s3.back() << endl;//back()返回最后一个元素的值
    cout << s3.size() << endl;//计算容器的大小
    auto iter1 = s3.begin();  //begin()返回第一个元素的迭代器
    auto iter2 = s3.cend();//begin()返回最后一个元素的迭代器
    s3.insert(iter1, 399);//向指定位置插入元素
    cout << *iter1 << endl;
    auto iter3 = s3.cend()-3;
    s3.erase(iter1,iter3);//删除两个迭代器之间的元素
    for (auto iter = s3.begin(); iter != s3.cend(); iter++)
    {
        cout << *iter << "我被干掉了" << endl;

    }
    cout << s3.capacity() << endl;
    s3.push_back(7); 
    s3.push_back(8);
    s3.push_back(9);
    s3.push_back(7); 
    s3.push_back(8);
    s3.push_back(9);
    s3.push_back(7);  
    s3.push_back(8);
    s3.push_back(9);
    cout << s3.capacity() << endl;
    s3.push_back(9);
    s3.push_back(9);
    s3.push_back(9);
    cout << s3.capacity() << endl;
    cout << s3.size() << endl;
    vector<int>s6{ 1,2,3,4,5,6 };
    for (auto iter = s6.begin(); iter != s6.cend(); iter++)
    {
        cout << *iter << endl;

    }
    cout << s3.size() << endl;
    s3.resize(500);
    for (auto iter = s3.begin(); iter != s3.cend(); iter++)
    {
        cout << *iter << endl;

    }
    cout << !s3.empty() << endl;

    cout << s3.at(9) << endl; //返回下标索引值所指向的数据

    s3.insert(s3.begin(), 200);
    s3.insert(s3.end(), 300);
    cout << s3.front() << endl;
    cout << s3.back() << endl;
    s3.erase(s3.begin(), s3.end());  //删除两个迭代器之间的元素
    if (!s3.empty()){
        
        for (auto iter = s6.begin(); iter != s6.cend(); iter++)
        {
            cout << *iter << endl;

        }
    }
    else {
    
        cout << "erase成功!!!" << endl;
    }
    s3.resize(3);
    cout << s3.capacity() << endl;
    cout << s3.size() << endl;
    vector<int>(s3).swap(s3);    //使用这个式子来节约内存
    cout << s3.capacity() << endl;
    cout << s3.size() << endl;

    //正向迭代器叫做iterator    vector的迭代器是随机访问迭代器  支持跳跃式的访问
    for (vector<int>::iterator iter = s6.begin(); iter != s6.end(); iter++)
    {
        cout << *iter << "正向迭代器" << endl;


    }
    //逆向迭代器叫做reverse::iterator    vector的迭代器是随机访问迭代器  支持跳跃式的访问
    for (vector<int>::reverse_iterator iter = s6.rbegin(); iter != s6.rend(); iter++)
    {
        cout << *iter<<"反向迭代器" << endl;
        
 
    }

    //使用reserve预留空间
    //使用reserve预留空间预先留空间的大小,防止多次进行动态扩展,浪费时间

    test1();

    vector<char>s;   //vector容器可以存储char类型的数据
    s.push_back('w');
    for (vector<char>::iterator iter = s.begin(); iter != s.end(); iter++)
    {
        cout << *iter << "正向迭代器" << endl;

    }


    vector<char>sw;   //vector容器可以存储char类型的数据
    sw.push_back('swf');
    for (vector<char>::iterator iter = sw.begin(); iter != sw.end(); iter++)
    {
        cout << *iter << "字符vector" << endl;

    }


    //vector容器可以存储自定义类型的数据
    people people1(20,"q");
    people people2(30, "w");
    people people3(40, "e");
    people people4(50, "r");
    people people5(60, "t");
    people people6(70, "x");
    vector<people>sw1;   
    sw1.push_back(people1);
    sw1.push_back(people2);
    sw1.push_back(people3);
    sw1.push_back(people4);
    sw1.push_back(people5);
    sw1.push_back(people6);
    for (auto iter = sw1.begin(); iter != sw1.end(); iter++)
    {
        cout <<(*iter).age << "vector存储自定义类型people" << endl;
        cout << (*iter).age << "vector存储自定义类型people" << endl;

    }

    system("pause");
    return 0;

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值