Vector int sorting

Hi all,

I'm new here, so I apologize in advance if I do anything noobish. Nice to meet you all.

I want to sort a vector int in ascending order, but when I test, the output isn't correct - the vector is still unsorted. Am I calling it incorrectly?

int sorted (vector <int> a) {
int size = a.size();
sort(a.begin(), a.end()); //problem

Any help is appreciated.
That is not all of your code.

What happens when you pass a parameter by value to a function?
int sorted (vector <int> a)

when you call this function, a is passed by value - that means a copy of the vector is passed to the function, not the vector itself. All the changes (sorting) you make within the function are gone when you return from the function, your original vector is unchanged. I suggest you modify your function to accept a reference instead, it's actually a 1 character change :-)

See the section
Arguments passed by value and by reference
at the following link: http://www.cplusplus.com/doc/tutorial/functions/

I really wish awesome works like K&R were available as webpages and we could just post links to different sections...
Topic archived. No new replies allowed.