Assign list to multiset

Hello. What's the right way to assign list to multiset.

1
2
list<int>randoms;
multiset<int>freq; //empty 

Static_cast won't work?
You mean you want to add all elements in the list to the multiset?

You can do it by passing begin and end iterators of the list to the multiset constructor.
 
multiset<int> freq(randoms.begin(), randoms.end());

Or if you want to do it after you have already created the multiset you can do the same using the insert function.
 
freq.insert(randoms.begin(), randoms.end());
Last edited on
Thanks.
Is there any way I could make this work:
1
2
3
4
5
for (auto &temp: freq)
	{
		cout << temp << " appears " << freq.count(temp);
		freq.erase(temp);
	}

I get
set iterator is not incrementable
Calling erase while iterating over the multiset is a bit tricky. I recommend you find a way so that you don't have to call erase, or a totally different way where you don't have to use multiset (hint: you can do what you're trying to do very easily using std::map).
Last edited on
Calling erase while iterating over the multiset is a bit tricky.

I could probably do this with while (!freq.empty()). But I don't think this is the problem.
Topic archived. No new replies allowed.