我有以下简单程序:
#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的地址。&
只能应用于左值和qualified-id(带有名称的事物)
*asdf.end()
该end
迭代器不dereferencable。它指向“最后一个”位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句