Vectors in C++ serve as a dynamic array-based container designed to hold a group of elements sharing the same data type. Unlike traditional arrays, vectors have the ability to adjust their size dynamically, allowing for flexible and efficient storage and management of data.
A vector is determined by including the <vector> header and defining a vector instance. The vector's data type is indicated within angle brackets, along with the variable name. For instance:
#include <vector>
std::vector<int> myVector;
Once a vector is established, elements can be inserted into it utilizing the push_back function. This function adds a new element at the conclusion of the vector. For instance:
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
Vectors also allow retrieval of specific elements through the operator. The initial element in the vector is positioned at index 0, while the final element is at index size-1. As an illustration:
std::cout << myVector[0] << std::endl;
std::cout << myVector[1] << std::endl;
std::cout << myVector[2] << std::endl;
In conjunction with incorporating elements, vectors also offer functions for inserting, deleting, and adjusting elements. The insert function enables the addition of an element at a particular index within the vector. The erase function allows for the elimination of an element from a specific index in the vector. The clear function is utilized to eliminate all elements from a vector.
Vectors offer a variety of handy functions for traversing through the elements in a vector. The begin function yields an iterator that references the initial element in the vector, while the end function yields an iterator that references the final element in the vector. By utilizing an iterator, you can sequentially access the elements in a vector, as demonstrated in the subsequent illustration:
std::vector<int>::iterator it;
for (it = myVector.begin(); it != myVector.end(); it++)
{
std::cout << *it << std::endl;
}
Vectors additionally offer the size function, which provides the count of elements within the vector. This function is useful for verifying if a vector is devoid of elements, as illustrated in the subsequent example:
if (myVector.empty())
{
std::cout << "The vector is empty." << std::endl;
}
else
{
std::cout << "The vector is not empty." << std::endl;
}
In summary, vectors serve as a robust and effective data structure in C++ that offers a flexible array-based storage for managing and manipulating groups of elements. Vectors offer functions for inserting, deleting, and updating elements, along with capabilities for traversing through the elements within a vector. Leveraging vectors in C++ enables you to create code that is both more succinct and optimal when handling data collections.
Adding Two Vectors in C++
Here is a sample code snippet in C++ showcasing the addition of two vectors:
#include <iostream>
#include <vector>
std::vector<int> addVectors(const std::vector<int>& vec1, const std::vector<int>& vec2)
{
// Check if the vectors have the same size
if (vec1.size() != vec2.size())
{
std::cout << "Error: Vectors must have the same size to add them." << std::endl;
return std::vector<int>();
}
// Create a vector to store the result
std::vector<int> result(vec1.size());
// Add the elements of vec1 and vec2 and store the result in result
for (int i = 0; i < vec1.size(); i++)
{
result[i] = vec1[i] + vec2[i];
}
return result;
}
int main()
{
// Create two vectors
std::vector<int> vec1 = {1, 2, 3};
std::vector<int> vec2 = {4, 5, 6};
// Add the two vectors
std::vector<int> result = addVectors(vec1, vec2);
// Print the result
std::cout << "The result of adding the two vectors is: [";
for (int i = 0; i < result.size(); i++)
{
std::cout << result[i];
if (i != result.size() - 1)
{
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
return 0;
}
Output
The result of adding the two vectors is: [5, 7, 9]
Explanation:
In this code snippet, the addVectors function is designed to accept two vectors as parameters and yield a new vector with the summed elements from the input vectors. In the main function, two vectors named vec1 and vec2 are initialized, combined using the addVectors function, and the resultant vector is displayed on the console.
Note that this code assumes that the vectors are of equal size. In the event that the vectors are of different sizes, the addVectors function will output an empty vector and display an error message.