std::back_insert_iterator::operator=

来自cppreference.com
(1)
back_insert_iterator<Container>&
    operator=( typename Container::const_reference value );
(C++11 前)
back_insert_iterator<Container>&
    operator=( const typename Container::value_type& value );
(C++11 起)
back_insert_iterator<Container>&
    operator=( typename Container::value_type&& value );
(2) (C++11 起)

插入给定的 value 到迭代器。

1) 导致 container->push_back(value)
2) 导致 container->push_back(std::move(value))

目录

[编辑] 参数

value - 要插入的值

[编辑] 返回值

*this


[编辑] 示例

[编辑] 示例

#include <iostream>
#include <iterator>
#include <vector>
 
void print(const std::vector<int>& vec)
{
    std::cout << "[";
    for (auto& elem : vec)
        std::cout << " " << elem;
    std::cout << " ]\n";
}
 
int main()
{
    std::vector<int> vec;
    std::back_insert_iterator< std::vector<int> > it(vec);
 
    for (int i=0; i<10; ++i)
        it = i; // 调用 vec.push_back(i)
 
    print(vec);
}

输出:

[ 0 1 2 3 4 5 6 7 8 9 ]