我试图在不使用resize()方法的情况下尝试调整前向列表的大小
我想尝试逆转向前_list(没有反向()方法),但我不确定如何在不使用resize()的情况下摆脱erase_after()方法之后的最后一个元素。
#include<forward_list>
template<typename vt>
class reverse_forward_list{
public:
static void reverse(std::forward_list<vt>& list){
typename std::forward_list<vt>::iterator iter = list.begin();
int x = 0;
for(vt& i :list){
list.emplace_front(i);
x++;
}
list.erase_after(iter,list.end());
//list.resize(x);
}
};
任何帮助都将受到赞赏
I want to try reversing a forward_list (without the reverse() method) but I'm not sure how to get rid of the last element after the erase_after() method without using resize().
#include<forward_list>
template<typename vt>
class reverse_forward_list{
public:
static void reverse(std::forward_list<vt>& list){
typename std::forward_list<vt>::iterator iter = list.begin();
int x = 0;
for(vt& i :list){
list.emplace_front(i);
x++;
}
list.erase_after(iter,list.end());
//list.resize(x);
}
};
Any help is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
list.before_begin()
还要删除先前的第一个元素。
另外,这可以通过 splicing 每个节点每个节点列表的正面(这可能是
反向
在内部工作的方式,您不必分配新节点/构造新值)Use
list.before_begin()
instead to also erase the previous first element.Also, this can be implemented more efficiently by splicing each node to the front of the list (which is probably how
reverse
works internally, and you won't have to allocate new nodes/construct new values)恕我直言,应该有一个充分的理由来操纵清单,同时穿越它……这是一场等待发生的灾难。
相反,请考虑使用临时列表,类似列表:
IMHO there should be a VERY good reason for manipulating a list while traversing it... It's a disaster waiting to happen.
Instead, consider using a temporary list, something like that: