vector:动态数组
单口容器,从首位置或中间位置插入元素时,会使得后面的元素位置发生变化,所以一般建议用push_back来从后端追加添加元素。(下右图,deque是双口容易(双端队列))
容器的基本操作代码如下:(包含了巧用swap收缩空间及reserve预留空间)
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #include <cstdlib> 4 #include <vector> //动态数组 5 #include <algorithm> //算法 6 7 using namespace std; 8 9 void print_vector(vector<int> &v) { 10 for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { 11 cout << *it << " "; 12 } 13 cout << endl; 14 } 15 16 void test01() { 17 vector<int> v1;//默认构造 18 int arr[] = { 10,20,30,40 }; 19 vector<int> v2(arr, arr + sizeof(arr) / sizeof(int)); 20 vector<int> v3(v2.begin(), v2.end()); 21 vector<int> v4(v3); 22 23 print_vector(v2); 24 print_vector(v3); 25 print_vector(v4); 26 } 27 28 void test02() { 29 int arr[] = { 10,20,30,40 }; 30 vector<int> v1(arr, arr + sizeof(arr) / sizeof(int)); 31 vector<int> v2; 32 v2.assign(v1.begin(), v1.end()); 33 vector<int> v3; 34 v3 = v2; 35 36 int arr1[] = { 100,200,300,400 }; 37 vector<int> v4(arr1, arr1 + sizeof(arr1) / sizeof(int)); 38 39 print_vector(v1); 40 print_vector(v2); 41 print_vector(v3); 42 print_vector(v4); 43 44 cout << "-----------------" << endl; 45 46 v4.swap(v1);//指针交换,不是堆上的数据拷贝 47 print_vector(v1); 48 print_vector(v2); 49 print_vector(v3); 50 print_vector(v4); 51 52 } 53 54 void test03() { 55 int arr[] = { 10,20,30,40 }; 56 vector<int> v1(arr, arr + sizeof(arr) / sizeof(int)); 57 58 if (v1.empty()) { 59 cout << "为空" << endl; 60 } 61 else { 62 cout << "不为空" << endl; 63 } 64 print_vector(v1); 65 v1.resize(2);//传进的参数小于容器的size,会从后往前删除相应数量元素 66 print_vector(v1); 67 v1.resize(6, 1);//传进的参数大于容器的size,会用传进的第二个参数补全 68 print_vector(v1); 69 70 for (int i = 0; i < 10000; i++) { 71 v1.push_back(i); 72 } 73 74 cout << "size:" << v1.size() << endl;//元素个数 75 cout << "容量:" << v1.capacity() << endl;//容量 76 77 } 78 79 void test04() { 80 int arr[] = { 100,200,300,400 }; 81 vector<int> v1(arr, arr + sizeof(arr) / sizeof(int)); 82 for (int i = 0; i < v1.size(); i++) { 83 cout << v1[i] << " "; 84 } 85 cout << endl; 86 87 for (int i = 0; i < v1.size(); i++) { 88 cout << v1.at(i) << " "; 89 } 90 cout << endl; 91 //at是抛异常的,[]不抛异常 92 93 v1.push_back(10); 94 v1.push_back(20); 95 v1.insert(v1.begin(), 30); 96 v1.insert(v1.end(), 40); 97 v1.insert(v1.begin() + 2, 110);//vector支持迭代器随机访问 98 //一般情况下,支持数组下标,都支持随机访问 99 100 print_vector(v1); 101 102 //删除 103 v1.erase(v1.begin()); 104 print_vector(v1); 105 v1.erase(v1.begin() + 1, v1.end()); 106 print_vector(v1); 107 v1.clear(); 108 109 cout << "size:" << v1.size() << endl; 110 111 112 } 113 114 //巧用swap缩减空间 115 void test05(){ 116 //vector添加元素时会自动增长空间,但是删除元素时不会自动缩小空间 117 vector<int> v; 118 for (int i = 0; i < 100000; i++) { 119 v.push_back(i); 120 } 121 cout << "size:" << v.size() << endl; 122 cout << "capacity:" << v.capacity() << endl; 123 124 v.resize(10); 125 cout << "-------------" << endl; 126 cout << "size:" << v.size() << endl; 127 cout << "capacity:" << v.capacity() << endl; 128 129 //收缩空间 130 //vector<int>(v)这是一个用v初始化的匿名对象 131 //与v交换完指针后使v的空间压缩,匿名对象自动销毁 132 vector<int>(v).swap(v); 133 cout << "-------------" << endl; 134 cout << "size:" << v.size() << endl; 135 cout << "capacity:" << v.capacity() << endl; 136 } 137 138 void test06() { 139 //reserve预留空间 与resize区别 140 141 int num = 0; 142 int* address = NULL; 143 144 vector<int> v; 145 v.reserve(100000);//预留空间 146 for (int i = 0; i < 100000; i++) { 147 v.push_back(i); 148 if (address != &(v[0])) { 149 address = &(v[0]); 150 num++;//可以判断扩展空间的次数 151 } 152 } 153 154 cout << "num:" << num << endl; 155 //如果知道容器大概需要的存储空间的元素个数,那么可以用reserve预留空间 156 //避免多次扩展空间减低效率 157 } 158 159 int main(void) { 160 test06(); 161 162 system("pause"); 163 return 0; 164 }
vector_code
reserve和risize:
原文地址:https://www.cnblogs.com/zzx1905/p/vector_swap_reserve_resize.html
时间: 2024-10-11 10:51:42