zoukankan      html  css  js  c++  java
  • 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
    
    
    测试结束
  • 相关阅读:
    javascript有用小技巧—实现分栏显示
    在Apk应用程序内,查找某个Activity。
    NodeJS系列~第一个小例子,实现了request.querystring功能
    IOS开发(objective-c)~开篇有理
    爱上MVC3~布局页的继承与扩展
    我心中的核心组件(可插拔的AOP)~第五回 消息组件
    基础才是重中之重~方法override详解
    数据结构 练习 20-查找 算法
    cocos2d-x适配多分辨率
    VS2010中使用CL快速 生成DLL的方法
  • 原文地址:https://www.cnblogs.com/do-your-best/p/11267857.html
Copyright © 2011-2022 走看看