C++ Vector data()30 Aug 2024 | 1 min read This function returns a pointer to an array internally used by a vector to store its elements. SyntaxConsider a vector 'v' and pointer 'p'. Syntax would be: ParameterIt does not contain any parameter. Return valueIt returns a pointer to an array. Example 1Let's see a simple example. Output: 10 20 30 40 50 In this example, k is the pointer of integer type referring to the elements of a vector. Example 2Let's see a simple another example. Output: C C++ Java .Net In this example, k is the pointer of string type referring to the elements of a vector v. Next TopicC++ Vector |
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 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 resize() It modifies the size of the vector to the specified value Size changed to 4 and new value is Syntax Consider a vector v. Syntax would be: v.resize(n,val); Parameter n : It is the new vector size. val : If n is greater than the current vector size, then value(val) is...
1 min read
C++ Vector end() This function returns an iterator referring to the past-last-element in the vector container. Syntax Consider a vector v. Syntax would be: iterator it=v.end() Parameter It does not contain any parameter. Return value It returns an iterator following the last element. Example 1 Let's see a simple example. #include<iostream> #include<vector> using namespace std; int main() { vector<int> v{10,20,20,40}; vector<int>::iterator it; for(it=v.begin();it!=v.end();it++) cout<<*it<<" "; return...
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 emplace() This function inserts a new element just before the position pos and size of the vector container increases. Syntax Consider a vector 'v'. Syntax would be: Iterator it=v.emplace(pos,args); Parameter pos: It defines the position before which the new element is to be inserted. args: Arguments forwarded to construct the new...
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 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 pop_back() It deletes the last element and reduces the size of the vector by one. Syntax Consider a vector v.Syntax would be: v.pop_back(); Parameter It does not contain any parameter. Return value It does not return any value. The following illustration show how pop_back() function works : This illustration shows how last element of...
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