- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
运行下面的代码,我希望看到测试 1 的第 4 个值的不同地址(与值 1 和 3 不同)。这表明 rend() 与 rbegin() 相同??我也不希望循环经历第二次迭代并出现段错误。
知道我做错了什么吗?
map<unsigned long, int*> newMap;
newMap[13] = new int(1340);
cout << "test 1:" << endl;
cout << &(*(newMap.begin())) << endl;
cout << &(*(newMap.end())) << endl;
cout << &(*(newMap.rbegin())) << endl;
cout << &(*(newMap.rend())) << endl;
cout << "test 2:" << endl;
for(map<unsigned long, int*>::reverse_iterator it=newMap.rbegin();it!=newMap.rend();){
cout << "next iteration" << endl;
map<unsigned long, int*>::reverse_iterator entry = it;
it++;
delete entry->second;
newMap.erase(entry->first);
}
输出:
test 1:
0x2299050
0x7fffcd574908
0x2299050
0x2299050
test 2:
next iteration
next iteration
*** glibc detected *** ./foo: double free or corruption (fasttop): 0x0000000002299030 ***
编辑:这是一个更新/简化的版本,仍然存在同样的问题(仅限测试 2);这不会导致段错误,因为它没有使用指针,但仍会经历两次循环:
map<int, int> newMap;
newMap[13] = 1340;
cout << "test 2:" << endl;
for(map<int, int>::reverse_iterator it=newMap.rbegin();it!=newMap.rend();){
cout << "next iteration" << endl;
int index = it->first;
int value = it->second;
it++;
newMap.erase(index);
}
最佳答案
在测试 1 中,您取消引用尾后迭代器。正如 ArchbishopOfBanterbury 在评论中提到的,其结果是不确定的。
在测试 2 中,您通过删除 it.base()
指向的元素使 it
无效。 std::reverse_iterator
持有指向下一个元素的迭代器(因此 newMap.rbegin()
持有等于 newMap.end()
和 newMap.rend()
的迭代器> 持有等于 newMap.begin()
的迭代器。
当您递增it
时,它等于newMap.rend()
,这意味着it.base()
等于newMap.begin()
。然后删除 it.base()
引用的元素,这会使 it.base()
无效,从而使 it
也无效。由于 it
现在无效,因此将它与 newMap.rend()
进行比较不起作用。
关于C++ rend() 指向与 rbegin() 相同的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38984745/
下面函数中rbegin()和rend()判断输入是否回文的具体作用是什么? bool palindromeCheck(string input) { if (input == string(i
运行下面的代码,我希望看到测试 1 的第 4 个值的不同地址(与值 1 和 3 不同)。这表明 rend() 与 rbegin() 相同??我也不希望循环经历第二次迭代并出现段错误。 知道我做错了什么
我在迭代器中有一个关于 begin() 之间的区别的问题和 rend() . #include #include #include #include using namespace std;
根据 C++ 规范 (23.2.4.3),vector::erase() 仅使“删除点之后的所有迭代器和引用”无效 因此,当使用 reverse_iterators 传递所有 vector 成员时,当
使用 XCode 4.6 在 Mac OS X 上测试。 此示例代码显示删除 std::list 的最后一个元素如我所料:对 list::end() 的迭代器引用仍然是“1过去了”并且仍然有效,即使删
我正在学习 Khronos Vulkan API,但我很难跨多个线程进行同步。 我试图将渲染工作分解到它自己的线程中。我的想法是有一个主线程,它将负责高级应用程序管理,重要的是,它将调用 VkQueu
我正在尝试反向迭代 std::map,遵循以下代码:http://www.cplusplus.com/reference/stl/map/rend/它说: rend() returns a rever
这不是如何将 reverse_iterator 转换为 iterator 的重复,因为我希望结果与正常转换不同。 只给定一个从 rend 返回的 reverse_iterator ,是否可以将其转换为
既然在 C++11 修订版中为标准包含添加了非成员 begin() 和 end() 函数,为什么会有非成员版本的 rbegin() 和 rend() 函数也没有添加?开始使用begin()和end()
我在 Vue 单文件组件中有一个选项卡结构,其中使用 use 组件内的 svg Sprite 和动态变量加载图标。 结果在 Chrome 和 Safari 中显示为空白。 SVG 不会呈现,即使
下面是我的 String 类的代码。我想实现 reverse_iterator 和 rbegin() 和 rend() 方法。已粘贴分配方法的代码。 String::reverse_iterator
我正在使用 boost::tokenizer,但我意识到它不支持 rbegin() 和 rend()。我想请问如何将这两个功能添加到现有类中? 这是来自 boost 网站: #include #in
对于 std::set 和 std::map 等以对数时间查找的数据类型,实现是否需要维护开始和结束迭代器?访问 begin 和 end 是否意味着查找可能以对数时间发生? 我一直假设开始和结束总是在
我一直在 MSVC 2013 中使用 std::rbegin 和 std::rend。当我尝试使用 GCC 4.9.1 或 clang 3.5.0 编译我的代码时,两者都告诉我“rbegin”和“re
代码 A: vector::const_reverse_iterator rcit; vector::const_reverse_iterator tit=v.rend(); for(rcit = v
我写了一些采用迭代器但必须以相反顺序进行比较的代码, template bool func(ConstBiIter seq_begin, ConstBiIter seq_end) { Cons
我是一名优秀的程序员,十分优秀!