C++ vector 功能简单介绍

xiaoxiao2021-02-28  99

#include <iostream> #include <vector> using namespace std; int main() { /* class vector index 0 1 2... 索引从0 1 2... member type value_type allocator_type reference const_reference pointer const_pointer iterator const_iterator reverse_iterator const_reverse_iterator difference_type size_type Allocator: get_allocator Non_member function overloads relational operators swap */ /* 初始化 Member functions: (constructor) (destructor) operator= 1. empty container constructor(default constructor) 2. fill constructor 3. range constructor 4. copy constructor(and copying with allocator) 5. initializer list constructor */ //constructors used in the same order as described above vector<int> first; vector<int> second(4, 100); vector<int> third(second.begin(), second.end()); vector<int> fourth(third); // the iterator constructor can also be used to construct from arrays; int myints[] = { 16, 2, 77,29 }; vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int)); vector<int> sixth(myints, myints + 4); /* Iterator: begin end rbegin rend cbegin cend crbegin crend */ // begin end for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) cout << ' ' << *it; cout << endl; // rbegin rend 反向迭代器 for (vector<int>::reverse_iterator rit = fifth.rbegin(); rit != fifth.rend(); ++rit) cout << ' ' << *rit; cout << endl; // crbegin crend for (auto rit = fifth.rbegin(); rit != fifth.rend(); ++rit) cout << ' ' << *rit; /* Capacity: size max_size resize capacity empty reserve shrink_to_fit */ vector<int> v; //std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl; v.reserve(10); //std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl; v.resize(10); v.push_back(0); //std::cout << "v.size() == " << v.size() << " v.capacity() = " << v.capacity() << std::endl; /* Element access: operator[] at front back data 返回 一个指向第一个元素的指针 */ vector<int> vec; for (int i = 0; i < 10; i++) vec.push_back(i); int *pi = vec.data(); //cout << vec.at(0) << ' ' << vec[0] << ' ' << vec.front() << ' ' << vec.back() << ' ' << *pi << ' ' << pi[2] << endl; /* Modifiers: assign 赋值 push_back add the element at the end pop_back delete the last element insert 插入一个值或者一个数组或者一个vector erase 删除某一个位置上的 或者 一段 swap clear emplace emplace_back */ // vec 0 1 2 3 4 5 6 7 8 9 vec.pop_back(); // 0 1 2 3 4 5 6 7 8 for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) cout << ' ' << *it; vector<int>::iterator it; it = vec.begin(); it = vec.insert(it, 200); //200 0 1 2 3 4 5 6 7 8 vec.insert(it, 2, 300); // 300 300 200 0 1 2 3 4 5 6 7 8 // " it " no longer valid, get a new one: int arr[] = { 501, 502, 503 }; vec.insert(vec.begin(), arr, arr + 3); // 501 502 503 300 300 200 0 1 2 3 4 5 6 7 8 vector<int> myvec = { 10, 20, 30 }; auto it0 = myvec.emplace(myvec.begin() + 1, 100); //10 100 20 30 myvec.emplace(it0, 200); // 10 200 100 20 30 for (auto& x : myvec) // 厉害的语法 cout << ' ' << x; vec.swap(myvec); for (auto& x : myvec) // 厉害的语法 cout << ' ' << x; return 0; }
转载请注明原文地址: https://www.6miu.com/read-38355.html

最新回复(0)