Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emplace_back と push_back の違いに関する説明を修正 #85

Merged
merged 2 commits into from
Dec 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions docs/ch03-05-vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ x.emplace_back(5);
auto size2 = x.size(); // 6
```

!!! question "emplace_backとpush_back"
C++11より前は `push_back` という関数のみが末尾への要素追加を担っていました。
C++11で追加された `emplace_back` は要素型のコンストラクタに直接引数を渡すことができるので
`push_back` と同じかそれ以上のパフォーマンスを得られます。
多くの処理系では `push_back` を実装するのに `emplace_back` を呼び出していますから
現代において `push_back` を使うべき理由は存在しません。
!!! question "`emplace_back` と `push_back`"
C++11 より前は `push_back` という関数のみが末尾への要素追加を担っていました。
C++11 で追加された `emplace_back` は要素型のコンストラクタに
直接引数を渡すことができるので `push_back` と同じか
それ以上のパフォーマンスを得られるケースがあります。
両者の最適な使い分けは高度なトピックとなるため、
詳細は [書籍 Effective Modern C++](https://www.oreilly.co.jp/books/9784873117362/) で紹介される
「項目42:要素の挿入の代わりに直接配置を検討する」を参照ください。

## 末尾から要素削除

Expand Down