一:介紹
vector是C++標準模板庫,是一個容器,底層是數組,為連續內存。
命名空間為std,所屬頭文件為<vector> 注意:不是<vector.h>
vector存儲數據時,會分配一個存儲空間,如果繼續存儲,該分配的空間已滿,就會分配一塊更大的內存,把原來的數據復制過來,繼續存儲,這些性能也會一定程度上會有損耗
二:常用操作
容量:
修改:
迭代器:
訪問元素:
三:存儲
簡單存儲
//存儲方式1 vector<int> v1(10); for (int i=0; i<10; i++) { v1[i] = i; } //存儲方式2 vector<int> v2; for (int i=0; i<10; i++) { v2.push_back(i); }
存儲結構體和結構體指針
struct Student { char name[32]; int age; }; //存儲結構體 vector<Student> vStu1; for (int i=0; i<10; i++) { Student stu; strcpy(stu.name, "woniu201"); stu.age = 30 + i; vStu1.push_back(stu); } //存儲結構體指針 vector<Student*> vStu2; for (int i=0; i<10; i++) { Student* pStu = (Student*)malloc(sizeof(Student)); strcpy(pStu->name, "woniu201"); pStu->age = 30 + i; vStu2.push_back(pStu); }
四:vector遍歷
vector<int> v; for (int i=0; i<100; i++) { v.push_back(i); } //遍歷方式1 for (int i=0; i<100; i++) { int& a = v[i]; printf("%d ", a); } //遍歷方式2 for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { int&a = *it; printf("%d ", a); }
五:排序
對vector整形進行排序
#include "stdlib.h"#include <vector>#include <algorithm>using namespace std;//升序比較函數int compare1(const int &a, const int &b){ return a < b;}//降序比較函數int compare2(const int &a, const int &b){ return a > b;}int main(){ vector<int> v; for (int i=0; i<10; i++) { v.push_back(rand() % 10); } //遍歷輸出 printf("排序前數據:"); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { printf("%d ", *it); } //升序排序 sort(v.begin(), v.end(), compare1); //遍歷輸出 printf("/n升序后數據:"); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { printf("%d ", *it); } //降序排序 sort(v.begin(), v.end(), greater<int>()); //遍歷輸出 printf("/n降序后數據:"); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { printf("%d ", *it); } getchar(); return 1;}
對存放類成員變量排序
#include <string>#include <vector>#include <algorithm>using namespace std;class Student {public: Student(string n, int c) :name(n), core(c) {} string name; int core;};//升序比較函數bool compare1(const Student& s1, const Student& s2){ return s1.core < s2.core;}//降序比較函數bool compare2(const Student& s1, const Student& s2){ return s1.core > s2.core;}int main(){ vector<Student> v; Student s1("aaaa", 97); Student s2("bbbb", 99); Student s3("cccc", 95); v.push_back(s1); v.push_back(s2); v.push_back(s3); printf("排序前數據:/n"); for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) { printf("%s; %d/n", ((*it).name).c_str(), (*it).core); } //升序排序 sort(v.begin(), v.end(), compare1); printf("/n升序后的數據:/n"); for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) { printf("%s; %d/n", ((*it).name).c_str(), (*it).core); } //降序排序 sort(v.begin(), v.end(), compare2); printf("/n降序后的數據:/n"); for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) { printf("%s; %d/n", ((*it).name).c_str(), (*it).core); } getchar(); return 1;}
六:查找
vector<int>::iterator it = find(v.begin(), v.end(), 5); if(it != v.end()) { cout << "found"; } else { cout << "not found"; }
七:刪除
for(vector<int>::iterator it=v.begin(); it != v.end(); it++) { if(*it == 8) { it = v.erase(it);//it會++一次 it--; //刪除完后需要--,否則最終循環越界 } }
八:釋放內存
存放整形vector釋放
//存放整型 vector<int> v; for (int i=0; i<100; i++) { v.push_back(i); } //釋放內存 { vector<int> vEmpty; v.swap(vEmpty); }
存放結構體vector釋放
//存儲結構體 vector<Student> vStu1; for (int i=0; i<10; i++) { Student stu; strcpy(stu.name, "woniu201"); stu.age = 30 + i; vStu1.push_back(stu); } //釋放內存 { vector<Student> } vector<Student> vEmpty; vStu1.swap(vEmpty);
存放結構體指針vector釋放
//存儲結構體指針 vector<Student*> vStu2; for (int i=0; i<10; i++) { Student* pStu = (Student*)malloc(sizeof(Student)); strcpy(pStu->name, "wangpengfei"); pStu->age = 30 + i; vStu2.push_back(pStu); } //釋放內存 for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++) { if (NULL != *it) { delete *it; *it = NULL; } }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答