关于C++插入迭代器(插入器)的那些事

插入器是一种迭代器适配器,它接受一个容器,生成一个迭代器,能实现向给定容器添加元素。其实适配器的本质就是实现不变,改变接口。例如容器适配器可以使一个容器vector表现得像一个stack一样,这里的迭代器适配器也是如此。


插入器有三种类型,差异在于元素插入的位置:

  • back_inserter 创建一个使用push_back的迭代器。
  • front_inserter 创建一个使用push_front 的迭代器。
  • inserter 创建一个使用insert的迭代器。

假设iter是一个插入迭代器,则

  • iter = val;
    在iter指定的当前位置插入值val。假定c是it绑定的容器,依赖于插入迭代器的不同种类,此赋值会分别调用c.push_back(val)、c.push_front(val)或c.inserter(iter,pos),其中pos为传递给inserter的迭代器位置
  • *iter,++iter,iter++
    这些操作虽然存在,但不会对iter做任何事情,每个操作都返回iter

接下来将结合代码具体阐述:

ostream &print(const std::list<int> &ilst, ostream &out) {
	for (const auto &i : ilst) {
		out << i << " ";
	}
	return out;
}
	std::list<int> lst{ 1,2,3,4 };
	std::list<int> ilst,lst2, lst3;
	auto iter_back = std::back_inserter(lst2);
	auto iter_front = std::front_inserter(lst3);
	auto iter = std::inserter(ilst, ilst.begin());


	iter_back = 3;  //等价于lst2.push_back(3);
	iter_back = 5;	//等价于lst2.push_back(5);

	iter_front = 2; //等价于lst3.push_back(2);
	iter_front = 4; //等价于lst3.push_back(4);

	//注意这里注释里的ilst.begin()始终和定义iter传递的迭代器一致,并不是实时的ilst的首迭代器
	iter = 3;		//等价于ilst.insert(ilst.begin(),3)
	iter = 6;		//等价于ilst.insert(ilst.begin(),6)
	iter = 9;		//等价于ilst.insert(ilst.begin(),9)
	
	print(lst2, cout) << endl;  //输出3 5
	print(lst3, cout) << endl;	//输出4 2
	print(ilst, cout) << endl;	//输出3 6 9

这里提出一点点关于标准库copy函数的小坑
copy算法并不检查目的序列,所以使用前务必保证目的序列大小不小于输入序列

	//copy算法并不检查目的序列,所以使用前务必保证目的序列大小不小于输入序列
	lst2.resize(lst.size()); lst3.resize(lst.size()); ilst.resize(lst.size());
	std::copy(lst.begin(),lst.end(), lst2.begin());
	std::copy(lst.begin(), lst.end(), lst3.begin());
	std::copy(lst.begin(), lst.end(), ilst.begin());
	print(lst2, cout) << endl;
	print(lst3, cout) << endl;
	print(ilst, cout) << endl;

附上标准库std::copy函数的源码:

template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, 
              OutputIt d_first)
{
    while (first != last) {
        *d_first++ = *first++;
    }
    return d_first;
}

重点来了,使用插入器则不用考虑目的序列与输入序列的大小关系:

	//使用插入迭代器则对目的序列无要求
	std::copy(lst.begin(), lst.end(), iter_back);
	std::copy(lst.begin(), lst.end(), iter_front);
	std::copy(lst.begin(), lst.end(), iter);
	print(lst2, cout) << endl;	//输出1 2 3 4
	print(lst3, cout) << endl;	//输出4 3 2 1
	print(ilst, cout) << endl;	//输出1 2 3 4

tips:
只有在容器支持push_front的情况下,才能使用front_inserter。同样,只有容器支持push_back的情况下,才能使用back_inserter。


以上就是本文的全部内容了,如果觉得有帮助可以点个赞吖~
乖巧

原创文章 7 获赞 7 访问量 342

猜你喜欢

转载自blog.csdn.net/Love_Point/article/details/105540571
今日推荐