How to write a Vector in C++
Vectors have the same properties as dynamic arrays, including the capacity to automatically resize whenever a component is added or removed and automated management of storage by the container. Iterations are able to access and navigate vector items since they are stored in continuous memory. Information is added at the end of the vector. Putting it at the conclusion requires more time because the set might need to be enlarged. The final part cannot be removed as there is no scaling involved. History is continuous when information is added or deleted in the initial phase or midway.
A "vector" in C++ usually means std::vector, a common container made available by the Standard C++ Template Library (STL). The dynamic array, or std::vector, can expand or contract in size as necessary. It constitutes one of the most frequently used structures for data in C++ and has the following benefits:
- Dynamic Sizing: A std::vector's size can be modified in place, as opposed to fixed-size collections. Without having to be worried about manually handling the recall, components may be added or removed.
- Contiguous Memory: Std::vector is effective for using random access since its members are stored in adjacent memory locations.
- Iterators: It offers iterators for simple element handling and navigation.
- Automated Storage Management: Std::vector manages the allocation of memory and disposal by itself, preventing a number of frequently occurring storage-related issues.
What does C++'s std::vector mean?
The class that serves as the template for vector containers and its member methods in C++ is called std::vector. It can be found in a header file titled "vector." Vector containers receive a variety of features from the member methods of the std::vector class. Here are a few instances of frequently used component functions:
Steps to write a vector in C++:
- Add the required header here
- Create and set up a vector
- Including more components in the vector
- Getting the vector's elements
- The vector's size is
- circling the vector repeatedly
- Component removal
- Extra operations
Example code:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> q1; for (int i = 1; i <= 5; i++) q1.push_back(i); cout << "Output of begin and end: "; for (auto i = q1.begin(); i != q1.end(); ++i) cout << *i << " "; cout << "\nOutput of cbegin and cend: "; for (auto i = q1.cbegin(); i != q1.cend(); ++i) cout << *i << " "; cout << "\nOutput of rbegin and rend: "; for (auto ir = q1.rbegin(); ir != q1.rend(); ++ir) cout << *ir << " "; cout << "\nOutput of begin and crend : "; for (auto ir = q1.crbegin(); ir != q1.crend(); ++ir) cout << *ir << " "; return 0; }
Output:
Example code of using std::vector :
#include <iostream> #include <vector> int main() { std::vector<int> myVector = {2, 4, 6, 8, 10}; myVector.push_back(12); for (int element : myVector) { std::cout << element << " "; } std::cout << std::endl; return 0; }
Output:
In the following example, an integer bucket is created, a component is added, and the container's contents are iterated over using the std::vector library.
For modifying and interacting with the information it holds, std::vector offers a variety of techniques and operations, like scaling, organizing, browsing, and many more. It is a flexible and effective data arrangement for a variety of typical C++ programming duties.
Iterators:
- Begin () provides an iterator referring to the vector's initial element, end() provides an iterate heading to the presumed component immediately following the vector's last element
- Provides an inverted iterator (reverse starting) referring to the vector's final element when called with the rbegin() function. At last, initially, it switches elements.
- Rend () provides an inverse iterator that points to the hypothetical component of the vector that comes before the first item (also known as the reverse end).
- An integer iterate referring to the vector's initial component is returned by the function cbegin().
- cend() - Provides an iterator that continually points to the hypothetical item that comes after the vector's last item.
- The begin () function provides an invariant negative iterate that points to the vector's final item (reverse starting).
- Crend() provides an integer reverse iterate referring to the hypothetical item preceding the initial component of the array (regarded as reverse end), moving from the last to the first element.
Example Code:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> q1; for (int i = 2; i <= 6; i++) q1.push_back(i); cout << "Output of begin and end: "; for (auto i = q1.begin(); i != q1.end(); ++i) cout << *i << " "; cout << "\nOutput of cbegin and cend: "; for (auto i = q1.cbegin(); i != q1.cend(); ++i) cout << *i << " "; cout << "\nOutput of rbegin and rend: "; for (auto ir = q1.rbegin(); ir != q1.rend(); ++ir) cout << *ir << " "; cout << "\nOutput of crbegin and crend : "; for (auto ir = q1.crbegin(); ir != q1.crend(); ++ir) cout << *ir << " "; return 0; }
Output:
Capacity:
- Size () - Provides the vector's item count.
- The function max_size() yields the highest amount of items that a vector can have.
- Capacity () - Gives the vector's existing memory allocation, represented as a number of items.
- Resize the box to fit 'n' elements by using resize(n).
- The function empty() determines if the box is vacant.
- shrink_to_fit() - Diminishes any items that exceed the container's maximum capability and lowers its capacity to accommodate its size.
- Reserve () - Appeals to ensure the vector capability can hold a minimum of n components.
Example Code:
#include <vector> using namespace std; int main() { vector<int> q1; for (int i = 1; i <= 4; i++) q1.push_back(i); cout << "Size : " << q1.size(); cout << "\nCapacity : " << q1.capacity(); cout << "\nMax_Size : " << q1.max_size(); q1.resize(4); cout << "\nSize : " << q1.size(); if (q1.empty() == false) cout << "\nVector is not empty"; else cout << "\nVector is empty"; q1.shrink_to_fit(); cout << "\nVector elements are: "; for (auto it = q1.begin(); it != q1.end(); it++) cout << *it << " "; return 0; }
Output:
Element Access:
- the operator of reference [g] at(g) - Gives a pointer to the component located at point 'g' in the vector
- front() - Provides a pointer to the vector's initial component
- back() - Provides a pointer to the vector's final element
- data() - Provides an immediate reference to the internal memory array that the vector uses to hold its owning components.
Example Code:
#include <bits/stdc++.h> using namespace std; int main() { vector<int> q1; for (int i = 5; i <= 50; i++) q1.push_back(i * 20); cout << "\nReference operator [q] : q1[4] = " << q1[4]; cout << "\nat : q1.at(5) = " << q1.at(5); cout << "\nfront() : q1.front() = " << q1.front(); cout << "\nback() : q1.back() = " << q1.back(); int* pos = q1.data(); cout << "\nThe first element is " << *pos; return 0; }
Output:
Adjusters:
- Assign () replaces the previous values push with the latest values assigned to the vector components. The _back() function is employed to put items into vectors at the back. The pop_back() function is utilized for removing components from vectors at its back.
- Introduces additional elements above the one that's at the designated location using the insert() function.
- To eliminate components from an object at a specific location or range, employ the erase() function.
- Swap (): This function is employed to exchange a vector's content for a different vector of the same type.
- Dimensions could vary. Clear () is a function that eliminates each component within the vector container.
- Emplace () - Inserts an additional component at the point to extend the container.
- The method emplace_back() is employed to add the component to the vector container.
Example Code:
#include <bits/stdc++.h> #include <vector> using namespace std; int main() { vector<int> v; v.assign(5, 30); cout << "The vector elements are: "; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; v.push_back(10); int n = v.size(); cout << "\nThe last element is: " << v[n - 1]; v.pop_back(); cout << "\nThe vector elements are: "; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; v.insert(v.begin(), 10); cout << "\nThe first element is: " << v[0]; v.erase(v.begin()); cout << "\nThe first element is: " << v[0]; v.emplace(v.begin(), 10); cout << "\nThe first element is: " << v[0]; v.emplace_back(20); n = v.size(); cout << "\nThe last element is: " << v[n - 1]; v.clear(); cout << "\nVector size after clear(): " << v.size(); vector<int> v1, v2; v1.push_back(1); v1.push_back(2); v2.push_back(3); v2.push_back(4); cout << "\n\nVector 1: "; for (int i = 0; i < v1.size(); i++) cout << v1[i] << " "; cout << "\nVector 2: "; for (int i = 0; i < v2.size(); i++) cout << v2[i] << " "; v1.swap(v2); cout << "\nAfter Swap \nVector 1: "; for (int i = 0; i < v1.size(); i++) cout << v1[i] << " "; cout << "\nVector 2: "; for (int i = 0; i < v2.size(); i++) cout << v2[i] << " "; }
Output: