c++中指针列表的最后一个元素
Last Element from List of pointers in c++
这是一段简单的代码,它给了我错误的输出,但我不知道为什么。
#include <iostream>
#include <list>
using namespace std;
void main(){
list<int*> l;
int x = 7;
int* y = &x;
//it works if I put list<int*> l; on this line instead.
l.push_back(y);
cout << **l.end() << endl; // not 7
}
我该如何修复它?
.end()
返回一个指向列表容器中过尾元素的迭代器。past-the-end元素是理论上的元素,它位于列表容器中最后一个元素之后。它不指向任何元素,因此不能被解引用。
使用front
或back
成员函数
cout << *l.front() << endl;
cout << *l.back() << endl;
查看此链接
相关文章:
- lower_bound()返回最后一个元素
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 仅显示链表的最后一个元素
- push_back通过自行创建的对象获取最后一个元素的向量
- 使用 map.end() 访问 map 的最后一个元素
- 如何知道地图中的最后一个元素是否被删除?
- std::矢量保存 只推送最后一个元素
- 反转后不返回最后一个元素
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- 在 QListView 中显示最后一个元素
- C++:在进行切片时对迭代器的约定,特别是对于访问最后一个元素并最终将其删除
- 遍历列表包含排除最后一个元素的内容
- 如何打印第一个和最后一个元素的和,然后打印第二个和倒数第二个元素的总和,依此类推
- C++链表最后一个元素的迭代器?
- 这种获取模板参数包中最后一个元素的方法是否有隐藏的开销?
- 使用 STL 交换堆栈 C++ 中的第一个和最后一个元素
- 数组:如何在指定索引之前显示数组的第一个和最后一个元素以及数组元素的差异?
- 你能pop_back一个向量,并且仍然使用迭代器到最后一个元素吗?
- 如何使用第一个、中间和最后一个元素的中位数正确分区?