目录
2. Iterator invalidation(迭代器失效的情况)
1.查看refrence
2. Iterator invalidation(迭代器失效的情况)
Operations(操作) | Invalidated(失效场景) |
All read only operations | Never |
swap, std::swap | end() |
clear, operator=, assign | Always |
reserve, shrink_to_fit | If the vector changed capacity, all of them. If not, none. |
erase | Erased elements and all elements after them (including end()) |
push_back, emplace_back | If the vector changed capacity, all of them. If not, only end(). |
insert, emplace | If the vector changed capacity, all of them. If not, only those at or after the insertion point (including end()). |
resize | If the vector changed capacity, all of them. If not, only end() and any elements erased. |
pop_back | The element erased and end(). |
3.Member types
Member type | Definition |
value_type | T |
allocator_type | Allocator |
size_type | Unsigned integer type (usually std::size_t) |
difference_type | Signed integer type (usually std::ptrdiff_t) |
reference | Allocator::reference(until C++11) |
value_type&(since C++11) | |
const_reference | Allocator::const_reference(until C++11) |
const value_type&(since C++11) | |
pointer | Allocator::pointer(until C++11) |
std::allocator_traits<Allocator>::pointer(since C++11) | |
const_pointer | Allocator::const_pointer(until C++11) |
std::allocator_traits<Allocator>::const_pointer(since C++11) | |
iterator | LegacyRandomAccessIterator to value_type |
const_iterator | LegacyRandomAccessIterator to const value_type |
reverse_iterator | std::reverse_iterator<iterator> |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
4.Member functions
Member functions | |
(constructor) | constructs the vector |
(public member function) | |
(destructor) | destructs the vector |
(public member function) | |
operator= | assigns values to the container |
(public member function) | |
assign | assigns values to the container |
(public member function) | |
get_allocator | returns the associated allocator |
(public member function) |
5.Element access
at | access specified element with bounds checking |
(public member function) | |
operator[] | access specified element |
(public member function) | |
front | access the first element |
(public member function) | |
back | access the last element |
(public member function) | |
data(C++11) | direct access to the underlying array |
(public member function) |
6.Iterators
(C++11) | returns an iterator to the beginning (public member function) |
(C++11) | returns an iterator to the end (public member function) |
(C++11) | returns a reverse iterator to the beginning (public member function) |
(C++11) | returns a reverse iterator to the end (public member function) |
7.Capacity
checks whether the container is empty (public member function) | |
returns the number of elements (public member function) | |
returns the maximum possible number of elements (public member function) | |
reserves storage (public member function) | |
returns the number of elements that can be held in currently allocated storage (public member function) | |
(C++11) | reduces memory usage by freeing unused memory (public member function) |
8.Modifiers
clears the contents (public member function) | |
inserts elements (public member function) | |
(C++11) | constructs element in-place (public member function) |
erases elements (public member function) | |
adds an element to the end (public member function) | |
(C++11) | constructs an element in-place at the end (public member function) |
removes the last element (public member function) | |
changes the number of elements stored (public member function) | |
swaps the contents (public member function) |
9.Non-member functions
operator!=(removed in C++20) operator<(removed in C++20) operator<=(removed in C++20) operator>(removed in C++20) operator>=(removed in C++20) operator<=>(C++20) | lexicographically compares the values in the vector (function template) |
specializes the std::swap algorithm (function template) | |
erase(std::vector)(C++20) erase_if(std::vector)(C++20) | Erases all elements satisfying specific criteria(function template) |
10.应用举例
10.1 初始化,插入与遍历的一个小例子
// g++ 001-vector.cpp -std=c++11
#include <iostream>
#include <vector>
int main()
{
//Create a vector containing integers
std::vector<int> v = { 7, 5, 16, 8 };
// Add two more integers to vector
v.push_back(25);
v.push_back(13);
// Print out the vector
std::cout << "v = { ";
for (int n : v) {
std::cout << n << ", ";
}
std::cout << "}; \n";
}
10.2 使用reserve(size_type new_cap)函数优化内存分配
#include <cstddef>
#include <new>
#include <vector>
#include <iostream>
// minimal C++11 allocator with debug output
template <class Tp>
struct NAlloc {
typedef Tp value_type;
NAlloc() = default;
template <class T> NAlloc(const NAlloc<T>&) {}
/* 申请内存 */
Tp* allocate(std::size_t n)
{
n *= sizeof(Tp);
std::cout << " allocating(申请内存) " << n << " bytes\n";
return static_cast<Tp*>(::operator new(n));
}
/* 释放内存 */
void deallocate(Tp* p, std::size_t n)
{
std::cout << "deallocating(释放内存) " << n*sizeof*p << " bytes\n";
::operator delete(p);
}
};
template <class T, class U>
bool operator==(const NAlloc<T>&, const NAlloc<U>&) { return true; }
template <class T, class U>
bool operator!=(const NAlloc<T>&, const NAlloc<U>&) { return false; }
int main()
{
int sz = 100;
std::cout << "**********using reserve: \n";
{
std::vector<int, NAlloc<int>> v1;
v1.reserve(sz);
for(int n = 0; n < sz; ++n)
v1.push_back(n);
}
std::cout << "\n\n\n====================================================================\n\n\n";
std::cout << "**********not using reserve: \n";
{
std::vector<int, NAlloc<int>> v1;
for(int n = 0; n < sz; ++n)
v1.push_back(n);
}
}
10.3 【push_back】VS【emplace_back】
(1)push_back直接构造,本质依然是需要先调用构造函数构造一个临时变量,再把这个临时变量拷贝构造进入vector的内存空间中;
(2)emplace_back没有构造临时对象,而是直接在vector的内存空间中原地构造.