hello,
i have a few questions pls help me out with them coz am really confused ..

1. can lists or vectors passed as parameters in a function?
2. can the lists or vectors be returned ?

i want to use lists or vectors in classes i need to knw if this is possible .
pls help me out
thankx.

1. can lists or vectors passed as parameters in a function?
Yes
2. can the lists or vectors be returned ?
Yes

Beware of the performance costs, though. If you just pass a vector, as in the code below, you'll end up copying the entire vector, which takes an amount of time proportional to the length of the vector. Unless the compiler is unreasonably intelligent, which is never the case.

int foo(vector<int> x) {
   return x.back() - x.front();
}

So in situations like these, pass the object by reference, so that x refers to caller's vector.

int foo(vector<int>& x) {
    return x.back() - x.front();
}

The second version takes a constant amount of time.

thankx ... am doing a project for school which involves a lot of file operations i wantedto avoid using an intermediate file as a buffer so thought of using lists/vectors which will be faster and easier .. but the cost overhead is higher but so is using the files so evens it out i think ...
thaknx :)

Assuming you have enough memory, passing a list/vector should be faster than file access for the same information. Passing by reference does expose the material being passed to being changed, so remember to use the keyword const judiciously if you don't want that to happen.

This article has been dead for over six months. Start a new discussion instead.