- 基本用法
#include<iostream> #include<vector> using namespace std; void main() { vector<int> a(10,1);//初始化容器,開辟10個單位空間·元素初始化為1 int i; cout << "初始化變量" << endl; for (int i=0;i<a.size();i++) { cout << a[i] << " "; } cout << "插入數據" << endl; cin >> a[2]; cin>> a[5]; cin >> a[8]; cout << "賦值之后的變量" << endl; for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; }
輸出結果:
- 常見花式操作
#include<iostream> #include<vector> using namespace std; void main() { int mynum[] = {8,9,12,24,35}; int i = 0; vector<int> a(mynum,mynum+5);//初始化容器,開辟10個單位空間·元素初始化為1 for (i=0;i<a.size();i++) { cout << a[i] << " "; } cout <<endl; vector<int> b(a.begin(), a.begin()+3);//借助另一容器的開始,及后面連續的n個單位 for (i = 0; i < b.size(); i++) { cout << b[i] << " "; } cout << endl; vector<int> c(&mynum[3], &mynum[5]);//以數組的第三個元素地址起,3個單位 for (i = 0; i < c.size(); i++) { cout << c[i] << " "; } }
輸出結果:
- 二維數組vector<vector<int>>a(4,vector<int>(4,8))
#include<iostream> #include<vector> using namespace std; void main() { //用vector聲明一個4*4的矩陣 vector<vector <int>>a(4,vector<int>(4,8)); int i = 0; int j = 0; for (i=0;i<a.size();i++) { for (j=0;j<a[i].size();j++) { cout << a[i][j] << " "; } cout << endl; } cin >> a[0][0]; cin >> a[1][1]; cin >> a[2][2]; cin >> a[3][3]; cout << "賦值后的語句"<<endl; for (i = 0; i < a.size(); i++) { for (j = 0; j < a[i].size(); j++) { cout << a[i][j] << " "; } cout << endl; } }
輸出結果:
- 用vector容器盛放一個類
#include<iostream> #include<string> #include<vector> using namespace std; class mycoach { public: friend ostream &operator<<(ostream &out, mycoach &t); mycoach(string name,int age) { this->name = name; this->age = age; } ~mycoach() { //cout << "回中式宿舍休息去了" << endl; } private: string name; int age; }; ostream &operator<<(ostream &out,mycoach &t) { out<< t.name << "......" << t.age << endl; return out; } void main() { vector<mycoach> v1; mycoach cpc("陳培昌", 22), fgf("付高峰", 30), xxd("徐曉冬", 40), mjx("明佳新", 22); v1.push_back(cpc);//把類對象壓入vector容器 v1.push_back(fgf); v1.push_back(xxd); v1.push_back(mjx); for (vector<mycoach>::iterator it= v1.begin(); it!=v1.end(); it++) { cout << *it << endl; } }
步驟一:聲明vector變量v1
步驟二:通過迭代器循環遍歷vector容器,for(vector<類型名>::iterator it(迭代器變量名) =v1.begin(); it!=v1.end();it++)
輸出結果:
- 把指針裝入vector容器
#include<iostream> #include<string> #include<vector> using namespace std; class mycoach { public: friend ostream &operator<<(ostream &out, mycoach &t); mycoach(string name,int age) { this->name = name; this->age = age; } mycoach(const mycoach &t) { this->name = t.name; this->age = t.age; } string name; int age; }; ostream &operator<<(ostream &out, mycoach &t) { out << t.name << "......" << t.age << endl; return out; } void main() { mycoach cpc("陳培昌", 22), fgf("付高峰", 30), xxd("徐曉冬", 40), mjx("明佳新", 22); mycoach *m1,*m2, *m3, *m4; m1 = &cpc; m2 = &fgf; m3 = &xxd; m4 = &mjx; vector<mycoach *> v1; v1.push_back(m1); v1.push_back(m2); v1.push_back(m3); v1.push_back(m4); for (vector<mycoach *>::iterator it=v1.begin();it!=v1.end();it++) { cout << (*it)->name << endl;//注意!把屬性聲明為public,否則無法通過指針直接訪問到 cout << (**it); } }
輸出結果:
- 查詢某一元素在容器中出現的次數
void main() { vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(5); v1.push_back(7); v1.push_back(8); for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) { cout << *it << endl; } int num = count(v1.begin(),v1.end(),5); cout << "5出現了" <<num<<"次"<< endl; system("pause"); }
輸出結果: