vector library c++
The vector library in C++ provides a dynamic array-like structure that can store elements of any data type. It is part of the Standard Template Library (STL) and offers various functions and methods to manipulate and access the elements stored in the vector.
To use the vector library in C++, you need to include the
Once you have included the
vector
You can also initialize a vector with values using initializer lists. For example:
vector
To add elements to the vector, you can use the push_back() function. This function appends an element to the end of the vector. For example:
myVector.push_back(6);
To access elements in the vector, you can use the indexing operator ([]). The index starts from 0. For example:
int element = myVector[0];
To get the size of the vector, you can use the size() function. For example:
int size = myVector.size();
To iterate over the elements of the vector, you can use a for loop. For example:
for(int i = 0; i < myVector.size(); i++) { cout << myVector[i] << " "; }
To remove elements from the vector, you can use the erase() function. This function takes an iterator as an argument and removes the element at that position. For example:
myVector.erase(myVector.begin() + 2);
These are just a few basic operations you can perform with the vector library in C++. There are many more functions and methods available that allow you to manipulate and work with vectors in different ways. It is recommended to refer to the C++ documentation for a comprehensive list of functions and methods provided by the vector library.