multimap problem

Zgugu Bamba's Avatar, Join Date: Sep 2011
Newbie Member
Maybe someone could help me understand what is wrong here. Below is a simple multimap studsubj, and I compare word_1,word_2 word_3 with it. I give these words the values exactly like in studsubj, then compare. For example, if word_1 = &quot;we&quot; ( or &quot;you&quot; ) ,word_2 = &quot;WORK&quot;, word_3 = &quot;today&quot; the match with studsubj is perfect. The problem is lines starting with &quot;THEY&quot;. To be more exact, the first line is ok, but comparing with any of the other two only has match with word_1, but not word_2 and word_3. I use iMac, OSX 10.6.8, programming tool - Xcode. Thank you very much. [code=C++] #include #include #include using namespace std; class student { private: string element1; string element2; string element3; public: student( ) { element1 = element2 = element3 = &quot; &quot;; } // 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 = &quot; &quot;; } // 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 =&quot; &quot;; } subject(string a)ption(a) {} string str( ) { return option;} }; typedef multimap mapsub; int main( ) { typedef multimap mapsub; mapsub studsubj; mapsub::const_iterator ptr; /* 1 */ studsubj.insert(pair(student(&quot;we&quot;,&quot; WORK&quot;,&quot;today&quot, subject(&quot;option 3&quot)); /* 2 */ studsubj.insert(pair(student(&quot;you&quot;,&quot ;WORK&quot;,&quot;today&quot, subject(&quot;option 4&quot)); /* 3 */ studsubj.insert(pair(student(&quot;THEY&quot;,&quo t;do&quot;,&quot;work&quot, subject(&quot;option 1&quot)); /* 4 */ studsubj.insert(pair(student(&quot;THEY&quot;,&quo t;read&quot;,&quot;books&quot, subject(&quot;option 1&quot)); /* 5 */ studsubj.insert(pair(student(&quot;THEY&quot;,&quo t;work&quot;,&quot;today&quot, subject(&quot;option 1&quot)); string word_1=&quot;we&quot;,word_2 = &quot;WORK&quot;,word_3 =&quot;today&quot;; // 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
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
That update got completely trashed. Try copying it in again, but copy and paste from plain text, not anything formatted. I don't know anything about Macs but you should have something similar to Notepad on there, copy it into that first, then copy+paste it into a reply to this text, and that should work.

The update isn't only unreadable as is, there is vital info missing. For example, #include what precisely? Whatever was in the angle brackets appears to have been stripped for some reason.
Zgugu Bamba's Avatar, Join Date: Sep 2011
Newbie Member
Hi,
maybe someone could help me understand what is wrong here.
Below is a simple multimap studsubj, and I compare word_1,word_2 word_3 with it. I give these words the values exactly as they are in
studsubj, then compare. For example, if word_1 = "we" ( or "you" ) ,word_2 = "WORK", word_3 = "today" the match with studsubj is perfect.
The problem is lines starting with "THEY". To be more exact, the first line is ok, but comparing with any of the other two lines only has match with word_1, but not word_2 and word_3. Why?
Thank you very much.
Code:
#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="THEY",word_2 = "read",word_3 ="books"; //  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;
}

Last edited by shabbir; 18Sep2011 at 08:28.. Reason: Code blocks
Zgugu Bamba's Avatar, Join Date: Sep 2011
Newbie Member
Here is the solution I got from my friend: &quot;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. &quot; So , everything works fine. Thanks to all.