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.
- Common Subtleties in Vector STLs
- vector::push_back() and vector::pop_back() in C++ STL
- Vector in C++ STL
- Initialize a vector in C++ (5 different ways)
- vector erase() and clear() in C++
- How arrays are passed to functions in C/C++
- Passing Reference to a Pointer in C++
- Nested list in C++ STL
- wcrtomb() function in C/C++
- vswprintf() function in C/C++