• C++ Vector实践


    实践如下:

    #include <iostream>
    #include <vector>
    #include <typeinfo>
    using namespace std;
    
    int main() {
        cout<<"Vector 测试"<<endl;
    
        vector<int> v1,v2;
        v1.reserve(10);
        v2.reserve(10);
    
        v1 = vector<int>(8,7);
        int array[8] = {1,2,3,4,5,6,7,8};
        v2 = vector<int>(array, array+8);
    
        cout<<"v1容量:"<<v1.capacity()<<endl;
        cout<<"v1当前各项:"<<endl;
        for(decltype(v2.size()) i = 0; i < v1.size(); i++){
            cout<<""<<i<<"项: "<<v1[i]<<endl;
        }
    
        cout<<"v2容量:"<<v2.capacity()<<endl;
        cout<<"v2当前各项:"<<endl;
        for(vector<int>::size_type i = 0; i < v2.size(); i++){
            cout<<""<<i<<"项: "<<v2[i]<<endl;
        }
    
        cout<<" typeid(size_t).name() = "<<typeid(size_t).name()<<endl;
        cout<<" typeid(vector<int>::size_type).name() = "<<typeid(vector<int>::size_type).name()<<endl;
        cout<<endl<<endl;
    
        v1.resize(0);
        cout<<"v1容量重新初始化为0"<<endl;
        if(v1.empty()){
            cout<<"v1是空的"<<endl;
            cout<<"v1中元素的个数是: "<<v1.size()<<endl;
            cout<<"v1容量是: "<<v1.capacity()<<endl;
        }
        else{
            cout<<"v1容量是: "<<v1.size()<<endl;
        }
    
        v1.resize(10);
        cout<<"v1容量重新初始化为10"<<endl;
        cout<<"v1中元素的个数是: "<<v1.size()<<endl;
        cout<<"v1容量是: "<<v1.capacity()<<endl;
        cout<<"v1当前各项:"<<endl;
        for(unsigned int i = 0; i < v1.size(); i++){
            cout<<""<<i<<"项: "<<v1[i]<<endl;
        }
        cout<<endl<<endl;
    
        v1.swap(v2);
        cout<<"v1与v2进行了swap操作"<<endl;
        cout<<"v1中元素的个数是: "<<v1.size()<<endl;
        cout<<"v1容量是: "<<v1.capacity()<<endl;
        cout<<"v1当前各项:"<<endl;
        for(unsigned int i = 0; i < v1.size(); i++){
            cout<<""<<i<<"项: "<<v1[i]<<endl;
        }
        cout<<endl<<endl;
    
        v1.push_back(22222);
        cout<<"v1.push_back(22222)操作"<<endl;
        cout<<"v1中元素的个数是: "<<v1.size()<<endl;
        cout<<"v1容量是: "<<v1.capacity()<<endl;
        cout<<"v1当前各项:"<<endl;
        for(unsigned int i = 0; i < v1.size(); i++){
            cout<<""<<i<<"项: "<<v1[i]<<endl;
        }
        cout<<endl<<endl;
    
        v1.erase(v1.end()-2);
        cout<<"v1.push_back(22222)操作"<<endl;
        cout<<"v1中元素的个数是: "<<v1.size()<<endl;
        cout<<"v1容量是: "<<v1.capacity()<<endl;
        cout<<"v1当前各项:"<<endl;
        for(unsigned int i = 0; i < v1.size(); i++){
            cout<<""<<i<<"项: "<<v1[i]<<endl;
        }
        cout<<endl<<endl;
    
        v1.pop_back();
        cout<<"v1.pop_back()操作"<<endl;
        cout<<"v1中元素的个数是: "<<v1.size()<<endl;
        cout<<"v1容量是: "<<v1.capacity()<<endl;
        cout<<"v1当前各项:"<<endl;
        for(unsigned int i = 0; i < v1.size(); i++){
            cout<<""<<i<<"项: "<<v1[i]<<endl;
        }
        cout<<endl<<endl;
    
        cout << "测试结束" << endl;
        return 0;
    }

    输出:

    Vector 测试
    v1容量:8
    v1当前各项:
      第0项: 7
      第1项: 7
      第2项: 7
      第3项: 7
      第4项: 7
      第5项: 7
      第6项: 7
      第7项: 7
    v2容量:8
    v2当前各项:
      第0项: 1
      第1项: 2
      第2项: 3
      第3项: 4
      第4项: 5
      第5项: 6
      第6项: 7
      第7项: 8
     typeid(size_t).name() = j
     typeid(vector<int>::size_type).name() = j
    
    
    v1容量重新初始化为0
    v1是空的
    v1中元素的个数是: 0
    v1容量是: 8
    v1容量重新初始化为10
    v1中元素的个数是: 10
    v1容量是: 10
    v1当前各项:
      第0项: 0
      第1项: 0
      第2项: 0
      第3项: 0
      第4项: 0
      第5项: 0
      第6项: 0
      第7项: 0
      第8项: 0
      第9项: 0
    
    
    v1与v2进行了swap操作
    v1中元素的个数是: 8
    v1容量是: 8
    v1当前各项:
      第0项: 1
      第1项: 2
      第2项: 3
      第3项: 4
      第4项: 5
      第5项: 6
      第6项: 7
      第7项: 8
    
    
    v1.push_back(22222)操作
    v1中元素的个数是: 9
    v1容量是: 16
    v1当前各项:
      第0项: 1
      第1项: 2
      第2项: 3
      第3项: 4
      第4项: 5
      第5项: 6
      第6项: 7
      第7项: 8
      第8项: 22222
    
    
    v1.push_back(22222)操作
    v1中元素的个数是: 8
    v1容量是: 16
    v1当前各项:
      第0项: 1
      第1项: 2
      第2项: 3
      第3项: 4
      第4项: 5
      第5项: 6
      第6项: 7
      第7项: 22222
    
    
    v1.pop_back()操作
    v1中元素的个数是: 7
    v1容量是: 16
    v1当前各项:
      第0项: 1
      第1项: 2
      第2项: 3
      第3项: 4
      第4项: 5
      第5项: 6
      第6项: 7
    
    
    测试结束
  • 相关阅读:
    转:Windows Socket五种I/O模型
    C++线程池的实现(二)
    C++ 简单 Hash容器的实现
    C++ TrieTree(字典树)容器的实现
    转载:C++线程池的一个实现
    C++用数组实现的静态队列
    C++ 类成员函数作为参数
    C++位操作符总结
    C++简单单例模式
    C++控制程序只运行一个实例
  • 原文地址:https://www.cnblogs.com/do-your-best/p/11267857.html
Copyright © 2020-2023  润新知