首页 > 代码库 > 【cpp】Vector

【cpp】Vector

 

 

 

 

这vector 很有用

 

// compile with: /EHsc  
#include <vector>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    vector<int> v1, v2, v3;  
  
    v1.push_back(10);  
    v1.push_back(20);  
    v1.push_back(30);  
    v1.push_back(40);  
    v1.push_back(50);  
  
    cout << "v1 = ";  
    for (auto& v : v1){  
        cout << v << " ";  
    }  
    cout << endl;  
  
    v2.assign(v1.begin(), v1.end());  
    cout << "v2 = ";  
    for (auto& v : v2){  //这个auto,应该是自动识别类型的意思,参见http://blog.csdn.net/huang_xw/article/details/8760403
        cout << v << " ";  
    }  
    cout << endl;  
  
    v3.assign(7, 4);  
    cout << "v3 = ";  
    for (auto& v : v3){  
        cout << v << " ";  
    }  
    cout << endl;  
  
    v3.assign({ 5, 6, 7 });  //注意这里的大括号
    for (auto& v : v3){  
        cout << v << " ";  
    }  
    cout << endl;  
}

技术分享

 

【cpp】Vector