C++ Vector resize()17 Mar 2025 | 1 min read It modifies the size of the vector to the specified value ![]() Size changed to 4 and new value is SyntaxConsider a vector v. Syntax would be: Parametern : It is the new vector size. val : If n is greater than the current vector size, then value(val) is inserted in the added space. Return valueIt does not return any value. Example 1Let's see a simple example when n is less than the current vector size. Output: Initial elements are: 1 2 3 4 5 6 7 8 9 10 After resizing its size to 5, elementsare: 1 2 3 4 5 Example 2Let's see a simple example n is greater than the current vector size. Output: Elements of v1 are :java C C++ java C C++ .Net .Net Next TopicC++ Vector |
C++ Vector size() It determines the number of elements in the vector. Syntax Consider a vector 'v' and number of elements 'n'. Syntax would be : int n=v.size(); Parameter It does not contain any parameter. Return value It returns the number of elements in the vector. Example 1 Let's see a simple example. #include<iostream> #include<vector> using namespace std; int main() { vector<string>...
1 min read
C++ Vector swap() This function is used to exchange the elements specified in two vectors. Syntax Consider two vectors v1 and v2. Syntax would be: v1.swap(v2); Parameter v2: v2 is a vector, whose value is to be swapped with another vector. Return value It does not return any value. Example 1 Let's see a simple example. #include<iostream> #include<vector> using...
1 min read
C++ Vector rbegin() The rbegin() stands for 'reverse beginning' and is used to point the last element of the vector. Syntax Consider a vector 'v' and syntax would be : reverse_iterator ritr=v.rbegin(); Parameter It does not contain any parameter. Return value It returns a reverse iterator pointing to the last element of the vector. Example...
1 min read
C++ Vector push_back() This function adds a new element at the end of the vector. Syntax Consider a vector v and 'k' is the value. Syntax would be: v.push_back(k) Parameter k: k is the value which is to be inserted at the end of the vector. Return value This function does not return any...
1 min read
C++ Vector capacity() This function determines the current capacity of the vector. Note: Capacity of the vector can be equal or greater than the size of the vector and if it is greater than the size of the vector, means allowing extra space to accommodate further operations. Syntax Consider a...
1 min read
C++ Vector cbegin() This function is used to point the first element of the vector container. cbegin() vs begin() The cbegin() function returns the constant iterator while begin() function returns an iterator. The element pointed by the end() function can be modified but not by cend() function. Syntax Consider a vector...
1 min read
C++ Vector front() It gives a reference to the first element of vector. Syntax Consider a vector.Syntax would be: v.front(); Parameter This function does not contain any parameter. Return value This function returns the first element of the vector. Example 1 Let's see a simple example. #include<iostream> #include<vector> using namespace std; int main() { vector<string> language{"java","C","C++"}; cout<<language.front(); return 0; } Output: java In this example, front()...
1 min read
C++ Vector operator=() This function assigns new values to the vector container and replacing old ones. Syntax Consider two vectors 'v' and 'v1'. Syntax would be : v.operator=(v1); Here, the values of v1 vector are assigned to the vector v2. Parameter v1:v1 is a vector object. Return value It returns *this. Example 1 Let's see a simple...
1 min read
C++ Vector clear() This function removes all the elements from the vector. Syntax Consider a vector v. Syntax would be: v.clear(); Parameter It does not contain any parameter. Return value It does not return any value. Example Let's see a simple example. #include<iostream> #include<vector> using namespace std; int main() { vector<int> v{1,2,3,4,5}; cout<<"Elements of v vector are :"; for(int i=0;i<v.size();i++) cout<<v[i]<<" "; v.clear(); for(int i=0;i<v.size();i++) cout<<v[i]; return...
1 min read
C++ Vector back() It gives a reference to the last element. Syntax Consider a vector v.Syntax would be: v.back(); Parameter This function does not contain any parameter. Return value This function returns the last element of vector Example Let's see a simple example. #include<iostream> #include<vector> using namespace std; int main() { vector<string> fruit{"mango","apple","banana"}; cout<<fruit.back(); return 0; } Output: banana In this example, back() function displays...
1 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India