c++ - 对 C++ 列表的基于索引的访问
问题描述
出于多种原因,我需要使用 C++ 列表(不是向量),但需要对元素进行基于索引的访问。
我想出了这样的事情:
point* point1i = std::next(listPoints.begin(), i);
point* point2i = std::next(listPoints.begin(), i + 1);
wherepoint
是在别处声明的类并且i
是整数。
但是当我编译我得到这个错误:
error: cannot convert ‘std::_List_iterator<point*>’ to ‘point*’ in initialization
我在这里做错了什么?
谢谢!
解决方案
listPoints.begin()
并std::next
处理迭代器。由于您似乎有一个std::list<point *>
并且需要该元素,因此只需取消引用所述迭代器:
point* point1i = *std::next(listPoints.begin(), i);
point* point2i = *std::next(listPoints.begin(), i + 1);
推荐阅读
- powershell - 在搜索中包含评论
- docker - Docker Swarm:在 Worker 节点上部署容器
- javascript - React Router Redux 总是显示第一条路由
- javascript - 数据库数组没有插入我的数据库有什么问题?
- javascript - 在js中添加带有编辑文本的新表格
- javascript - 如何删除 TypeScript 类型注释但保留格式?
- php - 先前插入的值在页面刷新时自动插入
- node.js - 节点模块 - aes256 - crypto.createHash 不是函数
- spring-boot - 使用表单数据上传 Spring Boot 文件?
- rest - 空响应和未找到响应的 HTTP 状态码