STL:vector用法總結

一:介紹

vector是C++標準模板庫,是一個容器,底層是數組,爲連續內存。
命名空間爲std,所屬頭文件爲<vector>   注意:不是<vector.h>
vector存儲數據時,會分配一個存儲空間,若是繼續存儲,該分配的空間已滿,就會分配一塊更大的內存,把原來的數據複製過來,繼續存儲,這些性能也會必定程度上會有損耗

mysql

二:經常使用操做

1.容量

a.vector大小:vector.size()
b.vector所佔內存實際大小:vector.capacity()redis

2.修改

a.尾部添加元素:vector.push_back()
b.尾部刪除元素:vector.pop_back()
c.交換兩個vector元素:vector.swap()
d.清空vector元素:vector.clear()
e.刪除指定元素:vector.erase(it)
sql

3.迭代器

a.vector開始指針:vector.begin()
b.vector尾部指針:vector.end()   注:最後一個元素的下一個位置,相似爲NULL,不是容器的最後一個元素docker

4.訪問元素

a.下標訪問:vector[1]  //不檢查是否越界
b.at方法訪問:vector.at(1) //自動檢查是否越界,如越界會拋出異常
c.訪問第一個元素:vector.front()
d.訪問最後一個元素:vector.back()

shell

三:存儲

簡單存儲
 
 1     //存儲方式1
 2     vector<int> v1(10);  3     for (int i=0; i<10; i++)  4  {  5         v1[i] = i;  6  }  7     //存儲方式2
 8     vector<int> v2;  9     for (int i=0; i<10; i++) 10  { 11  v2.push_back(i); 12     }

存儲結構體和結構體指針編程

 

 1     struct Student  2  {  3         char name[32];  4         int age;  5  };  6  
 7     //存儲結構體
 8     vector<Student> vStu1;  9     for (int i=0; i<10; i++) 10  { 11  Student stu; 12         strcpy(stu.name, "woniu201"); 13         stu.age = 30 + i; 14  vStu1.push_back(stu); 15  } 16     //存儲結構體指針
17     vector<Student*> vStu2; 18     for (int i=0; i<10; i++) 19  { 20         Student* pStu = (Student*)malloc(sizeof(Student)); 21         strcpy(pStu->name, "woniu201"); 22         pStu->age = 30 + i; 23  vStu2.push_back(pStu); 24     }

四:遍歷

 1     vector<int> v;  2     for (int i=0; i<100; i++)  3  {  4  v.push_back(i);  5  }  6     //遍歷方式1
 7     for (int i=0; i<100; i++)  8  {  9         int& a = v[i]; 10         printf("%d ", a); 11  } 12     //遍歷方式2
13     for (vector<int>::iterator it = v.begin(); it != v.end(); it++) 14  { 15         int&a = *it; 16         printf("%d ", a); 17     }

五:排序

對vector整形進行排序設計模式

 1 #include "stdlib.h"
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using namespace std;  6 
 7 //升序比較函數
 8 int compare1(const int &a, const int &b)  9 { 10     return a < b; 11 } 12 
13 //降序比較函數
14 int compare2(const int &a, const int &b) 15 { 16     return a > b; 17 } 18 
19 int main() 20 { 21     vector<int> v; 22     for (int i=0; i<10; i++) 23  { 24         v.push_back(rand() % 10); 25  } 26 
27     //遍歷輸出
28     printf("排序前數據:"); 29     for (vector<int>::iterator it = v.begin(); it != v.end(); it++) 30  { 31         printf("%d ", *it); 32  } 33 
34     //升序排序
35  sort(v.begin(), v.end(), compare1); 36 
37     //遍歷輸出
38     printf("\n升序後數據:"); 39     for (vector<int>::iterator it = v.begin(); it != v.end(); it++) 40  { 41         printf("%d ", *it); 42  } 43 
44     //降序排序
45     sort(v.begin(), v.end(), greater<int>()); 46 
47     //遍歷輸出
48     printf("\n降序後數據:"); 49     for (vector<int>::iterator it = v.begin(); it != v.end(); it++) 50  { 51         printf("%d ", *it); 52  } 53 
54  getchar(); 55     return 1; 56 }

