for vector c++

To declare a vector in C++, follow these steps:

  1. Include the necessary header file for vectors:
#include <vector>
  1. Declare a vector of a specific data type:
std::vector<int> myVector;
  1. Initialize the vector with a specific size and default value:
std::vector<int> myVector(5, 0);
  1. Add elements to the vector using push_back:
myVector.push_back(10);
  1. Access elements using the index operator:
int value = myVector[0];
  1. Iterate through the vector using iterators:
for (auto it = myVector.begin(); it != myVector.end(); ++it) {
    // Access *it
}
  1. Get the size of the vector using the size method:
int size = myVector.size();
  1. Remove elements from the vector using pop_back:
myVector.pop_back();
  1. Clear all elements from the vector:
myVector.clear();