problem with duplicates in multimap

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

problem with duplicates in multimap

Postby Zgugu Bamba » Sat Sep 17, 2011 2:34 am

HI,
maybe someone could help me understand how to handle duplicates in multimap correctly.
I have word1,word2 word3, and compare them with multimap studsubj.
word1,word2 word3 have same values as in studsubj, so if compared they should match,right?
Not in my case.The full match is when duplicate in studsubj is the second word, "WORK". That is if I compare
word_1 = "we" ( or "you" ), word_2 = "WORK", word_3 = "today" with studsubj, the match is perfect.

But if duplicate in studsubj is the first word ( "THEY") I have problems. The only match happens when I compare
record 3 ("THEY","do","work"), with word_1= "THEY", word_2 = "do", word_3 = "work".
If I change values of word_1,word_2 and word_3 to be same like in the other two records ("THEY") and compare the two,
I have no recognition. To be more exact, only word_1 has its match, but not word_2.

Is it because of comparison operator <?
Maybe it has to be changed to handle all duplicates, but i dont't know how.

My programming tool is Xcode and my computer is iMac, OS - Mac OS X 10.6.8.

Thank you very much!


#include <iostream>
#include <map>

#include <string>
using namespace std;

class student
{
private:
string element1;
string element2;
string element3;

public:
student( ) { element1 = element2 = element3 = " "; } // default constructor

student(string a,string b,string c):element1(a),element2(b),element3(c) {} // all elements filled
student(string a):element1(a) { element2 = element3 = " "; } // to compare element1

string e_1( ) { return element1; }
string e_2( ) { return element2; }
string e_3( ) { return element3; }
};

bool operator < (student a,student b) { return a.e_1() < b.e_1(); }

class subject
{
private:
string option;


public:
subject( ) { option =" "; }

subject(string a):option(a) {}

string str( ) { return option;}

};
typedef multimap<student,subject,less<student> > mapsub;


int main( )
{
typedef multimap<student,subject,less<student> > mapsub;
mapsub studsubj;
mapsub::const_iterator ptr;

/* 1 */ studsubj.insert(pair<student,subject>(student("we","WORK","today"), subject("option 3")));
/* 2 */ studsubj.insert(pair<student,subject>(student("you","WORK","today"), subject("option 4")));

/* 3 */ studsubj.insert(pair<student,subject>(student("THEY","do","work"), subject("option 1")));
/* 4 */ studsubj.insert(pair<student,subject>(student("THEY","read","books"), subject("option 1")));
/* 5 */ studsubj.insert(pair<student,subject>(student("THEY","work","today"), subject("option 1")));



string word_1="you",word_2 = "WORK",word_3 ="today"; // input words to сомраre with words from multimap studsubj

student st(word_1);
ptr = studsubj.find(st);
if(ptr != studsubj.end())
{


student st= ptr->first;
cout<<"st.e_1() match word_1 ( "<<word_1<<" )"<<endl;
do
{


if(st.e_2() == word_2 )
{
cout<<"st.e_2() match word_2 ( "<<word_2<<" )"<<endl;



if( st.e_3() == word_3 )
{

cout<<"st.e_3() match word_3 ( "<<word_3<<" )"<<endl;
break;
}


else
{

cout<<endl<<"word3 doesn't match word_3 ( "<<word_3<<" )"<<endl;
break;
}

} // if(st.e2() == word2 )
else
{
cout<<"st.e_2() doesn't match word_2 ( "<<word_2<<" )"<<endl;
break;
}


ptr++;
}
while(ptr != studsubj.upper_bound(word_2));
}

return 0;
}
Zgugu Bamba
 
Posts: 2
Joined: Sat Sep 17, 2011 2:06 am

Re: problem with duplicates in multimap

Postby Zgugu Bamba » Sun Sep 18, 2011 12:24 am

Here is he solution I got from my friend:
The loop is not iterating because you break from the loop when elements are not equal.
The temporary variable st is outside the loop and not being updated.
So it works fine.
Thanks to all!
Zgugu Bamba
 
Posts: 2
Joined: Sat Sep 17, 2011 2:06 am


Return to Algorithms & Data Structures

Who is online

Users browsing this forum: No registered users and 2 guests