ヘッダー ファイル <vector> で定義
テンプレート< クラス T、 |
(1) | |
名前空間 pmr { テンプレート <クラス T> |
(2) | (C++17以降) |
1)std::vector
は動的配列をカプセル化するシーケンシャル コンテナです。
2)std::pmr::vector
は、多態性アロケータを使用するテンプレート エイリアスです。
要素は順番に格納されます。つまり、要素にはイテレータを通じてだけでなく、要素への通常のポインタによってもアクセスできます。これは、ベクトル要素へのポインタを、配列要素へのポインタを期待する任意の関数に渡すことができることを意味します。 |
(C++03以降) |
ベクターのストレージは自動的に管理され、必要に応じて拡張および縮小されます。ベクトルは通常、将来の増加を管理するためにより多くのメモリが割り当てられるため、静的配列よりも多くのスペースを占有します。ベクターの使用方法は、要素が挿入されるたびに再割り当てするのではなく、余分なメモリが使い果たされた場合にのみ使用されます。割り当てられたメモリの合計量は、capacity() 関数で照会できます。追加のメモリは、shrink_to_fit() の呼び出しを通じてシステムに返すことができます。(C++11以降)
再割り当ては通常、パフォーマンスを重視する操作です。要素の数がわかっている場合は、reserve() 関数を使用して再割り当てを排除できます。
ベクトルに対する一般的な演算の複雑さ (効率) は次のとおりです。
- ランダムアクセス - 定数 O(1)
- 最後に要素を挿入または削除 - 償却定数 O(1)
- 要素の挿入または削除 - ベクトルの終端までの距離が直線的な O(n)
std::vector
(bool
以外T
) Container、AllocatorAwareContainer、SequenceContainer、ContiguousContainer (C++17 以降) 、ReversibleContainerの要件を満たします。
非メンバー関数
ベクトル内の値を辞書順に比較します
operator==,!=,<,<=,>,>=(std::vector)
テンプレート< クラス T、クラス Alloc > bool 演算子==( const std::vector<T,Alloc>& lhs, const std::vector<T,Alloc>& rhs ); |
(1) | |
テンプレート< クラス T、クラス Alloc > bool 演算子!=( const std::vector<T,Alloc>& lhs, const std::vector<T,Alloc>& rhs ); |
(2) | |
テンプレート< クラス T、クラス Alloc > bool 演算子<( const std::vector<T,Alloc>& lhs, const std::vector<T,Alloc>& rhs ); |
(3) | |
テンプレート< クラス T、クラス Alloc > bool 演算子<=( const std::vector<T,Alloc>& lhs, const std::vector<T,Alloc>& rhs ); |
(4) | |
テンプレート< クラス T、クラス Alloc > bool 演算子>( const std::vector<T,Alloc>& lhs, const std::vector<T,Alloc>& rhs ); |
(5) | |
テンプレート< クラス T、クラス Alloc > bool 演算子>=( const std::vector<T,Alloc>& lhs, const std::vector<T,Alloc>& rhs ); |
(6) |
2 つのコンテナの内容を比較します。
1-2 lhs
)rhs
と の内容が等しいこと、つまり、それらが同じ数の要素を持ちlhs
、の各要素rhs
が の同じ位置にある要素と等しいかどうかをチェックします。
3-6)lhs
とrhs
。std::lexicographical_compare と同等の関数で比較します。
パラメータ
左、右 | - | 内容を比較するコンテナ |
- オーバーロード (1-2) を使用するには、EqualityComparableT の要件を満たす必要があります。 |
||
- オーバーロード (3-6) を使用するには、LessThanComparableT (LessThanComparable) の要件を満たす必要があります。順序関係は全体的な順序を確立する必要があります。 |
戻り値
1) コンテナの内容が等しい場合は true、それ以外の場合は false
2) コンテナの内容が等しくない場合は true、それ以外の場合は false
3 lhs
)の内容が辞書編集的に の内容より小さい場合は true、それ以外の場合は false rhs
4)lhs
の辞書編集的にの内容以下である場合は true、それ以外の場合は false rhs
5) 若 lhs
的内容按字典序大于 rhs
的内容则为 true ,否则为 false
6) 若 lhs
的内容按字典序大于或等于 rhs
的内容则为 true ,否则为 false
复杂度
1-2) 若 lhs
与 rhs
的大小不同则为常数,否则与容器大小成线性
3-6) 与容器大小成线性
特化 std::swap 算法
std::swap(std::vector)
template< class T, class Alloc > void swap( vector<T,Alloc>& lhs, vector<T,Alloc>& rhs ); |
(C++17 前) | |
template< class T, class Alloc > void swap( vector<T,Alloc>& lhs, vector<T,Alloc>& rhs ) noexcept(/* see below */); |
(C++17 起) |
为 std::vector 特化 std::swap 算法。交换 lhs
与 rhs
的内容。调用 lhs.swap(rhs) 。
参数
lhs, rhs | - | 要交换内容的容器 |
返回值
(无)
复杂度
常数。
异常
noexcept 规定: noexcept(noexcept(lhs.swap(rhs))) |
(C++17 起) |
调用示例
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <vector>
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;
//3) 构造拥有 count 个有值 value 的元素的容器。
std::vector<Cell> vector2(6, generate());
std::generate(vector2.begin(), vector2.end(), generate);
std::cout << "vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//6) 复制构造函数。构造拥有 other 内容的容器。
std::vector<Cell> vector3(vector1);
std::cout << "vector3: ";
std::copy(vector3.begin(), vector3.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
//1) 若容器内容相等则为 true ,否则为 false
std::cout << "vector1 == vector2 : " << (vector1 == vector2) << std::endl;
std::cout << "vector1 == vector3 : " << (vector1 == vector3) << std::endl;
//2) 若容器内容不相等则为 true ,否则为 false
std::cout << "vector1 != vector2 : " << (vector1 != vector2) << std::endl;
std::cout << "vector1 != vector3 : " << (vector1 != vector3) << std::endl;
//3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false
std::cout << "vector1 < vector2 : " << (vector1 < vector2) << std::endl;
std::cout << "vector1 < vector3 : " << (vector1 < vector3) << std::endl;
//4) 若 lhs 的内容按字典序小于或等于 rhs 的内容则为 true ,否则为 false
std::cout << "vector1 <= vector2 : " << (vector1 <= vector2) << std::endl;
std::cout << "vector1 <= vector3 : " << (vector1 <= vector3) << std::endl;
//5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false
std::cout << "vector1 > vector2 : " << (vector1 > vector2) << std::endl;
std::cout << "vector1 > vector3 : " << (vector1 > vector3) << std::endl;
//6) 若 lhs 的内容按字典序大于或等于 rhs 的内容则为 true ,否则为 false
std::cout << "vector1 >= vector2 : " << (vector1 >= vector2) << std::endl;
std::cout << "vector1 >= vector3 : " << (vector1 >= vector3) << std::endl;
std::cout << std::endl;
//为 std::vector 特化 std::swap 算法。
//交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
std::cout << "swap before:" << std::endl;
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
vector1.swap(vector2);
std::cout << "swap after:" << std::endl;
std::cout << "vector1: ";
std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "vector2: ";
std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
return 0;
}
输出