logo

C++ STL の Vector::empty() および Vector::size()

ベクトル 動的配列と同じで、要素が挿入または削除されると自動的にサイズを変更する機能があり、そのストレージはコンテナーによって自動的に処理されます。

ベクトル::空()

empty() 関数は、ベクター コンテナーが空かどうかを確認するために使用されます。
構文:



  vectorname  .empty() Parameters : No parameters are passed. Returns : True, if vector is empty False, Otherwise>

例:

Input : myvector = 1, 2, 3, 4, 5 myvector.empty(); Output : False Input : myvector = {} myvector.empty(); Output : True>

時間の複雑さ – 定数O(1)

エラーと例外
1. 例外スローがないことが保証されています。
2. パラメータを渡すとエラーが表示されます。



CPP






avl ツリー
// CPP program to illustrate> // Implementation of empty() function> #include> #include> using> namespace> std;> int> main()> {> >vector<>int>>myvector{};>> >if> (myvector.empty())> >{> >cout <<>'True'>;> >}> >else> {> >cout <<>'False'>;> >}> >return> 0;> }>

>

>

出力

True>

応用 :
整数のリストが与えられた場合、すべての整数の合計を求めます。

Input : 1, 5, 6, 3, 9, 2 Output : 26 Explanation - 1+5+6+3+9+2 = 26>

アルゴリズム
1. ベクトルが空かどうかを確認し、空でない場合は、0 として初期化された変数にバック要素を追加し、バック要素をポップします。
2. ベクトルが空になるまでこの手順を繰り返します。
3. 変数の最終値を出力します。

CPP




// CPP program to illustrate> // Application of empty() function> #include> #include> using> namespace> std;> int> main()> {> >int> sum = 0;> >vector<>int>>myvector{ 1, 5, 6, 3, 9, 2 };>>' while> (!myvector.empty())> >{> >sum = sum + myvector.back();> >myvector.pop_back();> >}> >cout << sum;> >return> 0;> }>

>

>

出力

26>
ベクトル::サイズ()

size() 関数は、ベクトル コンテナのサイズまたはベクトル コンテナ内の要素の数を返すために使用されます。
構文:

  vectorname  .size() Parameters : No parameters are passed. Returns : Number of elements in the container.>

例:

Input : myvector = 1, 2, 3, 4, 5 myvector.size(); Output : 5 Input : myvector = {} myvector.size(); Output : 0>

時間計算量 – 定数O(1)

エラーと例外
1. 例外スローがないことが保証されています。
2. パラメータを渡すとエラーが表示されます。

CPP




カプセル化Java
// CPP program to illustrate> // Implementation of size() function> #include> #include> using> namespace> std;> int> main()> {> >vector<>int>>myvector{ 1, 2, 3, 4, 5 };>> >cout << myvector.size();> >return> 0;> }>

>

>

出力

5>

size() よりも empty() が推奨される理由
empty() 関数は、次のような点から size() 関数よりも好ましいと言われることがよくあります。

  1. empty() 関数 比較演算子は使用しません 、したがって、より便利に使用できます
  2. empty() 関数は 一定時間で実装される 一方、size() 関数の一部の実装では、list::size() などの O(n) 時間の複雑さが必要です。

応用 :
整数のリストが与えられた場合、すべての整数の合計を求めます。

Input : 1, 5, 6, 3, 9, 2 Output : 26 Explanation - 1+5+6+3+9+2 = 26>

アルゴリズム
1. ベクトルのサイズが 0 であるかどうかを確認し、そうでない場合は 0 として初期化された変数にバック要素を追加し、バック要素をポップします。
2. ベクトルのサイズが 0 になるまでこの手順を繰り返します。
3. 変数の最終値を出力します。

CPP




// CPP program to illustrate> // Application of size() function> #include> #include> using> namespace> std;> int> main()> {> >int> sum = 0;> >vector<>int>>myvector{ 1, 5, 6, 3, 9, 2 };>> >while> (myvector.size()>0) {>> >sum = sum + myvector.back();> >myvector.pop_back();> >}> >cout << sum;> >return> 0;> }>

>

>

出力

Java例外処理
26>

size() を使用するときは注意が必要です。

たとえば、次のプログラムを考えてみましょう。

C++




#include> using> namespace> std;> int> main()> {> >// Initializing a vector of string type> >vector vec = {>'Geeks'>,>'For'>,>'Geeks'> };> >for> (>int> i = 0 ; i <= vec.size() - 1 ; i++)> >cout << vec[i] <<>' '>;> >return> 0;> }>

>

>

出力

Geeks For Geeks>

上記のプログラムは問題なく動作しますが、次のプログラムを考えてみましょう。

C++




CSSで太字にする
#include> using> namespace> std;> int> main()> {> >// Initializing a vector of string type> >vector vec = {>'Geeks'>,>'For'>,>'Geeks'> };> >vec.clear();> >for> (>int> i = 0; i <= vec.size() - 1; i++)> >cout << vec[i] <<>' '>;> >cout <<>'Geeks For Geeks'>;> >return> 0;> }>

>

>

出力:

セグメンテーション違反 SIGEGV

上記のプログラムをコンパイルすると、size() の戻り値の型が unsigned long int のエイリアスである size_t であるため、セグメンテーション フォールト (SIGSEGV) が発生します。-> unsigned long int var = 0;-> coutcout << vec.size() – 1; // これは 18446744073709551615 にも等しくなります

したがって、上記のプログラムでは i = 0 から i = 18446744073709551615 までループしています。

次に、初期化されたコンテナから要素を削除し、一連の操作の後にコンテナが空になり、最後に上記のメソッドを使用してコンテナの内容を出力するシナリオを考えてみましょう。間違いなく、セグメンテーション違反 (SIGSEGV) が発生します。

それを修正するにはどうすればよいですか?

セグメンテーション違反 (SIGSEGV) を回避するには、container.size() を整数型に型キャストすることをお勧めします。

C++


Javaのwhileループ



#include> using> namespace> std;> int> main()> {> >// Initializing a vector of string type> >vector vec = {>'Geeks'>,>'For'>,>'Geeks'> };> > >// Clearing the vector> >// Now size is equal to 0> >vec.clear();> > >// Typecasting vec.size() to int> >for> (>int> i = 0; i <(>int>)vec.size() - 1; i++)> >cout << vec[i] <<>' '>;> >cout <<>'Geeks For Geeks'>;> >return> 0;> }> // This code is contributed by Bhuwanesh Nainwal>

>

>

出力

Geeks For Geeks>