Блог пользователя GODF

Автор GODF, история, 7 лет назад, По-английски

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

  • Проголосовать: нравится
  • +29
  • Проголосовать: не нравится

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +35 Проголосовать: не нравится

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.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

Unless you need to worry about things like exception handling, don't use emplace_back. It isn't faster.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

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)