vector<bool> クラス
vector<bool> クラスは、bool. 型の要素の vector の部分的特殊化です。 このクラスには、特殊化によって使用される基になる型のアロケーターがあり、ビットごとに 1 つの bool 値を格納することによって領域を最適化します。
構文
template <class Allocator = allocator<bool>>
class vector<bool, Allocator>
注釈
このクラス テンプレートの特殊化は vector と同様に動作しますが、この記事で説明する違いがあります。
bool 型を処理する操作は、コンテナーのストレージの値に対応します。 allocator_traits::construct はこれらの値の構築には使用されません。
Typedefs
| 型名 | 説明 |
|---|---|
const_pointer |
const_iterator のブール型要素への定数ポインターとして使用できる vector<bool> への typedef。 |
const_reference |
bool の typedef。 初期化後に、元の値への更新を確認しません。 |
pointer |
iterator のブール型要素へのポインターとして使用できる vector<bool> への typedef。 |
メンバー関数
| メンバー関数 | 説明 |
|---|---|
flip |
vector<bool> 内のすべてのビットを反転させます。 |
swap |
2 つの vector<bool> の要素を交換します。 |
operator[] |
指定した位置における vector<bool> 要素へのシミュレートされた参照を返します。 |
at |
vector::at 関数と同様に機能します。ただし、プロキシ クラス vector<bool>::reference を使用します。 「operator[]」も参照してください。 |
front |
vector::front 関数と同様に機能します。ただし、プロキシ クラス vector<bool>::reference を使用します。 「operator[]」も参照してください。 |
back |
vector::back 関数と同様に機能します。ただし、プロキシ クラス vector<bool>::reference を使用します。 「operator[]」も参照してください。 |
プロキシ クラス
| Name | 説明 |
|---|---|
vector<bool>::reference クラス |
bool& の動作をシミュレートするためのプロキシとして機能するクラスで、そのオブジェクトは vector<bool> オブジェクト内の要素 (単一ビット) への参照を提供できます。 |
要件
ヘッダー: <vector>
名前空間:
vector<bool>::const_pointer
vector<bool> オブジェクトに格納されるシーケンスのブール要素への定数ポインターとして使用できるオブジェクトを表す型。
typedef const_iterator const_pointer;
vector<bool>::const_reference
vector<bool> オブジェクトに格納されるシーケンスのブール要素への定数参照として使用できるオブジェクトを表す型。
typedef bool const_reference;
注釈
詳細およびコード例については、「vector<bool>::reference::operator=」を参照してください。
vector<bool>::flip
vector<bool> 内のすべてのビットを反転させます。
void flip();
例
// vector_bool_flip.cpp
// compile with: /EHsc /W4
#include <vector>
#include <iostream>
int main()
{
using namespace std;
cout << boolalpha; // format output for subsequent code
vector<bool> vb = { true, false, false, true, true };
cout << "The vector is:" << endl << " ";
for (const auto& b : vb) {
cout << b << " ";
}
cout << endl;
vb.flip();
cout << "The flipped vector is:" << endl << " ";
for (const auto& b : vb) {
cout << b << " ";
}
cout << endl;
}
vector<bool>::operator[]
指定した位置における vector<bool> 要素へのシミュレートされた参照を返します。
vector<bool>::reference operator[](size_type Pos);
vector&<bool&>::const_reference operator[](size_type Pos) const;
パラメーター
Pos
vector<bool> 要素の位置。
戻り値
インデックス付けされた要素の値を格納する vector<bool>::reference または vector<bool>::const_reference オブジェクト。
指定された位置がコンテナーのサイズ以上の場合、結果は未定義になります。
注釈
_ITERATOR_DEBUG_LEVEL を設定してコンパイルした場合、ベクターの境界の外にある要素にアクセスしようとすると、実行時エラーが発生します。 詳細については、「チェックを行う反復子」を参照してください。
例
このコード例には、vector<bool>::operator[] の正しい使用法と、2 つの一般的なコーディング ミスが示されており、これらはコメント アウトされています。これらの間違いにより、vector<bool>::operator[] が返す vector<bool>::reference オブジェクトのアドレスを取得できないため、エラーが発生します。
// cl.exe /EHsc /nologo /W4 /MTd
#include <vector>
#include <iostream>
int main()
{
using namespace std;
cout << boolalpha;
vector<bool> vb;
vb.push_back(true);
vb.push_back(false);
// bool* pb = &vb[1]; // conversion error - do not use
// bool& refb = vb[1]; // conversion error - do not use
bool hold = vb[1];
cout << "The second element of vb is " << vb[1] << endl;
cout << "The held value from the second element of vb is " << hold << endl;
// Note this doesn't modify hold.
vb[1] = true;
cout << "The second element of vb is " << vb[1] << endl;
cout << "The held value from the second element of vb is " << hold << endl;
}
vector<bool>::pointer
vector<bool> オブジェクトに格納されるシーケンスのブール要素へのポインターとして使用できるオブジェクトを表す型。
typedef iterator pointer;
vector<bool>::reference クラス
vector<bool>::reference クラスは bool& をシミュレートするために vector<bool> クラスによって提供されるプロキシ クラスです。
注釈
C++ では、ネイティブにビットを直接参照しないため、シミュレートされた参照が必要です。 vector<bool> は、要素ごとに 1 ビットだけ使用します。このビットは、このプロキシ クラスを使用して参照できます。 ただし、参照のシミュレーションは、特定の代入が有効でないため、完全ではありません。 たとえば、vector<bool>::reference オブジェクトのアドレスを取得できないため、vector<bool>::operator[] を使用する次のコードは正しくありません。
vector<bool> vb;
//...
bool* pb = &vb[1]; // conversion error - do not use
bool& refb = vb[1]; // conversion error - do not use
vector<bool>::reference::flip
参照先の vector<bool> 要素のブール値を反転します。
void flip();
例
// vector_bool_ref_flip.cpp
// compile with: /EHsc /W4
#include <vector>
#include <iostream>
int main()
{
using namespace std;
cout << boolalpha;
vector<bool> vb = { true, false, false, true, true };
cout << "The vector is: " << endl << " ";
for (const auto& b : vb) {
cout << b << " ";
}
cout << endl;
vector<bool>::reference vbref = vb.front();
vbref.flip();
cout << "The vector with first element flipped is: " << endl << " ";
for (const auto& b : vb) {
cout << b << " ";
}
cout << endl;
}
The vector is:
true false false true true
The vector with first element flipped is:
false false false true true
vector<bool>::reference::operator bool
vector<bool>::reference から bool への暗黙の変換を提供します。
operator bool() const;
戻り値
vector<bool> オブジェクトの要素のブール値。
注釈
vector<bool> オブジェクトはこの演算子では変更できません。
vector<bool>::reference::operator=
ブール値をビットに割り当てます。または参照先の要素が保持している値をビットに割り当てます。
reference& operator=(const reference& Right);
reference& operator=(bool Val);
パラメーター
Right
値がビットに割り当てられている要素の参照。
Val
ビットに割り当てられるブール値。
例
// vector_bool_ref_op_assign.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
#include <string>
using namespace std;
template <typename C> void print(const string& s, const C& c) {
cout << s;
for (const auto& e : c) {
cout << e << " ";
}
cout << endl;
}
int main()
{
cout << boolalpha;
vector<bool> vb = { true, false, false, true, true };
print("The vector is: ", vb);
// Invoke vector<bool>::reference::operator=()
vector<bool>::reference refelem1 = vb[0];
vector<bool>::reference refelem2 = vb[1];
vector<bool>::reference refelem3 = vb[2];
bool b1 = refelem1;
bool b2 = refelem2;
bool b3 = refelem3;
cout << "The original value of the 1st element stored in a bool: " << b1 << endl;
cout << "The original value of the 2nd element stored in a bool: " << b2 << endl;
cout << "The original value of the 3rd element stored in a bool: " << b3 << endl;
cout << endl;
refelem2 = refelem1;
print("The vector after assigning refelem1 to refelem2 is now: ", vb);
refelem3 = true;
print("The vector after assigning false to refelem1 is now: ", vb);
// The initial values are still stored in the bool variables and remained unchanged
cout << "The original value of the 1st element still stored in a bool: " << b1 << endl;
cout << "The original value of the 2nd element still stored in a bool: " << b2 << endl;
cout << "The original value of the 3rd element still stored in a bool: " << b3 << endl;
cout << endl;
}
The vector is: true false false true true
The original value of the 1st element stored in a bool: true
The original value of the 2nd element stored in a bool: false
The original value of the 3rd element stored in a bool: false
The vector after assigning refelem1 to refelem2 is now: true true false true true
The vector after assigning false to refelem1 is now: true true true true true
The original value of the 1st element still stored in a bool: true
The original value of the 2nd element still stored in a bool: false
The original value of the 3rd element still stored in a bool: false
vector<bool>::swap
プロキシ クラス vector<bool>::reference を使用してブール ベクター (vector<bool>) の 2 つの要素を交換する静的メンバー関数。
static void swap(
reference Left,
reference Right);
パラメーター
Left
Right 要素と交換される要素。
Right
Left 要素と交換される要素。
注釈
このオーバーロードは vector<bool> の特別なプロキシの要件をサポートします。 vector::swap には、vector<bool>::swap() の単一引数のオーバーロードと同じ機能があります。