首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >从列表C++创建队列

从列表C++创建队列
EN

Stack Overflow用户
提问于 2020-11-09 17:35:55
回答 1查看 78关注 0票数 0

非常简单的任务,但我有一些错误,我不明白。任务是创建一个函数,它将从列表中返回队列(使用STL)

代码:

代码语言:javascript
运行
AI代码解释
复制
#include <iostream>
#include <list>
#include <queue>
using namespace std;

queue<int> reverse(const list<int>& data) noexcept {
    queue <int> Queue;
    for (list<int>::reverse_iterator i = data.rbegin(); i != data.rend(); i++) {
        Queue.push(*i);
    }
    return Queue; 
}

错误:

代码语言:javascript
运行
AI代码解释
复制
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\xutility(1746,1): error C2664: 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>::_List_iterator(const std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>> &)': cannot convert argument 1 from '_BidIt' to 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>> &&'

1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\xutility(1746,96): message: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-09 17:52:32

dataconst list<int>&。您应该使用const_reverse_iterator

代码语言:javascript
运行
AI代码解释
复制
queue<int> reverse(const list<int>& data) noexcept
{
    queue <int> Queue;
    for (list<int>::const_reverse_iterator i = data.crbegin(); i != data.crend(); i++)
    {
        Queue.push(*i);
    }
    return Queue; 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64756478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文