vector::at() and vector::swap() in C++ STL
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);
Output : 3
Input : myvector = 3, 4, 1, 7, 3
myvector.at(3);
Output : 7
Errors and Exceptions
1. If the position is not present in the vector, it throws out_of_range.
2. 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
Application
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
1. Run a loop till the size of the vector.
2. 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 size.
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
1. It throws an error if the vector are not of the same type.
2. It throws error if the vector are not of the same size.
2. 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
Recommended Posts:
- Conditional or Ternary Operator (?:) in C/C++
- forward_list insert_after() function in C++ STL
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- C++ | asm declaration
- Pointers and References in C++
- Strings in C++ and How to Create them?
- Enum Classes in C++ and Their Advantage over Enum DataType
- Deque vs Vector in C++ STL
- C++ Programming Basics
- Introduction to C++ Programming Language
- ios manipulators noshowpos() function in C++
- ios rdstate() function in C++ with Examples
- ios operator !() function in C++ with Examples
- ios operator() function in C++ with Examples
- ios clear() function in C++ with Examples
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.
Improved By : soumya7



