Example & Tutorial understanding programming in easy ways.

Define vector in C++

What is vector data-type in C++ ?
-A vector is a sequence container class that implements dynamic array, means size automatically changes when appending elements.
-A vector stores the elements in contiguous memory locations and allocates the memory as needed at run time.

Difference between Vector and array:
-Array is static approach while vector is dynamic approach.
-Array size cannot be changed during run time while vector memory depends on numbers of elements.

program:

#include< iostream>
#include< vector>
using namespace std;
int main()
{
vector< string> v1;
v1.push_back("Data1");
v1.push_back("Data2");
for(vector< string>::iterator itr=v1.begin();itr!=v1.end();++itr)
cout<<*itr<< endl;
return 0;
}


output-

Data1
Data2

-In this program, we include the vector heador file that defines the vector function like push_back()
-push_back() is the function which are used to insert the data in the vector.




Read More →