Thread: costructing a, object while pushing_back

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    62

    costructing a, object while pushing_back

    hey

    do I have a way to push_back a data sctructure into a vector without an outside copy? I mean: can I initialize the constructor of a class directly on vector im storing it?

    avoiding doing this:
    Code:
    vector<particle> Event;
    particle part(id,pz,spin);
    Event.push_back(part);
    tks in advance for any help I get

    Edit: my obvious solution is to have an outside particle reseting but that defeats the portpuso of having multiple constructors
    Last edited by killme; 04-26-2013 at 08:30 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    in C++11 they added an emplace_back() that constructs the object in place. not sure which compilers support it yet, but at least gcc 4.7 does.

    something like this:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    class A
    {
      public:
        A(int a) { std::cout << "A(int) called" << std::endl; }
        A(std::string a) { std::cout << "A(std::string) called" << std::endl; }
    };
    
    int main()
    {
      std::vector<A> v;
      v.emplace_back(3);
      v.emplace_back("foo");
      return 0;
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Visual C++ I know supports it. Clang more then likely not does too, seeing as it is probably the compiler closes to implementing full C++11 support. It also tend to rely on the standard library shipped with GCC.
    Also, push_back isn't so bad, really, since you can move the data into the vector (but emplace_back is better). Moving is usually cheap. Example:

    vector<particle> Event;
    Event.push_back(particle(id,pz,spin));

    or

    vector<particle> Event;
    particle part(id,pz,spin)
    Event.push_back(std::move(part));
    // Don't use part after this; its state is undefined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    using C++11 isn't really an option (not that I don't have a compiler for it, its just a project requirement)

    Quote Originally Posted by Elysia View Post
    vector<particle> Event;
    Event.push_back(particle(id,pz,spin));
    this doesn't have the need to allocate temporary memory every time its called?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the C++ standard allows the compiler to optimize away some copies. not sure if this is one of them.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by killme View Post
    using C++11 isn't really an option (not that I don't have a compiler for it, its just a project requirement)

    this doesn't have the need to allocate temporary memory every time its called?
    Yes, it does allocate a temporary. But in C++11, you would move the temporary into the vector, thereby avoiding having to create an entire new object and copy the data over.
    If you can't use C++11, then you are out of luck. Your best option would be simply to store pointers and lazily construct the objects.
    If you case use TR1, you still have smart pointers at your disposal. Otherwise, there's boost.
    In worst case, you can make your own to manage memory for you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question: Determining object scope from root object
    By Imanuel in forum C++ Programming
    Replies: 6
    Last Post: 12-25-2012, 11:20 AM
  2. Trouble storing ifstream object in pair object
    By Programmer_P in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2012, 01:20 AM
  3. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  4. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 07:58 PM