C++ STL vector, constructors 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++ vector to construct a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other vector in C++ programming

To show: How to use the C++ vector to construct a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other vector in C++ programming

 

// C++ STL vector constructors

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

// vector iterators

vector <int>::iterator vec0Iter, vec1Iter, vec2Iter, vec3Iter, vec4Iter, vec5Iter;

 

// create an empty vector vec0

vector <int> vec0;

// create a vector vec1 with 10 elements of default value 0

vector <int> vec1(10);

// create a vector vec2 with 7 elements of value 13

vector <int> vec2(7, 13);

// create a vector vec3 with 5 elements of value 3 and with the allocator of vector vec2

vector <int> vec3(5, 3, vec2.get_allocator());

// vector vec4, a copy of vector vec2

vector <int> vec4(vec2);

// create a vector vec5 by copying the range of vec4[_First, _Last)

vector <int> vec5(vec4.begin() + 1, vec4.begin() + 3);

 

// print the data and  do some operations

cout<<"Operation: vector <int> vec0;"<<endl;

cout<<"vec0 vector data: ";

for(vec0Iter = vec0.begin(); vec0Iter != vec0.end(); vec0Iter++)

cout<<" "<<*vec0Iter;

cout<<endl;

cout<<"\nOperation: vector <int> vec1(10);"<<endl;

cout<<"vec1 vector data: ";

for(vec1Iter = vec1.begin(); vec1Iter != vec1.end(); vec1Iter++)

cout<<" "<<*vec1Iter;

cout<<endl;

cout<<"\nOperation: vector <int> vec2(7, 13);"<<endl;

cout<<"vec2 vector data: ";

for(vec2Iter = vec2.begin(); vec2Iter != vec2.end(); vec2Iter++)

cout<<" "<<*vec2Iter;

cout<<endl;

cout<<"\nOperation: vector <int> vec3(5, 3, vec2.get_allocator());"<<endl;

cout<<"vec3 vector data: ";

for(vec3Iter = vec3.begin(); vec3Iter != vec3.end(); vec3Iter++)

cout<<" "<<*vec3Iter;

cout<<endl;

cout<<"\nOperation: vector <int> vec4(vec2);"<<endl;

cout<<"vec4 vector data: ";

for(vec4Iter = vec4.begin(); vec4Iter != vec4.end(); vec4Iter++)

cout<<" "<<*vec4Iter;

cout<<endl;

cout<<"\nOperation: vector <int> vec5(vec4.begin()+1, vec4.begin()+3);"<<endl;

cout<<"vec5 vector data: ";

for(vec5Iter = vec5.begin(); vec5Iter != vec5.end(); vec5Iter++)

cout<<" "<<*vec5Iter;

cout<<endl;

return 0;

}

 

Output examples:

 

Operation: vector <int> vec0;

vec0 vector data:

Operation: vector <int> vec1(10);

vec1 vector data: 0 0 0 0 0 0 0 0 0 0

Operation: vector <int> vec2(7, 13);

vec2 vector data: 13 13 13 13 13 13 13

Operation: vector <int> vec3(5, 3, vec2.get_allocator());

vec3 vector data: 3 3 3 3 3

Operation: vector <int> vec4(vec2);

vec4 vector data: 13 13 13 13 13 13 13

Operation: vector <int> vec5(vec4.begin()+1, vec4.begin()+3);

vec5 vector data: 13 13

Press any key to continue . . .

 

 

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