1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <set>
int main ()
{
int myints[] = {42,71,1,71,5,2,7,8,1,2,1,3,3,3,3,1,71,71,12};
auto size = sizeof(myints)/sizeof(int);
//SETUP MULTISET
std::multiset<int> mymultiset (myints, myints + size);
auto count = mymultiset.count(0);
// DISPLAY ARRAY CONTENTS
std::cout << " myints contains:";
for (auto i = 0; i < size; i++)
std::cout << ' ' << myints[i];
std::cout << '\n';
// DISPLAY MULTISET CONTENTS
std::cout << "mymultiset contains:";
for (auto it = mymultiset.begin(); it != mymultiset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
// DISPLAY KEY vs COUNT FOR EACH ELEMENT
for (auto it = mymultiset.begin(); it != mymultiset.end(); ++it)
{
count = mymultiset.count(*it);
// KEYS WITH ODD COUNT
if(count % 2 == 1)
std::cout << " ODD Key: " << *it << " has " << count << " entries\n";
else
std::cout << "EVEN Key: " << *it << " has " << count << " entries\n";
for( auto jump = 0; jump < count - 1 ; jump++)
it++;
}
std::cout << '\n';
return 0;
}
|