C++ STL vector, various operators 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++ operator!= - to test if the vector object on the left side of the operator is not equal to the vector object on the right side, operator< - to test if the vector object on the left side of the operator is less than the vector object on the right side, operator<Tests> to test if the vector object on the left side of the operator is greater than the vector object on the right side and operator>= - to test if the vector object on the left side of the operator is greater than or equal to the vector object on the right side in C++ programming

To show: How to use the C++ operator!= - to test if the vector object on the left side of the operator is not equal to the vector object on the right side, operator< - to test if the vector object on the left side of the operator is less than the vector object on the right side, operator<Tests> to test if the vector object on the left side of the operator is greater than the vector object on the right side and operator>= - to test if the vector object on the left side of the operator is greater than or equal to the vector object on the right side in C++ programming

 

// C++ STL vector, operators

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

unsigned int i;

// vector container for integer elements

vector<int> vec1, vec2, vec3;

 

// print vector data

cout<<"vec1 vector data: ";

 

// append elements with values 1 to 10

for(i=1; i<=10; ++i)

vec1.push_back(i);

 

// print all elements separated by a space

for(i=0; i<vec1.size(); ++i)

cout<<vec1[i]<<' ';

cout<<endl;

 

cout<<"vec2 vector data: ";

// append elements with values 1 to 10

for(i=11; i<=20; ++i)

vec2.push_back(i);

 

// print all elements separated by a space

for(i=0; i<vec2.size(); ++i)

cout<<vec2[i]<<' ';

cout<<endl;

 

cout<<"vec3 vector data: ";

// append elements with values 1 to 10

for(i=1; i<=10; ++i)

vec3.push_back(i);

 

// print all elements separated by a space

for(i=0; i<vec3.size(); ++i)

cout<<vec3[i]<<' ';

cout<<endl<<endl;

 

// more operations

cout<<"Operation: vec1 != vec2;"<<endl;

if(vec1 != vec2)

cout<<"vec1 vector and vec2 vector is not equal."<<endl;

else

cout<<"vec1 vector and vec2 vector is equal."<<endl;

cout<<"\nOperation: vec1 == vec3;"<<endl;

if(vec1 == vec3)

cout<<"vec1 vector and vec3 vector is equal."<<endl;

else

cout<<"vec1 vector and vec3 vector is not equal."<<endl;

cout<<"\nOperation: vec1 < vec2;"<<endl;

if(vec1 < vec2)

cout<<"vec1 vector less than vec2 vector."<<endl;

else

cout<<"vec1 vector is not less than vec2 vector."<<endl;

cout<<"\nOperation: vec2 > vec1;"<<endl;

if(vec2 > vec1)

cout<<"vec2 vector greater than vec1 vector."<<endl;

else

cout<<"vec2 vector is not greater than vec1 vector."<<endl;

cout<<"\nOperation: vec2 >= vec1;"<<endl;

if(vec2 >= vec1)

cout<<"vec2 vector greater or equal than vec1 vector."<<endl;

else

cout<<"vec2 vector is not greater or equal than vec1 vector."<<endl;

cout<<"\nOperation: vec1 <= vec2;"<<endl;

if(vec1 <= vec2)

cout<<"vec1 vector less or equal than vec2 vector."<<endl;

else

cout<<"vec1 vector is not less or equal than vec2 vector."<<endl;

return 0;

}

 

Output examples:

 

vec1 vector data: 1 2 3 4 5 6 7 8 9 10

vec2 vector data: 11 12 13 14 15 16 17 18 19 20

vec3 vector data: 1 2 3 4 5 6 7 8 9 10

Operation: vec1 != vec2;

vec1 vector and vec2 vector is not equal.

Operation: vec1 == vec3;

vec1 vector and vec3 vector is equal.

Operation: vec1 < vec2;

vec1 vector less than vec2 vector.

Operation: vec2 > vec1;

vec2 vector greater than vec1 vector.

Operation: vec2 >= vec1;

vec2 vector greater or equal than vec1 vector.

Operation: vec1 <= vec2;

vec1 vector less or equal than vec2 vector.

Press any key to continue . . .

 

 

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