Vector is not a template

In C++, a vector is not a template. It is actually a container class provided by the Standard Template Library (STL). The vector class represents a dynamic array, which can grow or shrink in size at runtime.

The vector class is defined in the “vector” header file and is part of the std namespace. It can store elements of any data type, be it integers, floats, characters, or even user-defined classes.

Here’s an example of using the vector class to store integers:

    
#include <vector>
#include <iostream>

int main() {
    // Create a vector to store integers
    std::vector<int> myVector;

    // Add elements to the vector
    myVector.push_back(10);
    myVector.push_back(20);
    myVector.push_back(30);

    // Print the elements of the vector
    for (int i = 0; i < myVector.size(); i++) {
        std::cout << myVector[i] << " ";
    }

    return 0;
}
    
  

In this example, we create a vector called “myVector” to store integers. We then use the push_back() function to add elements to the vector. Finally, we iterate over the vector using a for loop and print each element.

The vector class provides various member functions, such as size(), empty(), at(), etc., to manipulate and access its elements. It also supports dynamic resizing, memory management, and other useful operations.

Read more

Leave a comment