对set进行如下测试:
在set容器中依次插入8 7 6 5 4 3 2 1,set容器会对数据进行排序为 1 2 3 4 5 6 7 8, 但是在调试时发现显示的数据时而不正确,最后通过打印发现顺序其实是正确的。但是原因未知…


测试代码如下:
#include <termio.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <set>
int main(int argc, char *argv[]) {
std::set<int> test_set;
for (int i = 1; i < 9; ++i) {
test_set.insert( 9 - i);
}
std::set<int>::const_iterator it = test_set.begin();
for(; it!=test_set.end();++it) {
std::cout << *it;
}
}
本文探讨了在C++中使用set容器进行数据排序的过程,通过具体实例展示了如何将一组逆序数字插入set容器,并观察其自动排序至升序的功能。尽管在调试过程中遇到了显示不一致的问题,但最终确认数据排序是正确的。本文提供了完整的测试代码,有助于理解set容器的工作原理。
1011

被折叠的 条评论
为什么被折叠?



