associate contaner c++

This is a discussion on associate contaner c++ within the C++ Programming forums, part of the General Programming Boards category; Hi, I need help to implement this program using "Multimap container" to search for the student record by student name. ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    associate contaner c++

    Hi, I need help to implement this program using "Multimap container" to search for the student record by student name. I want to have duplicate name in the map. please help

    insert
    Code:
    #include
    Code:
    <iostream>
    
    #include
    <string>
    
    #include
    <map>
    
    using
    namespace std;
    
    
    class
     Student
    
    {
    
    public
    :
    
      
    //--- Constructor
    
      Student (
    int id = 0, double gpa = 0);
    
      
    //--- Output operator
    
      
    friend ostream & operator<<(ostream & out, const Student & s);
    
      
    //--- Input operator
    
      
    friend istream & operator>>(istream & in, Student & s);
    
      
    //--- Less-than operator
    
      
    friendbooloperator<(const Student & a, const Student & b);
    
    private
    :
    
      
    //--- Data members
    
      
    int myId;
    
      
    double myGPA;
    
    };  
    // end of class Student
    
    
    //--- Definition of constructor
    
    inline
     Student::Student(int id, double gpa)
    
      : myId(id), myGPA(gpa)
    
    { }
    
    
    //--- Definition of input operator
    
    inline
     istream & operator>>(istream & in, Student & s)
    
    {
    
      in >> s.myId >> s.myGPA;
    
    }
    
    
    //--- Definition of output operator
    
    inline
     ostream & operator<<(ostream & out, const Student & s)
    
    { out << 
    "id = " << s.myId << "  GPA = " << s.myGPA;
    
     
    return out;
    
    }
    
    
    //--- Definition of less-than operator
    
    inline
    booloperator<(const Student & a, const Student & b)
    
    { 
    return a.myId < b.myId; }
    
    
    //==================================================================
    
    
    int
     main()
    
    {
    
      
    typedef map<string, Student, less<string> > map1;
    
      
    typedef map<Student, string, less<Student> > map2;
    
    
      map1 a1;   
    // associative array of Student, index type is string
    
      map2 a2;   
    // associative array of string, index type is Student
    
    
      Student s;
    
      s = Student(12345, 3.3);  a1[
    "Fred"] = s;   a2[s] = "Fred";
    
      s = Student(32322, 3.9);  a1[
    "Al"] = s;     a2[s] = "Al";
    
      s = Student(13131, 2.5);  a1[
    "Joan"] = s;   a2[s] = "Joan";
    
      s = Student(22121, 4.0);  a1[
    "Barb"] = s;   a2[s] = "Barb";
    
      s = Student(28888, 2.9);  a1[
    "George"] = s; a2[s] = "George";
    
      s = Student(19981, 3.0);  a1[
    "Dot"] = s;    a2[s] = "Dot";
    
      s = Student(20012, 2.9);  a1[
    "Sue"] = s;    a2[s] = "Sue";
    
      string name;
    
      cout << 
    "Enter a name: ";
    
      cin >> name;
    
      map1::iterator it1 = a1.find(name);
    
      cout << name << 
    " has ";
    
      
    if (it1 == a1.end())
    
        cout << 
    "no info";
    
      
    else
    
        cout << a1[name];
    
      cout << endl;
    
    
      Student aStudent;
    
      cout << 
    "Enter a Student's id and GPA: ";
    
      cin >> aStudent;
    
      map2::iterator it2 = a2.find(aStudent);
    
      cout << 
    "Student " << aStudent << " is ";
    
    
      
    if (it2 == a2.end())
    
        cout << 
    "no info";
    
      
    else
    
        cout << a2[aStudent];
    
      cout << endl;
    
    }
    

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You'll have to repost your code. Don't use any bbcode in your post except for the code tags. And make sure the program text is plain text as well.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Hi, I have repost the code.so please help me to implemented as Multimap. thank you
    insert
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    class Student
    {
    public:
      //--- Constructor
      Student (int id = 0, double gpa = 0);
      //--- Output operator
      friend ostream & operator<<(ostream & out, const Student & s);
      //--- Input operator
      friend istream & operator>>(istream & in, Student & s);
      //--- Less-than operator
      friend bool operator<(const Student & a, const Student & b);
    private:
      //--- Data members
      int myId;
      double myGPA;
    };  // end of class Student
    
    //--- Definition of constructor
    inline Student::Student(int id, double gpa)
      : myId(id), myGPA(gpa)
    { }
    
    //--- Definition of input operator
    inline istream & operator>>(istream & in, Student & s)
    {
      in >> s.myId >> s.myGPA;
    return in;
    }
    
    //--- Definition of output operator
    inline ostream & operator<<(ostream & out, const Student & s)
    { out << "id = " << s.myId << "  GPA = " << s.myGPA;
     return out;
    }
    
    //--- Definition of less-than operator
    inline bool operator<(const Student & a, const Student & b)
    { return a.myId < b.myId; }
    
    //==================================================================
    
    int main()
    {
      typedef map<string, Student, less<string> > map1;
      typedef map<Student, string, less<Student> > map2;
    
      map1 a1;   // associative array of Student, index type is string
      map2 a2;   // associative array of string, index type is Student
    
      Student s;
      s = Student(12345, 3.3);  a1["Fred"] = s;   a2[s] = "Fred";
      s = Student(32322, 3.9);  a1["Al"] = s;     a2[s] = "Al";
      s = Student(13131, 2.5);  a1["Joan"] = s;   a2[s] = "Joan";
      s = Student(22121, 4.0);  a1["Barb"] = s;   a2[s] = "Barb";
      s = Student(28888, 2.9);  a1["George"] = s; a2[s] = "George";
      s = Student(19981, 3.0);  a1["Dot"] = s;    a2[s] = "Dot";
      s = Student(20012, 2.9);  a1["Sue"] = s;    a2[s] = "Sue";
      string name;
      cout << "Enter a name: ";
      cin >> name;
      map1::iterator it1 = a1.find(name);
      cout << name << " has ";
      if (it1 == a1.end())
        cout << "no info";
      else
        cout << a1[name];
      cout << endl;
    
      Student aStudent;
      cout << "Enter a Student's id and GPA: ";
      cin >> aStudent;
      map2::iterator it2 = a2.find(aStudent);
      cout << "Student " << aStudent << " is ";
    
      if (it2 == a2.end())
        cout << "no info";
      else
        cout << a2[aStudent];
      cout << endl;
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Help you with what? You just dumped a load of code on us.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21