通过迭代器来获得对应元素的下标╮(╯▽╰)╭ ~

来源:互联网 发布:细菌觅食算法 编辑:程序博客网 时间:2024/06/01 10:26
#include <iostream>#include <cstdio>#include <vector>#include <map>#include <set>#include <string>#include <cstring>#include <cstdlib>#include <cmath>#include <utility>#include <algorithm>using namespace std;map<int, int> m;map<int, int> :: iterator it, firs, e;vector <int> v;int a[15];int main(){    for (int i=0; i<=10; i++)    {        a[i] = i;        v.push_back (i);        m[i]++;    }    for (it=m.begin(); it!=m.end(); it++)    {        cout << it->first << " " << it->second << endl;    }    firs = m.begin();    e = m.end();    cout << distance(firs, e) << " ~~ " << a[distance(firs, e)-1] << " ~~ " << a[(int)m.size()-1]<< endl;    //高能预警:如果下面一行第二个输出是a[distance(v.begin(), v.end())-1]就对啦    cout << distance(v.begin(), v.end())  << " ~~ " << a[distance(v.begin(), v.end())] << endl;     return 0;}

0 0