so vectors have to be passed by reference only? i can't do it normally? also what's the diff between passing it as a const vector ref and a normal vector ref? sorry im a noob
so vectors have to be passed by reference only? i can't do it normally?
of course you can pass them normally (that is pass them by value). but when you pass objects by value what happens is that a new object is created and then the object you pass is copied to the new object. this, for standard types (char, int, float etc) is ok, because standard types are small in size (char is one byte, short is 2, int is 2 or 4, long is 4 etc...). a vector however is not that small. Try doing this:
in my pc I get 12 as an output. that's 12 bytes. so instead of passing by value, I pass it by reference which has a cost of 4 bytes (the size of the variable holding the address of my vector)
also what's the diff between passing it as a const vector ref and a normal vector ref?
using the const keyword will inform your compiler that you don't want to change the object passed. If you try to do it accidentally your compiler will generate an error.
thanks alot man. so using the const keyword is a way to pass by ref without changing the value of the vector being passed correct? so isn't that the same as passing a vector normally? since when u pass it by ref with const, it does not change the vector. and when you pass it normally, it just makes a copy of the vector and operates on that correct? so the original vector is never changed.
Absolutely! The only difference is that passing a vector by value (what you refer to as "normally") costs more than passing it by a const reference. So, to sum up, if you don't want to modify an object, if its size is <=4 pass it by value, else pass it by a const reference. And if you want to modify it pass it by a (non-const) reference.
@master roshi...i don't want to modify the original, however, i want be able to use the data from the original vector and modify that data but not the vector itself. i passed it by value and that works however, when i try to pass by const ref and modify the data, i get an error like you said.
if i pass a vector by const ref but i can't modify it's data? then what's the point? is there a way to solve this without passing it by value?
@sammy, yea but passing by ref changes the original vector, i do not want to change the original vector. i just want to operate on the data it contains. i can pass by value and make a copy of the vector and work on the copy and the original would not be changed but i do not want to do that. whats another way of doing this? roshi said that that passing by const ref does not change the original vector, however, you can't operate on the data cause like he said, any attempts to change the object passed would produce an error.
passing by reference itself doesn't change the original vector. passing by reference allows you to change the vector inside your function (which is equivalent to modifying the data it contains)
if you don't want to modify the original vector but want to change the data maybe the solution to your problem is creating a new copy of the vector and work with this:
Generally (such as in the STL), vectors and other containers are passed as a pair of iterators to the range targeted, rather than the whole container; however I wouldn't say that that's a great amount different from passing it by ref. (The main advantage is more user control over the affected range of elements, afaik.) If you are trying to manipulate a copy of the vector just pass it normally, as an argument; a copy of it will be created (although this is rather memory inefficient).
I can imagine that const ref would come in handy if you had a real memory hog of an object that you needed to handle without modifying the original. Passing a copy would be a ram disaster. Of course, it's quite possible that such a massive variable would be a god object, but that's another matter.