Give me an instance of declaring a vector pointer and passing it to a function/
Give me an instance of declaring a vector pointer and passing it to a function/
What have you tried? Why are you not passing the vector by (const) reference?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am a beginner so I don't understand very much technical words, kindly elaborate, actually i want to send a vector to a function and get the value of the vector from the function
I can understand that this can be achieved by return of the function or sending the pointer of the vector and writing the same there in function.
what does it mean by passing the vector by (const) reference?
I suggest that you read a book that will explain these basics to you in a more structured fashion, complete with code examples. For example, you could work through Accelerated C++ by Koenig and Moo.Originally Posted by kapil1089thekin
Otherwise, we will essentially be writing a tutorial specially for you.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am consulting the book Complete C++ reference by Herbert Schildt, not getting exactly what I am posting here, more then that I am intermediate not exactly beginner, but trying to learn techniques and when I don't find it there I post them here.
Kindly help if possible.
I would approach with caution any book by Herbert Schildt as books by that author are notorious for factual inaccuracy.Originally Posted by kapil1089thekin
Have you not seen a code example similiar to:
In the above function, numbers is a reference parameter, hence a call such as:Code:void add123(std::vector<int>& numbers) { numbers.push_back(123); }
will cause v to have 123 pushed to its back.Code:std::vector<int> v; add123(v);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
In C to send the parameter by reference we had to declare the parameter as pointer, but in C++ declaring it by name but the writing the function in the format u said works, can you explain why is it so? what is going the story behind?
Within the bounds of the language, when you do
add123(v), then numbers becomes an alias for v. That is, they refer to the same object, with numbers being a different name for v.
That is really all to it.
You don't really need to care about how it works behind the scenes, because it's not specified by the standard and is irrelevant.