multiset

Pages: 12
Dec 12, 2016 at 4:54pm
hello guys, how can i find how many times appears multisets each elements?
Last edited on Dec 12, 2016 at 4:54pm
Dec 12, 2016 at 5:10pm
Dec 12, 2016 at 7:59pm
it counts a specific elements, i want to count each of them
Dec 12, 2016 at 8:26pm
closed account (E0p9LyTq)
it counts a specific elements, i want to count each of them


Then perform multiple count searches, one for each separate value you haven't previously searched for.
Dec 12, 2016 at 8:30pm
closed account (E0p9LyTq)
if you want to know the total number of elements in your multiset, use the size() method.
Dec 12, 2016 at 9:00pm
someone show me algorithm please :/
Dec 13, 2016 at 2:03am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <set>

int main ()
{
    int myints[]={10,73,12,22,73,73,12};
    std::multiset<int> mymultiset (myints,myints+7);

    std::multiset<int>::const_iterator itr = mymultiset.begin();
    do
    {
      std::cout << mymultiset.count(*(itr)) << " ";
      itr++;
    } while (itr != mymultiset.end());
}
Dec 13, 2016 at 6:12pm
your program output is 1 2 2 1 3 3 3, but i want something like that 1,2,1,3 because we have four number : 10 12 22 and 73
Dec 13, 2016 at 6:46pm
I don't know how familiar with C++11 you are, if there's anything that doesn't make sense give a shout:
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
#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>
#include <string>

using namespace std;

int main()
{
    typedef  multiset<int> mySet;
    typedef multiset<int>::iterator mySetItr;

    int myints[]={10,73,12,22,73,73,12};
     mySet mNums (myints,myints+7);

    mySetItr it = begin(mNums), itend = end(mNums);
    for_each<mySetItr&>(it, itend, [&mNums, &it] (const int& num)
    {
        auto p = mNums.equal_range(num);
        int counter = static_cast<int>(distance(p.first, p.second));
        cout << num << " " << counter << '\n';
        advance(it, counter - 1);
    });
}

Dec 13, 2016 at 9:57pm
my real problem is that when i input the number sequence i want to output numbers which appears odd times (sorry for my English) for example: if i input sequence: 1 2 4 1 2 3 the program must output: 3 4 because they appeared 1 time in my sequence and 1-is odd
Dec 13, 2016 at 9:58pm
and what i could do is

#include <bits/stdc++.h>
using namespace std;
int main()
{ int a,n;


multiset<int>m;
set<int>d;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a;
m.insert(a);
}

for(auto j=m.begin();j!=m.end();j++)

{ if(m.count(*(j))&1);
d.insert(*j);
}
for(auto t=d.begin();t!=d.end();t++) {
if(m.count(*(t))&1 ) cout<<*t<<endl;
}
}
Dec 13, 2016 at 10:00pm
i know that it is not a good version and i want to help me to optimize it, n-is how many numbers contain my sequnece and a-is that numbers
Dec 13, 2016 at 10:03pm
instead of ((*j)%2==0) i am using (*(j)&1) its same but i think its easy to use bitwise operator
Last edited on Dec 13, 2016 at 10:03pm
Dec 13, 2016 at 10:40pm
The problem here is that you're iterating over every element of the set, while you need to be jumping forward to the next distinct element.
So instead of
1
2
for(auto j=m.begin();j!=m.end();j++)
{

you want something more like
1
2
for(auto j = m.begin(); j != m.end(); j = m.upper_bound(*j))
{

btw, if(m.count(*(j))&1); does nothing, mind the semicolon
Dec 14, 2016 at 4:19am
closed account (48T7M4Gy)
hello guys, how can i find how many times appears multisets each elements?

i.e. how many times does each separate value in a multiset appear?

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;
}


   myints contains: 42 71 1 71 5 2 7 8 1 2 1 3 3 3 3 1 71 71 12
mymultiset contains: 1 1 1 1 2 2 3 3 3 3 5 7 8 12 42 71 71 71 71
EVEN  Key: 1 has 4 entries
EVEN  Key: 2 has 2 entries
EVEN  Key: 3 has 4 entries
 ODD  Key: 5 has 1 entries
 ODD  Key: 7 has 1 entries
 ODD  Key: 8 has 1 entries
 ODD  Key: 12 has 1 entries
 ODD  Key: 42 has 1 entries
EVEN  Key: 71 has 4 entries

Program ended with exit code: 0
Last edited on Dec 14, 2016 at 6:13am
Dec 14, 2016 at 6:58am
#CUbbi- my program is working, but i want to optimize it
Dec 14, 2016 at 7:10am
closed account (48T7M4Gy)
@biwkina

Please show us your latest program version. Thankyou.
Dec 14, 2016 at 7:32am
#kemort - my real problem is that when i input the number sequence i want to output numbers which appears odd times (sorry for my English) for example: if i input sequence: 1 2 4 1 2 3 the program must output: 3 4 because they appeared 1 time in my sequence and 1-is odd
and here is my program
#include <bits/stdc++.h>
using namespace std;
int main()
{ int a,n;


multiset<int>m;
set<int>d;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a;
m.insert(a);
}

for(auto j=m.begin();j!=m.end();j++)

{ if(m.count(*(j))&1);
d.insert(*j);
}
for(auto t=d.begin();t!=d.end();t++) {
if(m.count(*(t))&1 ) cout<<*t<<endl;
}
}
Dec 14, 2016 at 7:52am
i changed your program and it works what i wanted

#include <iostream>
#include <set>
using namespace std;
int main ()
{
int myints,numbers;
cin>>myints;
multiset<int> mymultiset;
for (int i=0;i<myints;i++)
{
cin>>numbers;
mymultiset.insert(numbers);
}


auto count = mymultiset.count(0);

for (auto it = mymultiset.begin(); it != mymultiset.end(); ++it)
{
count = mymultiset.count(*it);


if(count % 2 == 1)
cout << *it <<endl;

for( auto jump = 0; jump < count - 1 ; jump++)
it++;
}
cout << '\n';
if(!(count&1)) cout<<0;
return 0;
}
Dec 14, 2016 at 7:58am
and i am interested in may i do this for string? if i input the following string: abcac i want to output b. any ideas?
Pages: 12