我已經偶然發現了這個問題:我似乎無法在正常的std :: set的索引位置選擇該項目。這是STD中的錯誤嗎?在一個std :: set的索引處的元素?
下面一個簡單的例子:
#include <iostream>
#include <set>
int main()
{
std::set<int> my_set;
my_set.insert(0x4A);
my_set.insert(0x4F);
my_set.insert(0x4B);
my_set.insert(0x45);
for (std::set<int>::iterator it=my_set.begin(); it!=my_set.end(); ++it)
std::cout << ' ' << char(*it); // ups the ordering
//int x = my_set[0]; // this causes a crash!
}
什麼我可以做些什麼來解決這個問題?
'my_set [0]'不應該編譯。 – chris
您在問錯誤的問題,因爲您使用的是錯誤的容器。每個標準容器的設計都考慮到了一定的用途,反過來又不允許其他用戶(直接)使用。所以,首先,你需要確定你需要什麼操作,然後[選擇合適的容器](http://stackoverflow.com/questions/10699265/how-can-i-efficiently-select-a-standard-library- container-in-c11/10701102#10701102) –
[從集合中的任意索引獲取元素](http:// stackoverflow。com/questions/8907435/get-element-from-arbitrary-index-in-set) –