+ 3

What am I doing wrong?

I've missed the boat on multiset. how would one insert 'char' key values for a multiset, rather than standard key values as index for value values...(key,value) #include <iostream> #include <vector> #include <set> #include <cstdlib> #include <algorithm> #include <iterator> int main(){ std::vector<int> v(3200,rand()%100); std::multiset<char,int> mset; std::multiset<char,int>::iterator msetIt; std::cout<<"copied vector v to multiset mset: "<<std::endl; cpyVecToMSet(mset); for(msetIt;msetIt != mset.end();msetIt++){ std::cout<<*msetIt<<" "; }; return 0; } std::multiset<char,int> cpyVecToMSet(std::vector<int> &vect){ std::multiset<char, int> tmpMSet(vect.begin(),vect.end()); return tmpMSet; };

5th Nov 2018, 11:32 PM
Brandon Autry
Brandon Autry - avatar
2 Réponses
+ 9
I believe you are either looking for std::multimap, or trying to place std::pair within std::multiset. I've looked into the documentation of std::multiset - The declaration does not appear to allow you to map a key to a value. The second template argument is Compare, which tells the container how to sort the multiset. As a result, std::multiset<char, int> probably does not make sense. You can try using std::multiset<std::pair<char,int>> obj; //obj.insert(std::make_pair('a', 1)); //obj.emplace('a', 1); multiset<pair> example -> https://www.geeksforgeeks.org/multisetemplace-c-stl/ or std::multimap<char,int> obj; multimap ref -> http://www.cplusplus.com/reference/map/multimap/begin/
6th Nov 2018, 3:13 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
that completely answers my question. I have looked and looked for a way to make that work and this confirms I'll have to read more :)
6th Nov 2018, 3:38 AM
Brandon Autry
Brandon Autry - avatar