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.
This operator is used to assign new contents to the container by replacing the existing contents.
It also modifies the size according to the new contents.
vectorname1 = (vectorname2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as parameter to the container written on left side of the operator.
Input : myvector1 = 1, 2, 3 myvector2 = 3, 2, 1, 4 myvector1 = myvector2; Output : myvector1 = 3, 2, 1, 4 Input : myvector1 = 2, 6, 1, 5 myvector2 = 3, 2 myvectoe1 = myvector2; Output : myvector1 = 3, 2
Errors and Exceptions
1. If the containers are of different types, an error is thrown.
2. It has a basic no exception throw guarantee otherwise.
myvector1= 3 2 1 4
This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at() function throws an out-of-range exception when the position is not in the bounds of the size of vector, while this operator causes undefined behaviour.
vectorname[position] Parameters : Position of the element to be fetched. Returns : Direct reference to the element at the given position.
Input : myvector = 1, 2, 3 myvector; Output : 3 Input : myvector = 3, 4, 1, 7, 3 myvector; Output : 7
Errors and Exceptions
1. If the position is not present in the vector, it shows undefined behaviour.
2. It has a no exception throw guarantee otherwise.
Given a vector of integers, print all the integers present at odd positions.
Input :1, 2, 3, 4, 5, 6, 7, 8, 9 Output :2 4 6 8 Explanation - 2, 4, 6, and 8 are at position 1, 3, 5 and 7 which are odd
1. Run a loop till the size of the vector.
2. Check if the position is not divisible by 2, then print the element at that position.
2 4 6 8
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- fill() and fill_n() functions in C++ STL
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- accumulate() and partial_sum() in C++ STL : numeric header
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Descending order in Map and Multimap of C++ STL
- Insertion and Deletion in STL Set C++
- std::minmax() and std::minmax_element() in C++ STL
- deque::at() and deque::swap() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::front() and list::back() in C++ STL
- list::empty() and list::size() in C++ STL
- list::remove() and list::remove_if() in C++ STL
- queue::empty() and queue::size() in C++ STL
- queue::push() and queue::pop() in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
- stack empty() and stack size() in C++ STL
- deque::front() and deque::back() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.