C++ Vector

   C++ vector is a container template available with Standard Template Library pack. This C++ vector can be used to store similar typed objects sequentially, which can be accessed at a later point of time. As this is a template, all types of data including user defined data types like struct and class can be stored in this container.

   This article explains briefly about how to insert, delete, access the data with respect to the C++ vector. The type stl::string is used for the purposes of explaining the sample code. Using stl::string is only for sample purposes. Any other type can be used with the C++ vector.

Inserting a stl::string into the C++ Vector:

   The values can be added to the c++ vector at the end of the sequence. The function push_back should be used for this purpose. The <vector> header file should be included in all the header files in order to access the C++ vector and its functions.


#include <vector>
#include <string>
#include <iostream.h>

void main()
{

//Declaration for the string data
std::string strData = “One”;
//Declaration for C++ vector
std:: vector <std::string> str_Vector;
str_Vector.push_back(strData);
strData = “Two”;
str_Vector.push_back(strData);
strData = “Three”;
str_Vector.push_back(strData);
strData = “Four”;
str_Vector.push_back(strData);

}


   The above code adds 4 strings of std::string type to the str_Vector. This uses std:: vector.push_back function. This function takes 1 parameter of the designated type and adds it to the end of the c++ vector.

Accessing Elements of C++ Vector:

   The elements after being added can be accessed in two ways. One way is our legacy way of accessing the data with vector.at(position_in_integer) function. The position of the data element is passed as the single parameter to access the data.

Using our normal logic for accessing elements stored in C++ Vector:

   If we want to access all the data, we can use a for loop and display them as follows.


for(int i=0;i < str_Vector.size(); i++)
{
std::string strd = str_Vector.at(i);
cout<<strd.c_str()<<endl;
}


   The std:: vector .size() function returns the number of elements stored in the vector.

Using C++ vector iterator provided by STL:

   The next way is to use the iterator object provided by the STL. These iterators are general purpose pointers allowing c++ programmers to forget the intricacies of object types and access the data of any type.


std::vector<std::string>::iterator itVectorData;
for(itVectorData = str_Vector.begin(); itVectorData != str_Vector.end(); itVectorData++)
{
std::string strD = *(itVectorData);
}


Removing elements from C++ vector:

   Removing elements one by one from specified positions in c++ vector is achieved with the erase function.

   The following code demonstrates how to use the erase function to remove an element from position 2 in the str_Vector from our sample.

str_Vector.erase(str_Vector.begin()+1,str_Vector.begin()+2);

   The following sample demonstrates how to use the erase() function for removing elements 2,3 in the str_Vector used in the above sample.

str_Vector.erase(str_Vector.begin()+1,str_Vector.begin()+3);

   If one wants to remove all the elements at once from the c++ vector, the vector.clear() function can be used.