duction to C++ Vector
In C++, a vector is a dynamic array that can grow or shrink in size. It is part of the Standard Template Library (STL) and provides a flexible and efficient way to store and manipulate collections of elements. Vectors are commonly used in C++ programming due to their versatility and ease of use.
Creating a Vector
To create a vector in C++, you need to include the <vector>
header file. Here’s an example of how to create a vector:
#include <vector> using namespace std; int main() { // Create an empty vector vector<int> numbers; // Add elements to the vector numbers.push_back(10); numbers.push_back(20); numbers.push_back(30); return 0; }
In the above example, we create an empty vector called “numbers” using the vector<int>
syntax. We then use the push_back()
function to add elements to the vector. The vector automatically resizes itself to accommodate the new elements.
Accessing Elements in a Vector
You can access elements in a vector using the square bracket notation ([]). Here’s an example:
#include <vector> #include <iostream> using namespace std; int main() { vector<string> fruits; fruits.push_back("Apple"); fruits.push_back("Banana"); fruits.push_back("Orange"); // Accessing elements cout << fruits[0] << endl; // Output: Apple cout << fruits[1] << endl; // Output: Banana cout << fruits[2] << endl; // Output: Orange return 0; }
In the above example, we create a vector called “fruits” and add three elements to it. We then use the square bracket notation to access and print the elements of the vector.
Iterating Over a Vector
You can use a loop to iterate over the elements of a vector. Here’s an example:
#include <vector> #include <iostream> using namespace std; int main() { vector<int> numbers; numbers.push_back(1); numbers.push_back(2); numbers.push_back(3); // Iterating over the vector for (int i = 0; i < numbers.size(); i++) { cout << numbers[i] << endl; } return 0; }
In the above example, we create a vector called “numbers” and add three elements to it. We then use a for loop to iterate over the vector and print each element.
Vector Operations
C++ vectors provide various operations to manipulate and modify the elements. Here are some commonly used operations:
- push_back(element): Adds an element to the end of the vector.
- pop_back(): Removes the last element from the vector.
- insert(iterator, element): Inserts an element at a specified position in the vector.
- erase(iterator): Removes an element at a specified position from the vector.
- size(): Returns the number of elements in the vector.
- empty(): Checks if the vector is empty.
- clear(): Removes all elements from the vector.
Here’s an example that demonstrates some of these operations:
#include <vector> #include <iostream> using namespace std; int main() { vector<int> numbers; numbers.push_back(1); numbers.push_back(2); numbers.push_back(3); // Inserting an element at position 1 numbers.insert(numbers.begin() + 1, 4); // Removing the element at position 2 numbers.erase(numbers.begin() + 2); // Printing the elements for (int i = 0; i < numbers.size(); i++) { cout << numbers[i] << endl; } return 0; }
In the above example, we add three elements to the vector “numbers”. We then insert an element at position 1 using the insert()
function and remove the element at position 2 using the erase()
function. Finally, we iterate over the vector and print its elements.
Conclusion
C++ vectors are a powerful tool for managing collections of elements. They provide a flexible and efficient way to store, access, and manipulate data. By understanding how to create and use vectors, you can enhance your C++ programming skills and build more robust applications.