C++ Vector shrink_to_fit()30 Aug 2024 | 1 min read This function is used to change the capacity of the vector and makes it equal to the size of the vector. SyntaxConsider a vector 'v'. Syntax would be: ParameterIt does not contain any parameter. Return valueIt does not return any value. 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 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 at() It gives a reference to an element. Syntax Consider a vector v and k is the position. Syntax would be: vector<object_type> v; v.at(k) ; Parameter k: k defines the position of element which is to be returned by at() function. Return value It returns the element of specified position. The following illustration...
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 begin() This function is used to point the first element of the vector. begin() vs front() The begin() function is used to return an iterator pointing to the first element of the vector while front() function is used to return a reference to the same element in...
1 min read
C++ Vector cend() This function is used to point to the past-the-last element (element after the last element) in the vector. cend() vs end() The cend() function returns the constant iterator while end() function returns an iterator. The element pointed by the end() functioncan be modified but not by...
1 min read
C++ Vector operator[]() This function is used to access a specified element. Syntax Consider a vector 'v' and position 'pos'. Syntax would be: v.operator[ ](pos); Parameter pos: It defines the position of the element. Return value It returns the element of specified location. Example 1 Let's see a simple example. #include<iostream> #include<vector> using namespace std; int main() { vector<string> v{"C","C++","java"}; for(int i=0;i<v.size();i++) cout<<v.operator[](i)<<" "; return...
1 min read
C++ Vector data() This function returns a pointer to an array internally used by a vector to store its elements. Syntax Consider a vector 'v' and pointer 'p'. Syntax would be: data_type *p=v.data(); Parameter It does not contain any parameter. Return value It returns a pointer to an array. Example 1 Let's see a simple example. #include...
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 empty() This function determines whether the vector is empty or not. Syntax Consider a vector v. Syntax would be : bool empty(); Parameter It does not contain any parameter. Return value It returns a boolean value either 0 or 1. Example 1 Let's see a simple example. #include<iostream> #include<vector> using namespace std; int main() { vector<int> v; if(v.empty()) cout<<"Vector v is empty"; else cout<<"Vector...
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