std::vector 是物理存储空间连续、无需指定大小、自动扩容的动态数组,并提供了丰富的 API。
初练
- 添加元素(末尾):push_back
- 删除元素(末尾):pop_back
- 获取元素个数:size
- 遍历方法
1 #include <iostream> 2 #include <vector> 3 4 int main() 5 { 6 std::vector<int> container; 7 container.push_back(1); 8 container.push_back(2); 9 container.push_back(3); 10 container.push_back(4); 11 container.push_back(5); 12 for (int i = 0; i < container.size(); ++i) std::cout << 13 container[i] << ' '; 14 std::cout << std::endl; 15 16 container.pop_back(); 17 container.pop_back(); 18 for (auto& e : container) std::cout << e << ' '; 19 std::cout << std::endl; 20 }
新手
- 删除某一索引或某一范围的元素(传入其迭代器):erase
- 删除所有元素:clear
- 适用自定义类型
- 引用传递避免直接拷贝
1 #include <iostream> 2 #include <vector> 3 4 struct Vertex 5 { 6 float x, y, z; 7 }; 8 9 std::ostream& operator<<(std::ostream& stream, const Vertex& vertex) 10 { 11 stream << vertex.x << ", " << vertex.y << ", " << vertex.z; 12 return stream; 13 } 14 15 void Print(const std::vector<Vertex>& vertices) 16 { 17 std::cout << "----------------------------------------" << 18 std::endl; 19 for (auto& v : vertices) std::cout << v << std::endl; 20 std::cout << "----------------------------------------" << 21 std::endl; 22 } 23 24 int main() 25 { 26 std::vector<Vertex> vertices; 27 vertices.push_back({ 1, 2, 3 }); 28 vertices.push_back({ 4, 5, 6 }); 29 vertices.push_back({ 7, 8, 9 }); 30 vertices.push_back({ 10, 11, 12 }); 31 vertices.push_back({ 13, 14, 15 }); 32 vertices.push_back({ 16, 17, 18 }); 33 34 Print(vertices); 35 vertices.erase(vertices.begin() + 1); // Delete index 1 36 Print(vertices); 37 vertices.erase(vertices.begin() + 2, vertices.end() - 1); // 38 Delete index 2, 3 39 Print(vertices); 40 vertices.clear(); // No any element 41 Print(vertices); 42 }
-
分配的内存容量、物理容量(动态扩充):capacity
-
指定分配的内存容量大小:reserve
-
就地创建并添加元素:emplace_back
-
两种避免多余元素拷贝的方法
1 #include <iostream> 2 #include <vector> 3 4 struct Vertex 5 { 6 float x, y, z; 7 8 Vertex(float x, float y, float z) 9 : x(x), y(y), z(z) 10 { 11 } 12 13 Vertex(const Vertex& vertex) 14 : x(vertex.x), y(vertex.y), z(vertex.z) 15 { 16 std::cout << "Copied!" << std::endl; 17 } 18 }; 19 20 void PrintCapacity(const std::vector<Vertex>& vertices) 21 { 22 std::cout << vertices.capacity() << std::endl; 23 } 24 25 int main() 26 { 27 // The code that can be optimized. 28 std::cout << "----------------------------------------" << 29 std::endl; 30 { 31 std::vector<Vertex> vertices; 32 vertices.push_back({ 1, 2, 3 }); 33 PrintCapacity(vertices); 34 vertices.push_back({ 4, 5, 6 }); 35 PrintCapacity(vertices); 36 vertices.push_back({ 7, 8, 9 }); 37 PrintCapacity(vertices); 38 } 39 40 // Method 1: use reserve to avoid resizing capacity to decrease 41 copies. 42 std::cout << "----------------------------------------" << 43 std::endl; 44 { 45 std::vector<Vertex> vertices; 46 vertices.reserve(3); 47 PrintCapacity(vertices); 48 vertices.push_back({ 1, 2, 3 }); 49 vertices.push_back({ 4, 5, 6 }); 50 vertices.push_back({ 7, 8, 9 }); 51 PrintCapacity(vertices); 52 } 53 54 // Method 2: use emplace_back to efficiently construct element 55 inside the vector container. 56 std::cout << "----------------------------------------" << 57 std::endl; 58 { 59 std::vector<Vertex> vertices; 60 vertices.reserve(3); 61 vertices.emplace_back(1, 2, 3); 62 vertices.emplace_back(4, 5, 6); 63 vertices.emplace_back(7, 8, 9); 64 } 65 66 std::cout << "----------------------------------------" << 67 std::endl; 68 }
中级
-
获取第一个元素的引用:front
-
获取最后一个元素的引用:back
-
获取第一个元素的指针:data
-
修改 size:resize
1 #include <iostream> 2 #include <vector> 3 4 int main() 5 { 6 std::vector<int> v; 7 v.push_back(1); 8 v.push_back(2); 9 v.push_back(3); 10 11 std::cout << "------------front/back------------" << std::endl; 12 std::cout << v.front() << std::endl; 13 v.front() = 100; // this will change v[0] 14 std::cout << v[0] << std::endl; 15 std::cout << v.back() << std::endl; 16 17 std::cout << "------------data------------" << std::endl; 18 int* p = v.data(); 19 std::cout << (&(v.front()) == p) << std::endl; // 1 20 std::cout << *p << std::endl; 21 std::cout << p[1] << std::endl; 22 23 std::cout << "------------resize------------" << std::endl; 24 for (auto& e : v) std::cout << e << " "; 25 std::cout << std::endl; 26 v.resize(5, -1); 27 for (auto& e : v) std::cout << e << " "; 28 std::cout << std::endl; 29 std::cout << "size=" << v.size() << " capacity=" << v.capacity() 30 << std::endl; // both increase 31 }
- 释放不必要的空间:shrink_to_fit
1 #include <iostream> 2 #include <vector> 3 4 int main() 5 { 6 std::vector<int> v; 7 for (int i = 0; i < 100000; ++i) v.push_back(i); 8 v.resize(3); 9 10 std::cout << "size=" << v.size() << std::endl; 11 std::cout << "capacity=" << v.capacity() << std::endl; 12 std::cout << "-----------------------------------------" << 13 std::endl; 14 15 v.shrink_to_fit(); 16 std::cout << "size=" << v.size() << std::endl; 17 std::cout << "capacity=" << v.capacity() << std::endl; 18 }
- 插入元素:insert
1 #include <iostream> 2 #include <vector> 3 4 void Print(const std::vector<int>& v) 5 { 6 for (auto& e : v) std::cout << e << " "; 7 std::cout << std::endl; 8 } 9 10 int main() 11 { 12 std::vector<int> v; 13 v.emplace_back(1); 14 v.emplace_back(2); 15 v.emplace_back(3); 16 Print(v); 17 18 std::cout << "------------insert(v.begin(), 100)------------" << 19 std::endl; 20 v.insert(v.begin(), 100); // insert at index 0 21 Print(v); 22 23 std::cout << "------------insert(v.begin(), 2, 1000)------------" 24 << std::endl; 25 v.insert(v.begin(), 2, 1000); // insert 2 items at index 0 26 Print(v); 27 28 std::cout << "------------insert(std::next(v.begin(), 3), 29 other.begin(), other.end())------------" << std::endl; 30 std::vector<int> other(2, 666); 31 v.insert(std::next(v.begin(), 3), other.begin(), other.end()); // 32 insert a range at index 3 33 Print(v); 34 35 std::cout << "------------insert(v.begin(), arr, arr + 36 std::size(arr))------------" << std::endl; 37 int arr[3] = { 888, 888, 888 }; 38 v.insert(v.begin(), arr, arr + std::size(arr)); // insert a array 39 at index 0 40 Print(v); 41 42 std::cout << "------------insert(v.end(), { 999, 999 43 })------------" << std::endl; 44 v.insert(v.end(), { 999, 999 }); // insert literals 45 Print(v); 46 }
高级 or 骨灰级
Life is short. keep it simple.