C++ Vectors – Detailed Tutorial With Examples

Vectors are dynamic arrays in C++ and are part of the Standard Template Library (STL). They are similar to arrays, but the size of a vector can be changed during runtime, making them more flexible than arrays. Vectors also provide many built-in functions to perform various operations, making them easier to use.

Creating a Vector

A vector can be created by specifying its data type and size, or by using the default constructor which creates an empty vector. Here is an example of both methods:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    // Creating a vector with size and data type
    vector<int> v1(5);

    // Creating an empty vector
    vector<float> v2;

    return 0;
}

Accessing Vector Elements

Elements in a vector can be accessed using the square bracket notation, just like an array. Here is an example:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4, 5};

    // Accessing elements using square bracket notation
    cout << "Element at index 0: " << v[0] << endl;
    cout << "Element at index 1: " << v[1] << endl;
    cout << "Element at index 2: " << v[2] << endl;

    return 0;
}

Output:

Element at index 0: 1
Element at index 1: 2
Element at index 2: 3

Adding Elements to a Vector

Elements can be added to a vector using the push_back function. Here is an example:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v;

    // Adding elements to the vector
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);

    // Printing the elements in the vector
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;

    return 0;
}

Output:

10 20 30

Removing Elements from a Vector

Elements can be removed from a vector using the pop_back function. This function removes the last element from the vector. Here is an example:

#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40, 50};

// Removing the last element
v.pop_back();

// Printing the elements in the vector
for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
}
cout << endl;

return 0;
}

Output:

10 20 30 40

Vector Methods

The Standard Template Library (STL) in C++ provides several built-in functions or methods for vectors. Some of the commonly used vector methods are:

  1. push_back(value): Adds an element to the end of the vector.
  2. pop_back(): Removes the last element of the vector.
  3. clear(): Removes all the elements of the vector, making it empty.
  4. size(): Returns the number of elements in the vector.
  5. empty(): Returns a boolean value indicating whether the vector is empty or not.
  6. resize(n): Changes the size of the vector to n. If the new size is larger than the current size, additional elements are added with default values.
  7. capacity(): Returns the maximum number of elements that can be stored in the vector before a reallocation of memory is necessary.
  8. reserve(n): Requests that the vector’s capacity be at least enough to contain n elements.
  9. at(index): Returns the element at the specified index.
  10. front(): Returns the first element of the vector.
  11. back(): Returns the last element of the vector.
  12. begin(): Returns an iterator pointing to the first element of the vector.
  13. end(): Returns an iterator pointing to the position after the last element of the vector.
  14. swap(v): Swaps the contents of the current vector with the contents of v.

Conclusion

In conclusion, vectors are a useful and flexible data structure in C++. With their ability to resize dynamically and various built-in functions, vectors make it easier to perform various operations. Whether you are a beginner or an experienced programmer, vectors are a great tool to have in your toolkit.

Leave a Reply

Your email address will not be published. Required fields are marked *