Thread: Simple Question - std::vector as argument

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Simple Question - std::vector as argument

    Hello,

    I am new to C++ and trying to understand how arguments are passed to functions, particular with regards to objects such as the std::vector.

    When a variable is passed as an argument to a function, a copy of the variable is made, and the copy is passed to the function, right? For this reason, if we are passing a large object to a function, it is wise to pass a pointer to the object, rather than the object itself, to speed things up.

    However, what happens when a std::vector is passed as an argument?

    Suppose the we have a vector of objects of type MyClass, such as vector<MyClass> my_vector, and we pass this as an argument to a function. Will the entire vector be copied, such that every instance of MyClass stored in the vector is also copied? This would take a long time right?

    But if we have a vector of pointers, such as vector<MyClass*> my_vector, then when the vector is passed as an argument, are the actual objects themselves copied, or is a copy only made of the pointers to those objects?

    Is there any way we can pass a vector as an argument, without having to make all these copies, as this is very time consuming??

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ejohns85
    Suppose the we have a vector of objects of type MyClass, such as vector<MyClass> my_vector, and we pass this as an argument to a function. Will the entire vector be copied, such that every instance of MyClass stored in the vector is also copied? This would take a long time right?
    Yes. Maybe ("long time" is relative).

    Quote Originally Posted by ejohns85
    But if we have a vector of pointers, such as vector<MyClass*> my_vector, then when the vector is passed as an argument, are the actual objects themselves copied, or is a copy only made of the pointers to those objects?
    Just as with a vector<MyClass>, the elements of the vector are copied, but in this case the elements are pointers, so the latter is correct.

    Quote Originally Posted by ejohns85
    Is there any way we can pass a vector as an argument, without having to make all these copies, as this is very time consuming??
    Pass the vector by (const) reference, or in some cases, as you noted, you would pass a pointer to the vector.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ...This would take a long time right?
    Define "long". If you store instances of objects in the class then yes this can cause problems. If you are storing pointers to objects on the heap in the vector then not so much. But 'long' is a relative term. The time required in one program may be fine whereas in another would be unacceptable. It depends on what your performance requirements are.
    Last edited by VirtualAce; 02-19-2010 at 05:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question with programming
    By eihn in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2010, 06:09 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM