Vectors in C++: An Introduction

Vectors are a common data structure used in C++, particularly in the Standard Template Library (STL). They are dynamic arrays that can grow or shrink in size as needed, which makes them an efficient and flexible option for storing and manipulating collections of data.

A vector is an object of the class std::vector, which is defined in the <vector> header file. To create a vector, we need to specify the type of data it will store, such as integers, strings, or custom objects. For example, here is how we can create a vector of integers:

#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers;
  return 0;
}

One of the main advantages of using vectors is that they can be easily resized at any time. We can use the push_back method to add elements to the end of a vector, or the resize method to change its size:

#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers;
  numbers.push_back(1);
  numbers.push_back(2);
  numbers.push_back(3);

  std::cout << "The size of the vector is: " << numbers.size() << std::endl;

  numbers.resize(5);
  std::cout << "The size of the vector is now: " << numbers.size() << std::endl;

  return 0;
}

Another useful feature of vectors is that they provide random access to their elements. This means that we can access any element in the vector using the square bracket notation, just like with arrays. For example:

#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers;
  numbers.push_back(1);
  numbers.push_back(2);
  numbers.push_back(3);

  std::cout << "The first element of the vector is: " << numbers[0] << std::endl;
  std::cout << "The last element of the vector is: " << numbers[numbers.size() - 1] << std::endl;

  return 0;
}

There are many other methods and properties available in the std::vector class, such as begin, end, clear, and empty, that provide even more functionality and make vectors a powerful tool for working with collections of data in C++.

Here are examples of using some of the most commonly used methods of the std::vector class in C++:

  1. push_back: Adds an element to the end of the vector.
#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers;
  numbers.push_back(1);
  numbers.push_back(2);
  numbers.push_back(3);

  std::cout << "The elements in the vector are: ";
  for (int i : numbers) {
    std::cout << i << " ";
  }
  std::cout << std::endl;

  return 0;
}

Output:

The elements in the vector are: 1 2 3

emplace_back can also be used instead of push_back.

  1. pop_back: Removes the last element from the vector.
#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers;
  numbers.push_back(1);
  numbers.push_back(2);
  numbers.push_back(3);

  std::cout << "The elements in the vector are: ";
  for (int i : numbers) {
    std::cout << i << " ";
  }
  std::cout << std::endl;

  numbers.pop_back();

  std::cout << "The elements in the vector after pop_back are: ";
  for (int i : numbers) {
    std::cout << i << " ";
  }
  std::cout << std::endl;

  return 0;
}

Output:

The elements in the vector are: 1 2 3 The elements in the vector after pop_back are: 1 2
  1. size: Returns the number of elements in the vector.
#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers;
  numbers.push_back(1);
  numbers.push_back(2);
  numbers.push_back(3);

  std::cout << "The size of the vector is: " << numbers.size() << std::endl;

  return 0;
}

Output:

The size of the vector is: 3
  1. empty: Returns true if the vector is empty, and false otherwise.
#include <vector>
#include <iostream>

int main() {
  std::vector<int> numbers;

  if (numbers.empty()) {
    std::cout << "The vector is empty." << std::endl;
  } else {
    std::cout << "The vector is not empty." << std::endl;
  }

  numbers.push_back(1);

  if (numbers.empty()) {
    std::cout << "The vector is empty." << std::endl;
  } else {
    std::cout << "The vector is not empty." << std::endl;
  }

  return 0;
}

In conclusion, vectors in C++ are a versatile and efficient data structure for storing and manipulating collections of data. Whether you are working with simple types like integers or strings, or more complex objects, vectors are a valuable tool to have in your arsenal.