如何用reverse_iterator插入 [英] How do you insert with a reverse_iterator

查看:81
本文介绍了如何用reverse_iterator插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++中插入一个STL列表,但我只有一个反向迭代器。

I want to insert something into a STL list in C++, but I only have a reverse iterator. What is the usual way to accomplish this?

这样做(当然是这样)

std::list<int> l;
std::list<int>::iterator forward = l.begin();
l.insert(forward, 5);

这不工作:(我该怎么办?)

This doesn't work: (what should I do instead?)

std::list<int> l;
std::list<int>::reverse_iterator reverse = l.rbegin();
l.insert(reverse, 10);


推荐答案

l.insert .base(),10); 将插入'10'在末尾,给定你的'reverse'迭代器的定义。实际上, l.rbegin()。base()== l.end()

l.insert(reverse.base(), 10); will insert '10' at the end, given your definition of the 'reverse' iterator. Actually, l.rbegin().base() == l.end().

这篇关于如何用reverse_iterator插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