Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
at() function is used reference the element present at the position given as the parameter to the function.
Syntax:
vectorname.at(position) Parameters: Position of the element to be fetched. Returns: Direct reference to the element at the given position.
Examples:
Input: myvector = 1, 2, 3
myvector.at(2);
Outpu: 3
Input: myvector = 3, 4, 1, 7, 3
myvector.at(3);
Output: 7
Errors and Exceptions
- If the position is not present in the vector, it throws out_of_range.
- It has a strong no exception throw guarantee otherwise.
// CPP program to illustrate // Implementation of at() function #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector; myvector.push_back(3); myvector.push_back(4); myvector.push_back(1); myvector.push_back(7); myvector.push_back(3); cout << myvector.at(3); return 0; } |
Output:
7
Applications:
Given a vector of integers, print all the integers present at even positions.
Input: 1, 2, 3, 4, 5, 6, 7, 8, 9 Output: 1 3 5 7 9 Explanation - 1, 3, 5, 7 and 9 are at position 0, 2, 4, 6 and 8 which are even
Algorithm
- Run a loop till the size of the vector.
- Check if the position is divisible by 2, if yes, print the element at that position.
// CPP program to illustrate // Application of at() function #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector; myvector.push_back(1); myvector.push_back(2); myvector.push_back(3); myvector.push_back(4); myvector.push_back(5); myvector.push_back(6); myvector.push_back(7); myvector.push_back(8); myvector.push_back(9); // vector becomes 1, 2, 3, 4, 5, 6, 7, 8, 9 for (int i = 0; i < myvector.size(); i += 2) { cout << myvector.at(i); cout << " "; } return 0; } |
Output:
1 3 5 7 9
This function is used to swap the contents of one vector with another vector of same type and sizes of vectors may differ.
Syntax:
vectorname1.swap(vectorname2) Parameters: The name of the vector with which the contents have to be swapped. Result: All the elements of the 2 vectors are swapped.
Examples:
Input: myvector1 = {1, 2, 3, 4}
myvector2 = {3, 5, 7, 9}
myvector1.swap(myvector2);
Output: myvector1 = {3, 5, 7, 9}
myvector2 = {1, 2, 3, 4}
Input: myvector1 = {1, 3, 5, 7}
myvector2 = {2, 4, 6, 8}
myvector1.swap(myvector2);
Output: myvector1 = {2, 4, 6, 8}
myvector2 = {1, 3, 5, 7}
Errors and Exceptions
- It throws an error if the vector is not of the same type.
- It has a basic no exception throw guarantee otherwise.
// CPP program to illustrate // Implementation of swap() function #include <iostream> #include <vector> using namespace std; int main() { // vector container declaration vector<int> myvector1{ 1, 2, 3, 4 }; vector<int> myvector2{ 3, 5, 7, 9 }; // using swap() function to swap // elements of vector myvector1.swap(myvector2); // printing the first vector cout << "myvector1 = "; for (auto it = myvector1.begin(); it < myvector1.end(); ++it) cout << *it << " "; // printing the second vector cout << endl << "myvector2 = "; for (auto it = myvector2.begin(); it < myvector2.end(); ++it) cout << *it << " "; return 0; } |
Output:
myvector1 = 3 5 7 9 myvector2 = 1 2 3 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Difference between cout and std::cout in C++
- Sorting Vector of Arrays in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Minimize count of divisions by D to obtain at least K equal array elements
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-C++
- Lexicographical smallest number after at most K consecutive swaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

