使用std :: set的.begin()和.end()函数会产生意外的结果

fdgklatkljfdsaf

我有以下简单程序:

#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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在WHERE子句中混合使用AND和OR会产生意外结果

R,使用dplyr :: mutate和ifelse包含grepl()会产生意外结果

如何使用自定义.begin()和.end()循环std :: set?

使用 "%" 操作会产生意外结果

如果无法修改set元素,为什么std :: set提供begin()和end()

std :: begin和std :: end无法使用指针并引用原因?

从函数中调用malloc和realloc会产生意外结果

使用goroutine进行迭代会产生意外结果

MySQL-使用LEFT JOIN会产生意外结果

尝试在别名中使用`pwd`会产生意外结果

为什么在std :: string上使用Sizeof运算符会产生意外结果?

是否可以使用std库的find,begin和end编写in_array helper函数?

为非数组重载std :: begin()和std :: end()

std :: begin和std :: end的const重载的目的是什么?

使用Repast Simphony产生意外结果

在 SQL 查询中使用“true”而不是 true 会产生意外结果

使用np.savez存储字典会产生意外结果吗?

使用TimePeriod.NET的CalendarPeriodCollector计算“工作时间”会产生意外结果

使用char **指针的字符串操作代码会产生意外结果

使用python进行二进制读取会产生意外结果

使用相同的“for”循环和“all()”函数会产生不同的结果

R计算股票的beta(使用PerformanceAnalytics CAPM.beta()函数或lm()函数产生意外结果)

为什么std :: variant使用begin和end迭代器进行编译?

Bash脚本和大文件(错误):使用重定向内置的读取内容输入会产生意外结果

vector :: begin()和std :: begin()之间的区别

为什么STL为std :: container :: begin和std :: container :: end函数提供const重载?

如果我使用vector :: begin()而不是std :: back_inserter(vector)作为set_intersection的输出会怎样?

使用 ST_Disjoint() 函数会产生意想不到的结果

在VB.NET中以给定大小和X / Y POS绘制圆会产生意外结果