使用 std::set 的 .begin() 和 .end() 函数会产生意想不到的结果
Using std::set's .begin() and .end() function produces unexpected results
我有以下简单的程序:
#include <iostream>
#include <set>
using namespace std;
int main()
{
unsigned int qwer = 6132;
unsigned int ty = 3512;
unsigned int vv = 4331;
unsigned int gg = 1337;
set<unsigned int> asdf = {};
asdf.insert(qwer);
asdf.insert(ty);
asdf.insert(vv);
asdf.insert(gg);
cout << "&asdf.begin() = " << &asdf.begin();
unsigned int setint = *asdf.begin();
cout << "nsetint = " << setint;
setint = *asdf.end();
cout << "nsetint = " << setint;
cout << "n&asdf.end() = " << &asdf.end();
return 0;
}
它产生以下输出:
&asdf.begin() = 0x22fe08
setint = 1337
setint = 4
&asdf.end() = 0x22fe08
为什么asdf.begin()
和asdf.end()
的地址匹配?我假设他们会有不同的地址指向不同的值?尽管它们的地址确实匹配,但指向的值不匹配!这是为什么呢?
编辑:还有为什么setint = asdf.end((似乎将setint的值设置为集合中的元素数量而不是集合中的最后一个值?(我假设应该是6132吧?
你有很多未定义的行为。
&asdf.begin()
&asdf.end()
您正在获取 prvalue 的地址。 &
只能应用于左值和限定 ids(有名称的事物(
*asdf.end()
迭代器end
不可取消引用。它指向"一过即止"的立场。
相关文章:
- 为什么在递归中使用循环会产生意想不到的结果?
- 字符到int8_t转换会产生意想不到的结果?
- libc++ 对 std::map/set::equal_range 的实现给出了意想不到的结果
- 微小加密算法实现会产生意想不到的结果
- 使用 std::set 的 .begin() 和 .end() 函数会产生意想不到的结果
- 在我的C++链表实现中取消引用节点指针,给出意想不到的结果
- C++正则表达式Visual Studio Community 2015给出<regex>意想不到的结果
- istringstream int8_t产生意想不到的结果
- 划分 OpenCV 垫会产生意想不到的结果
- 提升精神,提升任何意想不到的结果
- regex_match给出意想不到的结果
- 从 PASCAL 到 C++ 的代码转换给出了意想不到的结果
- 意想不到的结果c++
- 在lambda上使用条件运算符调用std::any_of会得到意想不到的结果
- vector::insert在VS2010中执行意想不到的结果
- list resize会产生意想不到的结果
- CUDA: 2D数组索引产生意想不到的结果
- 将字符串转换为整数会产生意想不到的结果
- 在c++中,一些宏语句可能会产生意想不到的结果
- Visual Studio可变宏展开会产生意想不到的结果