When we pass an array to a function, a pointer is actually passed.
When a vector is passed to a function, a copy of the vector is created. For example, we can see below program, changes made inside the function are not reflected outside because function has a copy.
The above style of passing might also take a lot of time in cases of large vectors. So it is a good idea to pass by reference.
10 20 30
If we do not want a function to modify a vector, we can pass it as a const reference.
This article is contributed by Kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Passing by pointer Vs Passing by Reference in C++
- Passing a vector to constructor in C++
- vector::front() and vector::back() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::crend() & vector::crbegin() with example
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::empty() and vector::size() in C++ STL
- vector::begin() and vector::end() in C++ STL
- vector::at() and vector::swap() in C++ STL
- Parameter Passing Techniques in C/C++
- Passing Reference to a Pointer in C++
- How to delete an element from the Set by passing its value in C++
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Passing and Returning Objects in C++
- vector rbegin() and rend() function in C++ STL
- vector emplace() function in C++ STL
- vector max_size() function in C++ STL
- vector shrink_to_fit() function in C++ STL