vector methods in c++ code example
Example 1: how to create a vector in c++
#include <vector>
using namespace std;
int main()
{
vector<int> vect;
vect.push_back(10);
for (int x : vect)
cout << x << " ";
return 0;
}
Example 2: how to use vectors c++
#include <iostream>
#include <vector>
using namespace std;
int main() {
const int size = 4;
vector<int> myVect (size, 4);
for (int i=0; i<size; i++) {
cout << "Vector index(" << i <<") is: "<< myVect[i] << endl;
}
return 0;
}
Example 3: vector functions c++
Iterators
-----------------------------------------
begin | Returns an iterator to the beginning
end | Returns an iterator to the end
Capacity
-----------------------------------------
empty | Checks whether the container is empty
size | Returns the number of elements
reserve | Reserves storage
capacity | Returns the number of elements that can be held in currently allocated storage
Element access
-----------------------------------------
at | Access specified element with bounds checking
front | Access the first element
back | Access the last element
operator[] | Access specified element
Modifiers
-----------------------------------------
clear | Clears the contents
insert | Inserts elements
emplace | Constructs element in-place
erase | Erases elements
push_back | Adds an element to the end
emplace_back | Constructs an element in-place at the end
pop_back | Removes the last element
resize | Changes the number of elements stored
swap | Swaps the contents
*Notes*
- https:
- https:
- https: