C++ std::string append vs push_back()

In C++03 (for which most of “cplusplus.com”‘s documentation is written), the complexities were unspecified because library implementers were allowed to do Copy-On-Write or “rope-style” internal representations for strings. For instance, a COW implementation might require copying the entire string if a character is modified and there is sharing going on. In C++11, COW and rope … Read more

Why emplace_back is faster than push_back?

Your test case isn’t very helpful. push_back takes a container element and copies/moves it into the container. emplace_back takes arbitrary arguments and constructs from those a new container element. But if you pass a single argument that’s already of element type to emplace_back, you’ll just use the copy/move constructor anyway. Here’s a better comparison: Foo … Read more

Vector of structs initialization

Create vector, push_back element, then modify it as so: struct subject { string name; int marks; int credits; }; int main() { vector<subject> sub; //Push back new subject created with default constructor. sub.push_back(subject()); //Vector now has 1 element @ index 0, so modify it. sub[0].name = “english”; //Add a new element if you want another: … Read more

Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects

Let’s see what the different calls that you provided do: emplace_back(mystring): This is an in-place construction of the new element with whatever argument you provided. Since you provided an lvalue, that in-place construction in fact is a copy-construction, i.e. this is the same as calling push_back(mystring) push_back(std::move(mystring)): This calls the move-insertion, which in the case … Read more

Is it safe to push_back an element from the same vector?

It looks like http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#526 addressed this problem (or something very similar to it) as a potential defect in the standard: 1) Parameters taken by const reference can be changed during execution of the function Examples: Given std::vector v: v.insert(v.begin(), v[2]); v[2] can be changed by moving elements of vector The proposed resolution was that this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)