creating a map from two vectors

前端 未结 5 1093
旧时难觅i
旧时难觅i 2020-12-17 23:32

If I have two stl vectors vect1, vect2 and I want to produce from them a map, so first element from vect1 will correspond to first element in vect2 and so on. How can I do t

5条回答
  •  自闭症患者
    2020-12-17 23:58

    assuming, you are going to ignore the extra (size of vect1 != size of vect2), this could be a solution:

    map target; //vector vect1, vector vect2;
    vector::iterator it1 = vect1.begin();
    vector::iterator it2 = vect2.begin();
    while(it1 != vect1.end() && it2 != vect2.end())
    {
    target.insert(std::make_pair(*it1, *it2));
    it1++;it2++;
    }
    

    EDIT : Thanks Nim for pointing out *it1 thing.

提交回复
热议问题