對存放類成員變量排序數組

 1 #include <string>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;  5  
 6 class Student {  7 public:  8     Student(string n, int c) :name(n), core(c) {}  9  
10     string name; 11     int core; 12 }; 13  
14 //升序比較函數
15 bool compare1(const Student& s1, const Student& s2) 16 { 17     return s1.core < s2.core; 18 } 19  
20 //降序比較函數
21 bool compare2(const Student& s1, const Student& s2) 22 { 23     return s1.core > s2.core; 24 } 25  
26  
27 int main() 28 { 29     vector<Student> v; 30     Student s1("aaaa", 97); 31     Student s2("bbbb", 99); 32     Student s3("cccc", 95); 33  
34  v.push_back(s1); 35  v.push_back(s2); 36  v.push_back(s3); 37  
38     printf("排序前數據:\n"); 39     for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) 40  { 41         printf("%s; %d\n", ((*it).name).c_str(), (*it).core); 42  } 43  
44     //升序排序
45  sort(v.begin(), v.end(), compare1); 46  
47     printf("\n升序後的數據:\n"); 48     for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) 49  { 50         printf("%s; %d\n", ((*it).name).c_str(), (*it).core); 51  } 52  
53     //降序排序
54  sort(v.begin(), v.end(), compare2); 55     printf("\n降序後的數據:\n"); 56     for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) 57  { 58         printf("%s; %d\n", ((*it).name).c_str(), (*it).core); 59  } 60  getchar(); 61     return 1; 62 }

六:查找

1     vector<int>::iterator it = find(v.begin(), v.end(), 5); 2     if(it != v.end()) 3  { 4         cout << "found"; 5  } 6     else
7  { 8         cout << "not found"; 9     }

七:刪除

1     for(vector<int>::iterator it=v.begin(); it != v.end(); it++) 2  { 3         if(*it == 8) 4  { 5             it = v.erase(it);//it會++一次
6             it--;       //刪除完後須要--,不然最終循環越界
7  } 8     }

八:釋放內存

存放整形vector釋放mybatis

1     //存放整型
2     vector<int> v; 3     for (int i=0; i<100; i++) 4  { 5  v.push_back(i); 6  } 7     //釋放內存
8     vector<int> (v).swap(v);

 

存放結構體vector釋放併發

//存儲結構體
    vector<Student> vStu1; for (int i=0; i<10; i++) { Student stu; strcpy(stu.name, "wangpengfei"); stu.age = 30 + i; vStu1.push_back(stu); } //釋放內存
    vector<Student> (vStu1).swap(vStu1);

 

存放結構體指針vector釋放

 

 1     //存儲結構體指針
 2     vector<Student*> vStu2;  3     for (int i=0; i<10; i++)  4  {  5         Student* pStu = (Student*)malloc(sizeof(Student));  6         strcpy(pStu->name, "wangpengfei");  7         pStu->age = 30 + i;  8  vStu2.push_back(pStu);  9  } 10     //釋放內存
11     for (vector<Student*>::iterator it = vStu2.begin(); it != vStu2.end(); it++) 12  { 13         if (NULL != *it) 14  { 15             delete *it; 16             *it = NULL; 17  } 18     }

 

掃碼關注公衆號

專一分享Java,C/C++,STL,Spring框架,mybatis框架,mysql,redis,分佈式,高併發,設計模式,爬蟲,docker,shell編程等相關技術,在這裏一塊兒探討,一塊兒學習,一塊兒進步,不按期分享視頻書籍資源,充分利用碎片化時間,讓咱們的技術之路更加有樂趣。

相關文章
相關標籤/搜索