1: //vector_f.cpp
3: #include <iostream>
4: #include <iomanip>
5: #include <vector>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program illustrates the member function flip() "
11: "\nof the specialized container type vector<bool>.";
12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
14: vector<bool> v(5);
15: cout << "\nFor a vector<bool> object of size 5 we have:";
16: cout << "\nSize = " << v.size() << " Capacity = " << v.capacity();
17: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
18: cout << "\nContents of v displayed without the boolalpha "
19: "manipulator:\n";
20: for(vector<bool>::size_type i=0; i<v.size(); i++)
21: cout << v.at(i) << " ";
22: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
23: cout << "\nContents of v displayed with the boolalpha "
24: "manipulator:\n";
25: for(vector<bool>::size_type i=0; i<v.size(); i++)
26: cout << boolalpha << v.at(i) << " ";
27: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
29: cout << "\nNow we \"flip\" all values in the vector object, "
30: "using v.flip().";
31: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
32: v.flip();
34: cout << "\nContents of v displayed with the boolalpha "
35: "manipulator:\n";
36: for(vector<bool>::size_type i=0; i<v.size(); i++)
37: cout << boolalpha << v.at(i) << " ";
38: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
40: cout << "\nFinally, we \"flip\" all values in even positions, "
41: "\nusing v[i].flip() (not the member function, note).";
42: for (vector<bool>::size_type i=0; i<v.size(); i++)
43: if (i%2 == 1) v[i].flip(); //<-Note test for even position!
44: cout << "\nContents of v displayed with the boolalpha "
45: "manipulator:\n";
46: for(vector<bool>::size_type i=0; i<v.size(); i++)
47: cout << boolalpha << v.at(i) << " ";
48: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
49: }