Skip to content

Latest commit

 

History

History
63 lines (51 loc) · 1.33 KB

back_inserter.md

File metadata and controls

63 lines (51 loc) · 1.33 KB

back_inserter

  • iterator[meta header]
  • std[meta namespace]
  • function template[meta id-type]
namespace std {
  template <class Container>
  back_insert_iterator<Container> back_inserter(Container& x);           // (1) C++03

  template <class Container>
  constexpr back_insert_iterator<Container> back_inserter(Container& x); // (1) C++20
}
  • back_insert_iterator[link back_insert_iterator.md]

概要

back_insert_iteratorのヘルパ関数。

戻り値

back_insert_iterator<Container>(x)
  • back_insert_iterator[link back_insert_iterator.md]

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
  std::vector<int> src = {1, 2, 3};
  std::vector<int> dest;

  // srcの要素をdestに追加しながらコピー
  dest.reserve(src.size());
  std::copy(src.begin(), src.end(), std::back_inserter(dest));

  for (int x : dest) {
    std::cout << x << std::endl;
  }
}
  • std::back_inserter[color ff0000]
  • dest.reserve[link /reference/vector/vector/reserve.md]
  • src.size()[link /reference/vector/vector/size.md]
  • src.begin()[link /reference/vector/vector/begin.md]
  • src.end()[link /reference/vector/vector/end.md]

出力

1
2
3

参照