Hi. I have following problem. Yesterday I tried to solve this problem The same code with emplace_back does not pass the system test. However the same code with push_back passes the system test. So my question is: What is happening here ? Link to the submission with push_back Link to the submission with emplace_back
push_back creates a copy of the object you want to insert before pushing it to the vector and hence it modifies the type accordingly (in this case to long double) whereas emplace_back just moves the argument to the vector and hence the element remain of the same type and here you can check that atan2 is a built-in function that returns a double and not a long double.
So your problem was with precision and not with the use of push_back and emplace_back.
Thank you! Now i got it. When I cast it to long double it gets accepted.
That doesn't make sense to me,
emplace_back
just calls some constructor with the arguments, and there thedouble
would be cast tolong double
in my understanding.Unless you need to worry about things like exception handling, don't use
emplace_back
. It isn't faster.emplace_back can also be used to append objects with fewer characters. For example for a vector<pair<int,int>> you can do: vec.emplace_back(a,b)
Similarly, you can go with vec.push_back({a, b}).
But
vec.emplace_back(a, b)
has one more character thanvec.push_back({a, b})
... :DOk, you got me there. Just to pretend Im not completely wrong, you can also use emplace_back when you use structs or classes. For example for a struct for points named pt and for vector vec you can use vec.emplace_back(x,y) in comparision to vec.push_back(pt(x,y)). (Plus you can do #define eb emplace_back)