std::vector is a container class template in the C++ Standard Template Library (STL). It is a dynamic array that can grow or shrink in size as needed. std::vector is defined in the <vector> header file.
Here are some examples of how std::vector can be used:
Storing a collection of elements:
Copy code
std::vector<int> v { 1, 2, 3, 4, 5 };
In this example, a std::vector named v is initialized with a list of integers. The vector can be used to store and manipulate a collection of integers.
Dynamic size:
Copy code
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
In this example, a std::vector named v is created and elements are added using the push_back() function. The size of the vector can grow dynamically as elements are added.
Iterating through elements:
Copy code
std::vector<int> v { 1, 2, 3, 4, 5 };
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
In this example, a std::vector named v is initialized with a list of integers. The vector provides an iterator, which can be used to iterate through the elements of the vector.
Accessing elements:
Copy code
std::vector<int> v { 1, 2, 3, 4, 5 };
std::cout << v[2] << std::endl; // Output: 3
In this example, a std::vector named v is initialized with a list of integers. The vector provides an array-like syntax for accessing elements by index.
Sorting elements:
Copy code
std::vector<int> v { 5, 4, 3, 2, 1 };
std::sort(v.begin(), v.end());
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
In this example, a std::vector named v is initialized with a list of integers. The std::sort algorithm can be used to sort the elements of the vector in ascending order.
Finding an element:
Copy code
std::vector<int> v { 1, 2, 3, 4, 5 };
auto it = std::find(v.begin(), v.end(), 3);
if (it != v.end()) {
std::cout << "Found 3 in the vector" << std::endl;
}
In this example, a std::vector named v is initialized with a list of integers. The std::find algorithm can be used to search for an element in the vector.
These are just a few examples of how std::vector can be used. The vector provides a rich set of functions and interfaces that can be used to manipulate, sort, and access elements, making it a powerful and versatile container.
any more?
Sure, here are a few more examples of how std::vector can be used:
Resizing the vector:
Copy code
std::vector<int> v;
v.resize(10); // Resize the vector to have 10 elements
In this example, the resize() function is used to change the size of the vector to 10 elements. If the vector is being made smaller, the extra elements will be removed, if the vector is being made bigger, the new elements will be initialized with the default value.
Inserting elements:
Copy code
std::vector<int> v { 1, 2, 3, 4, 5 };
v.insert(v.begin() + 2, 6); // insert 6 at index 2
In this example, the insert() function is used to insert an element (6) at a specific position in the vector.
Removing elements:
Copy code
std::vector<int> v { 1, 2, 3, 4, 5 };
v.erase(v.begin() + 2); // remove the element at index 2
In this example, the erase() function is used to remove an element from the vector.
Using vector with user-defined types
Copy code
class Point {
public:
int x, y;
Point(int x = 0, int y = 0) : x(
any more
Yes, here are a few more examples:
Using std::vector with user-defined types:
Copy code
class Point {
public:
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
};
std::vector<Point> points;
points.push_back(Point(1, 2));
points.push_back(Point(3, 4));
In this example, a user-defined Point class is used with a std::vector. The vector can store objects of the Point class and provides the same set of functions and interfaces as with built-in types.
Using std::vector with custom allocator
Copy code
#include <memory>
std::vector<int, std::allocator<int>> v;
In this example, custom allocator is used with vector. This can be useful in scenarios where the program needs to allocate memory in a specific way.
Using std::vector with custom comparator
Copy code
std::vector<int> v{3,1,5,2,4};
std::sort(v.begin(), v.end(), std::greater<int>());
To embed this project on your website, copy the following code and paste it into your website's HTML: