In Java, I always used arrayLists. It’s easy to use, it has dynamic size changes, and also have the convenience as arrays like using indexes to directly access the elements, and so on.

So C++ is something like arraylists in Java,

To use vector, we have to do #include <vector> because it’s a library from std. There are several use cases of vectors.. for example,

code from geeksforgeek

	// C++ progra to illustrate the 
	// iterators in vector 
	#include <iostream> 
	#include <vector> 
	  
	using namespace std; 
	  
	int main() 
	{ 
	    vector<int> g1; 
	  
	    for (int i = 1; i <= 5; i++) 
	        g1.push_back(i); 
	  
	    cout << "Output of begin and end: "; 
	    for (auto i = g1.begin(); i != g1.end(); ++i) 
	        cout << *i << " "; 
	  
	    cout << "\nOutput of cbegin and cend: "; 
	    for (auto i = g1.cbegin(); i != g1.cend(); ++i) 
	        cout << *i << " "; 
	  
	    cout << "\nOutput of rbegin and rend: "; 
	    for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) 
	        cout << *ir << " "; 
	  
	    cout << "\nOutput of crbegin and crend : "; 
	    for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir) 
	        cout << *ir << " "; 
	  
	    return 0; 
	} 

explanations of some syntaxs are..

  1. begin() - returns an iterator pointing to the first element in the vector
  2. end() - Returns an iterator pointing to the theoretical element that follows the last element in the vector
  3. rbegin() - Returns a reverse iterator pointing to the last element in the vector(reverse beginning). It moves from last to first element
  4. rend() - Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
  5. cbegin() - Returns a constant iterator pointing to the first element in the vector
  6. cend() - Returns a constant iterator pointing

Capacity

  1. size() – Returns the number of elements in the vector.
  2. max_size() – Returns the maximum number of elements that the vector can hold.
  3. capacity() – Returns the size of the storage space currently allocated to the vector expressed as number of elements.
  4. resize() – Resizes the container so that it contains ‘g’ elements.
  5. empty() – Returns whether the container is empty.
  6. shrink_to_fit() – Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.
  7. reserve() – Requests that the vector capacity be at least enough to contain n elements.

examples at geeksforgeek https://www.geeksforgeeks.org/vector-in-cpp-stl/

Element access

  1. reference operator [g] – Returns a reference to the element at position ‘g’ in the vector
  2. at(g) – Returns a reference to the element at position ‘g’ in the vector
  3. front() – Returns a reference to the first element in the vector
  4. back() – Returns a reference to the last element in the vector
  5. data() – Returns a direct pointer to the memory array used internally by the vector to store its owned elements.

Modifiers

  1. assign() – It assigns new value to the vector elements by replacing old ones
  2. push_back() – It push the elements into a vector from the back
  3. pop_back() – It is used to pop or remove elements from a vector from the back.
  4. insert() – It inserts new elements before the element at the specified position
  5. erase() – It is used to remove elements from a container from the specified position or range.
  6. swap() – It is used to swap the contents of one vector with another vector of same type and size.
  7. clear() – It is used to remove all the elements of the vector container
  8. emplace() – It extends the container by inserting new element at position
  9. emplace_back() – It is used to insert a new element into the vector container, the new element is added to the end of the vector