A very simple C++ vector container program 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: Defining and creating the vector container in C++ programming

To show: How to create a vector container in C++ programming

 

// C++ STL vector example

#include <iostream>

// vector header file

#include <vector>

using namespace std;

 

int main(void)

{

// vector container for integer elements declaration

vector<int> coll;

// using the for loop as the iterator

unsigned int i;

 

// append elements with values 1 to 10

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

coll.push_back(i);

 

// print all elements separated by a space

cout<<"coll vector data: ";

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

cout<<coll[i]<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

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

Press any key to continue . . .

 

 

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