336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

What really happens when you insert object to a vector? Well, lets find out!

Lets consider code like this, first we have a class Foo:

1
2
3
4
class Foo
{
   //Foo stuff.
};

Now it is time to use it, for example like this:

1
2
3
4
5
6
7
8
std::vector<Foo> fooVector;
//Some code here...
for(int i = 0; i < 10; ++i)
{
  Foo bar;
  //Do something with bar here.
  fooVector.push_back(bar);
}

What will happen here, is that a copy constructor for Foo will be called, and depending on your Foo implementation, it can be very time consuming.

Ok so, maybe this would help:

1
2
3
4
5
6
7
std::vector<Foo> fooVector;
//Some code here...
for(int i = 0; i < 10; ++i)
{
  fooVector.push_back(Foo());
  //Do something with last fooVector element.
}

Looks like it might be better! However this solution will still call the copy constructor. In this scenario, fallowing will happen:

  • Constructor for Foo will be called and TEMP variable will be created.
  • Copy constructor will be called, returned value will be inserted to vector.
  • Destructor for TEMP object will be called.

And this cannot be optimized by the compiler. While it might be faster than the previous solution, it can still be very time consuming.

So what to do? Unfortunatelly, in C++03 in search for fastest implementation, our only option is to use pointers here. Whether it would be smart pointer or raw pointer, you will not get away without them.

For example we could just do this:

1
2
3
4
5
6
7
8
std::vector<Foo*> fooVector;
//Some code here...
for(int i = 0; i < 10; ++i)
{
  fooVector.push_back(new Foo());
  //Do something with last fooVector element.
}
//Don't forget to take care of your memory or use smart pointers!

While this is fine in optimization way, it does not keep the C++ style, as if the fooVector is local variable with life time limited by its scope, that is — it should be created in stack and it does not make much sense to keep its elements in the heap if we will need them only for the same scope as our vector. However in C++11 we will be able to get around in this situation in more of the C++ style! With emplace (or emplace_back), that does the fallowing:

The element is constructed in-place, i.e. no copy or move operations are performed. The constructor of the element is called with exactly the same arguments, as supplied to the function.

We will be able to keep the C++ style in this kind of situations while maintaining high C++ performance.
In the “old” C++ I’d suggest to use ptr_container provided by boost for this situation instead of raw pointer use. Comparison can be found on my post about RAII.

출처 : https://mikeongis.wordpress.com/2011/11/10/inserting-object-to-c11-vector-without-copy-constructor/


블로그 이미지

뚱땡이 우주인

,