Defined in the header file <vector>
template< class T, |
(1) | |
namespace pmr { template <class T> |
(2) | (since C++17) |
1) std::vector
is a sequential container that encapsulates a dynamic array.
2) std::pmr::vector
is a template alias that uses a polymorphic allocator.
Elements are stored sequentially, which means that elements can be accessed not only through iterators, but also with regular pointers to elements. This means that a pointer to a vector element can be passed to any function expecting a pointer to an array element. |
(since C++03) |
The storage of vector is managed automatically, expanding and shrinking on demand. A vector typically takes up more space than a static array because more memory is allocated to manage future growth. The way the vector is used is not to reallocate every time an element is inserted, but only when the extra memory is exhausted. The total amount of memory allocated can be queried with the capacity() function. Additional memory can be returned to the system through a call to shrink_to_fit(). (since C++11)
Reallocation is usually a performance-intensive operation. The reserve() function can be used to eliminate reallocation if the number of elements is known.
The complexity (efficiency) of common operations on vector is as follows:
- Random access - constant O(1)
- Insert or remove elements at the end - amortized constant O(1)
- Insert or remove elements - O(n) linear in distance to end of vector
std::vector
(For bool
other T
) Satisfy the requirements of Container , AllocatorAwareContainer , SequenceContainer , ContiguousContainer (since C++17) and ReversibleContainer .
iterator
Returns an iterator pointing to the first element of the container
std::vector<T,Allocator>::begin,
std::vector<T,Allocator>::cbegin
iterator begin(); |
(until C++11) | |
iterator begin() noexcept; |
(since C++11) | |
const_iterator begin() const; |
(until C++11) | |
const_iterator begin() const noexcept; |
(since C++11) | |
const_iterator cbegin() const noexcept; |
(since C++11) |
Returns an iterator pointing to the first element of the container.
If the container is empty, the returned iterator will be equal to end().
parameter
(none)
return value
Iterator pointing to the first element.
the complexity
constant.
returns an iterator pointing to the end of the container
std::vector<T,Allocator>::end,
std::vector<T,Allocator>::cend
iterator end(); |
(until C++11) | |
iterator end() noexcept; |
(since C++11) | |
const_iterator end() const; |
(until C++11) | |
const_iterator end() const noexcept; |
(since C++11) | |
const_iterator cend() const noexcept; |
(since C++11) |
Returns an iterator pointing to the element following the last element of the container.
This element behaves as a placeholder; attempting to access it results in undefined behavior.
parameter
(none)
return value
An iterator pointing to the last element following.
the complexity
constant.
Returns a reverse iterator pointing to the last element of the container
std::vector<T,Allocator>::rbegin,
std::vector<T,Allocator>::crbegin
reverse_iterator rbegin(); |
(until C++11) | |
reverse_iterator rbegin() noexcept; |
(since C++11) | |
const_reverse_iterator rbegin() const; |
(until C++11) | |
const_reverse_iterator rbegin() const noexcept; |
(since C++11) | |
const_reverse_iterator crbegin() const noexcept; |
(since C++11) |
Returns a reverse iterator pointing to the first element of the reversed container. It corresponds to the last element of the non-reverse container.
parameter
(none)
return value
Reverse iterator pointing to the first element.
the complexity
constant.
returns a reverse iterator pointing to the front
std::vector<T,Allocator>::rend,
std::vector<T,Allocator>::crend
reverse_iterator rend(); |
(until C++11) | |
reverse_iterator rend() noexcept; |
(since C++11) | |
const_reverse_iterator rend() const; |
(until C++11) | |
const_reverse_iterator rend() const noexcept; |
(since C++11) | |
const_reverse_iterator crend() const noexcept; |
(since C++11) |
Returns a reverse iterator pointing to the element following the last element of the reversed container. It corresponds to the previous element of the first element of the non-reverse container. This element behaves as a placeholder and attempting to access it results in undefined behavior.
parameter
(none)
return value
Reverse iterator pointing to the element after the last element.
the complexity
constant.
call example
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <vector>
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
int main()
{
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return cell;
};
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector<Cell> vector1(6, generate());
std::generate(vector1.begin(), vector1.end(), generate);
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
//返回指向容器首元素的迭代器。若容器为空,则返回的迭代器将等于 end() 。
//返回指向容器末元素后一元素的迭代器。此元素表现为占位符;试图访问它导致未定义行为。
for (std::vector<Cell>::iterator it = vector1.begin(); it != vector1.end(); it++)
{
Cell cell = generate();
*it = cell;
std::cout << "iterator: " << &(*it) << " = " << cell << std::endl;
}
std::cout << std::endl;
for (std::vector<Cell>::const_iterator cit = vector1.cbegin(); cit != vector1.cend(); cit++)
{
std::cout << "const_iterator: " << &(*cit) << " : " << *cit << std::endl;
}
std::cout << std::endl;
//返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。
//返回指向逆向容器末元素后一元素的逆向迭代器。
//它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。
for (std::vector<Cell>::reverse_iterator rit = vector1.rbegin(); rit != vector1.rend(); rit++)
{
Cell cell = generate();
*rit = cell;
std::cout << "reverse_iterator: " << &(*rit) << " = " << cell << std::endl;
}
std::cout << std::endl;
for (std::vector<Cell>::const_reverse_iterator crit = vector1.crbegin(); crit != vector1.crend(); crit++)
{
std::cout << "const_reverse_iterator: " << &(*crit) << " : " << *crit << std::endl;
}
std::cout << std::endl;
return 0;
}
output