我有一个<code>std::set<;std::字符串>,我想知道插入后元素在集合中的确切位置。
我尝试了std::distance
,但没有任何运气:
#include <iostream>
#include <string>
#include <set>
#include <iterator>
using namespace std;
int main (int argc, char const *argv[])
{
string array[] = { "zero", "one", "one", "zero", "two", "three", "zero" };
set<string> numbers;
for(size_t i = 0; i < 7; ++i)
{
int dist = distance(numbers.begin(), numbers.insert(array[i]).first);
cout << array[i] << " " << dist << endl;
}
return 0;
}
输出:
zero 0
one 0
one 0
zero 1
two 1
three 1
zero 3
相反,我期待的是:
zero 0
one 1
one 1
zero 0
two 2
three 3
zero 0
有什么想法吗?