一 简介
1. std::inserter
template< class Container >
std::insert_iterator<Container> inserter( Container& c,
typename Container::iterator i ); (until C++20)
template< class Container >
constexpr std::insert_iterator<Container> inserter( Container& c,
ranges::iterator_t<Container> i );(since C++20)
构建一个容器 c 的 std::insert_iterator。c 需要支持 insert 操作。
2. std::front_inserter
template< class Container >
std::front_insert_iterator<Container> front_inserter( Container& c ); (until C++20)
template< class Container >
constexpr std::front_insert_iterator<Container> front_inserter( Container& c ); (since C++20)
构建一个容器 c 的 std::front_insert_iterator。c 需要支持 push_front 操作。
3. std::back_inserter
template< class Container >
std::back_insert_iterator<Container> back_inserter( Container& c ); (until C++20)
template< class Container >
constexpr std::back_insert_iterator<Container> back_inserter( Container& c ); (since C++20)
构建一个容器 c 的 std::back_insert_iterator。c 需要支持 push_back 操作。
4. std::make_reverse_iterator (C++14)
template <class Iter>
std::reverse_iterator<Iter> make_reverse_iterator( Iter i ); (since C++14) (until C++17)
template <class Iter>
constexpr std::reverse_iterator<Iter> make_reverse_iterator( Iter i ); (since C++17)
构建一个迭代器 i 的 std::reverse_iterator。
5. std::make_move_iterator (C++11)
template< class Iter >
std::move_iterator<Iter> make_move_iterator( const Iter& i ); (since C++11) (until C++14)
template< class Iter >
std::move_iterator<Iter> make_move_iterator( Iter i ); (since C++14) (until C++17)
template< class Iter >
constexpr std::move_iterator<Iter> make_move_iterator( Iter i ); (since C++17)
构建一个迭代器 i 的 std::move_iterator。
二 例子
#include <iterator>
#include <algorithm> // copy
#include <iostream>
#include <vector>
#include <list>
#include <string>
template<typename T>
void print(const T& vc, const std::string& pre="")
{
std::cout << pre;
for (auto i : vc) {
std::cout << i << " ";
}
std::cout << std::endl;
}
int main() {
std::vector<int> vc{1,2,3,4,5};
{
// std::inserter
std::vector<int> vc1;
std::copy(vc.begin(), vc.end(), std::inserter(vc1, vc1.begin()));
print(vc1, "vc1: ");
}
{
// std::front_inserter
std::list<int> vc2;
std::copy(vc.begin(), vc.end(), std::front_inserter(vc2));
print(vc2, "vc2: ");
}
{
// std::back_inserter
std::vector<int> vc3;
std::copy(vc.begin(), vc.end(), std::back_inserter(vc3));
print(vc3, "vc3: ");
}
{
// std::make_reverse_iterator
std::vector<int> vc4;
std::copy(std::make_reverse_iterator(vc.end()),
std::make_reverse_iterator(vc.begin()), std::back_inserter(vc4));
print(vc4, "vc4: ");
}
{
// std::make_move_iterator
std::cout << std::endl << "std::make_move_iterator Demo: " << std::endl;
std::vector<std::string> vs {"one", "two", "three"};
print(vs, "vs: ");
std::vector<std::string> vc5(std::make_move_iterator(vs.begin()), std::make_move_iterator(vs.end()));
print(vc5, "vc5: ");
print(vs, "vs: ");
}
std::cin.get();
return 0;
}
三 参考