ベクターは動的配列と同じで、要素が挿入または削除されると自動的にサイズを変更する機能があり、そのストレージはコンテナーによって自動的に処理されます。ベクトル要素は連続したストレージに配置されるため、反復子を使用してアクセスしたり走査したりできます。ベクトルでは、データは最後に挿入されます。配列の拡張が必要になる場合があるため、最後に挿入すると差分時間がかかります。サイズ変更が行われないため、最後の要素の削除には一定の時間しかかかりません。先頭または途中での挿入と消去は時間的に直線的です。
C++ の std::vector とは何ですか?
std::vector C++ では、ベクトル コンテナーとそのメンバー関数を含むクラス テンプレートです。内部で定義されています ヘッダファイル。 std::vector クラスのメンバー関数は、ベクター コンテナーにさまざまな機能を提供します。
C++ でベクトルを宣言する構文
std::vector vectorName;>
ここで、データ型はベクトルの各要素のデータの型です。すでに std 名前空間を使用している場合は、std:: を削除できます。
C++ でのベクトルの初期化
次の方法でベクトルを初期化できます。
1. リストを使用した初期化
この初期化は宣言によって行われます。ここでは、要素のリストをベクトル コンストラクターに渡して、指定された要素を含むベクトルを作成します。
vector name({ value1, value2, value3 .... });> 2. 初期化 単一の値の場合
この初期化も宣言によって行われます。ここでは、ベクトルのサイズを指定し、ベクトルのすべての要素をその値で初期化します。
vector name(size, value);>
3. 別のベクトルからの初期化
この初期化は、other_vec の正確なコピーであるベクトルを作成するために使用されます。
vector name(other_vec);>
std::vector クラスの一般的に使用されるメンバー関数の一部を以下に示します。
イテレータ
- 始める() – ベクトルの最初の要素を指す反復子を返します。
- 終わり() – ベクトルの最後の要素に続く理論上の要素を指す反復子を返します。
- rbegin() – ベクトルの最後の要素 (逆の開始) を指す逆反復子を返します。最後の要素から最初の要素に移動します
- 与える() – ベクトルの最初の要素に先行する理論上の要素を指す逆反復子を返します (逆端と見なされます)。
- cbegin() – ベクトルの最初の要素を指す定数反復子を返します。
- いくつか() – ベクトルの最後の要素に続く理論上の要素を指す定数反復子を返します。
- crbegin() – ベクトルの最後の要素 (逆の開始) を指す定数逆反復子を返します。最後の要素から最初の要素に移動します
- クレンド() – ベクトルの最初の要素に先行する理論上の要素 (逆端とみなされる) を指す定数逆反復子を返します。
C++
// C++ program to illustrate the> // iterators in vector> #include> #include> > using> namespace> std;> > int> main()> {> >vector<>int>>g1;>> > >for> (>int> i = 1; i <= 5; i++)> >g1.push_back(i);> > >cout <<>'Output of begin and end: '>;> >for> (>auto> i = g1.begin(); i != g1.end(); ++i)> >cout << *i <<>' '>;> > >cout <<>'
Output of cbegin and cend: '>;> >for> (>auto> i = g1.cbegin(); i != g1.cend(); ++i)> >cout << *i <<>' '>;> > >cout <<>'
Output of rbegin and rend: '>;> >for> (>auto> ir = g1.rbegin(); ir != g1.rend(); ++ir)> >cout << *ir <<>' '>;> > >cout <<>'
Output of crbegin and crend : '>;> >for> (>auto> ir = g1.crbegin(); ir != g1.crend(); ++ir)> >cout << *ir <<>' '>;> > >return> 0;> }> |
>
>出力
Output of begin and end: 1 2 3 4 5 Output of cbegin and cend: 1 2 3 4 5 Output of rbegin and rend: 5 4 3 2 1 Output of crbegin and crend : 5 4 3 2 1>
容量
- サイズ() – ベクトル内の要素の数を返します。
- max_size() – ベクトルが保持できる要素の最大数を返します。
- 容量() – 現在ベクトルに割り当てられている記憶領域のサイズを要素数として返します。
- サイズ変更(n) – 「n」個の要素が含まれるようにコンテナのサイズを変更します。
- 空の() – コンテナが空かどうかを返します。
- シュリンクトゥフィット() – コンテナの容量をそのサイズに合わせて減らし、容量を超えたすべての要素を破棄します。
- 予約する() – ベクトル容量が少なくとも n 個の要素を含むのに十分であることを要求します。
C++
// C++ program to illustrate the> // capacity function in vector> #include> #include> > using> namespace> std;> > int> main()> {> >vector<>int>>g1;>> > >for> (>int> i = 1; i <= 5; i++)> >g1.push_back(i);> > >cout <<>'Size : '> << g1.size();> >cout <<>'
Capacity : '> << g1.capacity();> >cout <<>'
Max_Size : '> << g1.max_size();> > >// resizes the vector size to 4> >g1.resize(4);> > >// prints the vector size after resize()> >cout <<>'
Size : '> << g1.size();> > >// checks if the vector is empty or not> >if> (g1.empty() ==>false>)> >cout <<>'
Vector is not empty'>;> >else> >cout <<>'
Vector is empty'>;> > >// Shrinks the vector> >g1.shrink_to_fit();> >cout <<>'
Vector elements are: '>;> >for> (>auto> it = g1.begin(); it != g1.end(); it++)> >cout << *it <<>' '>;> > >return> 0;> }> |
>
アナコンダ vs パイソンヘビ
>出力
Size : 5 Capacity : 8 Max_Size : 4611686018427387903 Size : 4 Vector is not empty Vector elements are: 1 2 3 4>
要素へのアクセス
- 参照演算子 [g] – ベクトル内の位置「g」にある要素への参照を返します。
- at(g) – ベクトル内の位置「g」にある要素への参照を返します。
- フロント() – ベクトルの最初の要素への参照を返します。
- 戻る() – ベクトル内の最後の要素への参照を返します。
- C++
// C++ program to illustrate the>// element access in vector>#include>using>namespace>std;>>int>main()>{>>vector<>int>>g1;>>>>for>(>int>i = 1; i <= 10; i++)>>g1.push_back(i * 10);>>>cout <<>' Reference operator [g] : g1[2] = '><< g1[2];>>>cout <<>' at : g1.at(4) = '><< g1.at(4);>>>cout <<>' front() : g1.front() = '><< g1.front();>>>cout <<>' back() : g1.back() = '><< g1.back();>>>// pointer to the first element>>int>* pos = g1.data();>>>cout <<>' The first element is '><< *pos;>>return>0;>}>java intをdoubleにする
>
>
出力Reference operator [g] : g1[2] = 30 at : g1.at(4) = 50 front() : g1.front() = 10 back() : g1.back() = 100 The first element is 10>
修飾子
- 割当() – 古い値を置き換えることにより、ベクトル要素に新しい値を割り当てます
- プッシュバック() – 要素を後ろからベクトルにプッシュします
- ポップバック() – ベクターの要素を後方からポップまたは削除するために使用されます。
- 入れる() – 指定された位置の要素の前に新しい要素を挿入します
- 消去() – コンテナーの指定された位置または範囲から要素を削除するために使用されます。
- スワップ() – あるベクトルの内容を同じタイプの別のベクトルと交換するために使用されます。サイズは異なる場合があります。
- クリア() – ベクターコンテナのすべての要素を削除するために使用されます
- 埋め込む() – 位置に新しい要素を挿入してコンテナを拡張します
- emplace_back() – 新しい要素をベクター コンテナに挿入するために使用され、新しい要素はベクターの末尾に追加されます
C++
// C++ program to illustrate the>// Modifiers in vector>#include>#include>using>namespace>std;>>int>main()>{>>// Assign vector>>vector<>int>>で;>>>>// fill the vector with 10 five times>>v.assign(5, 10);>>>cout <<>'The vector elements are: '>;>>for>(>int>i = 0; i cout << v[i] << ' '; // inserts 15 to the last position v.push_back(15); int n = v.size(); cout << ' The last element is: ' << v[n - 1]; // removes last element v.pop_back(); // prints the vector cout << ' The vector elements are: '; for (int i = 0; i cout << v[i] << ' '; // inserts 5 at the beginning v.insert(v.begin(), 5); cout << ' The first element is: ' << v[0]; // removes the first element v.erase(v.begin()); cout << ' The first element is: ' << v[0]; // inserts at the beginning v.emplace(v.begin(), 5); cout << ' The first element is: ' << v[0]; // Inserts 20 at the end v.emplace_back(20); n = v.size(); cout << ' The last element is: ' << v[n - 1]; // erases the vector v.clear(); cout << ' Vector size after clear(): ' << v.size(); // two vector to perform swap vectorv1、v2; v1.push_back(1); v1.push_back(2); v2.push_back(3); v2.push_back(4); コート<< ' Vector 1: '; for (int i = 0; i cout << v1[i] << ' '; cout << ' Vector 2: '; for (int i = 0; i cout << v2[i] << ' '; // Swaps v1 and v2 v1.swap(v2); cout << ' After Swap Vector 1: '; for (int i = 0; i cout << v1[i] << ' '; cout << ' Vector 2: '; for (int i = 0; i cout << v2[i] << ' '; }> >
>
出力The vector elements are: 10 10 10 10 10 The last element is: 15 The vector elements are: 10 10 10 10 10 The first element is: 5 The first element is: 10 The first element is: 5 The last element is: 20 Vector size after erase(): 0 Vector 1: 1 2 Vector 2: 3 4 After Swap Vector 1: 3 4 Vector 2: 1 2>
ベクトルに対してさまざまな操作を行う場合の時間計算量は次のとおりです。
- ランダムアクセス - 定数 O(1)
- 末尾の要素の挿入または削除 - 定数 O(1)
- 要素の挿入または削除 - ベクトルの終わりまでの距離に直線 O(N)
- サイズを知る – 定数 O(1)
- ベクトルのサイズ変更 - Linear O(N)
std::vector のすべてのメンバー関数
以下は、C++ の std::vector クラスのすべてのメンバー関数のリストです。
ベクトル関数
説明
ベクトルの末尾に要素を追加します。 ベクトルの最後の要素を削除します。 ベクトル内の要素の数を返します。 max_size()
ベクトルが保持できる要素の最大数を返します。 サイズ変更()
ベクトルのサイズを変更します。 ベクトルが空かどうかを確認します。 オペレーター[]
特定の位置にある要素にアクセスします。 で()
f映画インド
境界チェックを使用して、特定の位置にある要素にアクセスします。 フロント()
ベクトルの最初の要素にアクセスします。 戻る()
ベクトルの最後の要素にアクセスします。 始める()
ベクトルの最初の要素を指す反復子を返します。 終わり()
ベクトルの最後の要素を指す反復子を返します。 rbegin()
ベクトルの最後の要素を指す逆反復子を返します。 与える()
ベクトルの最初の要素に先行する要素を指す逆反復子を返します。 始める
const_iterator を先頭に戻します いくつかの
const_iterator を最後まで返します 始めます
const_reverse_iterator を返して開始を逆にします 信じる
const_reverse_iterator を逆端に返します ベクトル内の特定の位置に要素を挿入します。 ベクトル内の特定の位置または範囲から要素を削除します。 スワップ()
ベクトルの内容を別のベクトルの内容と交換します。 ベクトルからすべての要素を削除します。 埋め込む()
要素を構築してベクターに挿入します。 emplace_back()
要素を構築し、ベクトルの末尾に挿入します。 割当()
古い値を置き換えることにより、新しい値をベクトル要素に割り当てます。 容量()
現在ベクトルに割り当てられている記憶域のサイズを返します。 予約する()
ベクトル容量が、少なくとも指定された数の要素を含むのに十分であることを要求します。 シュリンクトゥフィット()
未使用の領域を解放することでメモリ使用量を削減します。 get_allocator
ベクトルに関連付けられたアロケーター オブジェクトのコピーを返します。 必読:
- C++ でベクトルに指定された要素が含まれているかどうかを確認するには?
- C++ で Vector 内の特定の要素のインデックスを見つける方法
- C++ でのベクトルのソート