C++ STL vector_bool, swap() code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ swap() to exchange the elements of two vectors with Boolean elements in C++ programming

To show: How to use the C++ vector_bool, swap() to exchange the elements of two vectors with Boolean elements in C++ programming

 

// C++ STL vector_bool, swap()

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

// bool vector

_Bvector vec1, vec2;

 

// push/insert data

vec1.push_back(0);

vec1.push_back(0);

vec2.push_back(1);

vec2.push_back(1);

 

// print the data and do some operations

cout<<"The vector vec1 is: "<<vec1.front()<<" "<<vec1.back()<<endl;

cout<<"The vector vec2 is: "<<vec2.front()<<" "<<vec2.back()<<endl;

cout<<"Operation: swap(vec1, vec2);"<<endl;

swap(vec1, vec2);

cout<<"After swapping, vec1 is: "<<vec1.front()<<" "<<vec1.back()<<endl;

cout<<"After swapping, vec2 is: "<<vec2.front()<<" "<<vec2.back()<<endl;

return 0;

}

 

Output examples:

 

The vector vec1 is: 0 0

The vector vec2 is: 1 1

Operation: swap(vec1, vec2);

After swapping, vec1 is: 1 1

After swapping, vec2 is: 0 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index