Simple Question - std::vector as argument

This is a discussion on Simple Question - std::vector as argument within the C++ Programming forums, part of the General Programming Boards category; Hello, I am new to C++ and trying to understand how arguments are passed to functions, particular with regards to ...

  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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    ...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 04: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, 05: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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21