Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. query reg STL maps

query reg STL maps

Scheduled Pinned Locked Moved ATL / WTL / STL
c++databasetutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 1149186
    wrote on last edited by
    #1

    Sir, I m implementing std::maps and new to maps I am using int as the key and the class as the value. I have declared it like this But my main is to insert a int as the key and the entire class as the value. Also by giving the key ,I want to get the value using find function How to achieve this. Pls correct me if I am wrong. #include #include #include class one { int age; int sal; }one1; typedef map Map1; int main() { Map theMap1; Map::iterator theIterator1; one.age =50; theMap1.insert(Map::value_type(0,one.age)); theMap1.insert(Map::value_type(1,one.age)); return 0; } I am getting errors Regards

    P 1 Reply Last reply
    0
    • U User 1149186

      Sir, I m implementing std::maps and new to maps I am using int as the key and the class as the value. I have declared it like this But my main is to insert a int as the key and the entire class as the value. Also by giving the key ,I want to get the value using find function How to achieve this. Pls correct me if I am wrong. #include #include #include class one { int age; int sal; }one1; typedef map Map1; int main() { Map theMap1; Map::iterator theIterator1; one.age =50; theMap1.insert(Map::value_type(0,one.age)); theMap1.insert(Map::value_type(1,one.age)); return 0; } I am getting errors Regards

      P Offline
      P Offline
      Paul Ranson
      wrote on last edited by
      #2

      You seem to have a confusion between Map1 and Map, between one1 and one, or are these just typos in the post? What are your errors? Paul

      U 1 Reply Last reply
      0
      • P Paul Ranson

        You seem to have a confusion between Map1 and Map, between one1 and one, or are these just typos in the post? What are your errors? Paul

        U Offline
        U Offline
        User 1149186
        wrote on last edited by
        #3

        I need to map an int to a class. so this is the code.I hope I am clear. #include #include #include sing namespace std; class one { int age; int sal; }one1; typedef map Map; int main() { Map theMap; Map::iterator theIterator; //Open the file //Read each line of file and store in class variables index = 0; while(!file.eof()) { one.age =50; one sal = 1000; theMap.insert(Map::value_type(index,one)); index++; } //to get the class theIterator = theMap.find(0); //Get the entire first record ?How should I achieve this which is stored in a class? cout << endl << "The class "<< (*theIterator).first; cout << endl << "The class "<< (*theIterator).second << endl ; return 0; }

        S P 2 Replies Last reply
        0
        • U User 1149186

          I need to map an int to a class. so this is the code.I hope I am clear. #include #include #include sing namespace std; class one { int age; int sal; }one1; typedef map Map; int main() { Map theMap; Map::iterator theIterator; //Open the file //Read each line of file and store in class variables index = 0; while(!file.eof()) { one.age =50; one sal = 1000; theMap.insert(Map::value_type(index,one)); index++; } //to get the class theIterator = theMap.find(0); //Get the entire first record ?How should I achieve this which is stored in a class? cout << endl << "The class "<< (*theIterator).first; cout << endl << "The class "<< (*theIterator).second << endl ; return 0; }

          S Offline
          S Offline
          sas2222
          wrote on last edited by
          #4

          i had a little time to delve into this, so here you go: It appears that your map declaration is incomplete: typedef map Map should look like: typedef map Map stl maps are templated classes, meaning at declartion time the coder needs to tell the map what is being stored in the map, in this case, an integer and a One object are being stored in the map. -- your One class should have the age and salary members public (for the quicky example); I changed your One class to look like class One { public: One() {age = -1; sal = -1;}; //ctor int age; // these could be private wwith public accessors.. int sal; protected: private: }; -- also in your while loop, you should instantiate a new One object (either on the heap with 'new' or on the stack like below): when assigning the int and the One object to the map, stl will use the One object's copy constructor to make a copy of the object to store in the map; while(!file.eof()) { One newOne; // create new Oneobject on the stack newOne.age = 50; newOne.sal = 1000; theMap.insert(Map::value_type(index, newOne)); index++; } -------- when printing out your map, your code as written assumes the One class has an overloaded operator <<; you can also print the indiviudals members like so (note the accessing of the One object's members): cout << endl << "The class "<< (*theIterator).first; cout << endl << "The class "<< ((*theIterator).second).age << " | " << ((*theIterator).second).sal << endl ; -- here's a cut and paste of my changes to your example: i compile it but did not execute it, btw... class One { public: One() {age = -1; sal = -1;}; //ctor init of members int age; int sal; protected: private: }; typedef map Map; int main() { Map theMap; Map::iterator theIterator; //Open the file //Read each line of file and store in class variables int index = 0; while(!file.eof()) { One newOne; newOne.age =50; newOne.sal = 1000; theMap.insert(Map::value_type(index, newOne)); index++; } //to get the class theIterator = theMap.find(0); //Get the entire first record ?How should I achieve this which is stored in a class? cout << endl << "The class "<< (*theIterator).first; cout << endl << "The class "<< ((*theIterator).second).age << " | " << ((*theIterator).second).sal << endl ; return 0; } sas

          S 1 Reply Last reply
          0
          • S sas2222

            i had a little time to delve into this, so here you go: It appears that your map declaration is incomplete: typedef map Map should look like: typedef map Map stl maps are templated classes, meaning at declartion time the coder needs to tell the map what is being stored in the map, in this case, an integer and a One object are being stored in the map. -- your One class should have the age and salary members public (for the quicky example); I changed your One class to look like class One { public: One() {age = -1; sal = -1;}; //ctor int age; // these could be private wwith public accessors.. int sal; protected: private: }; -- also in your while loop, you should instantiate a new One object (either on the heap with 'new' or on the stack like below): when assigning the int and the One object to the map, stl will use the One object's copy constructor to make a copy of the object to store in the map; while(!file.eof()) { One newOne; // create new Oneobject on the stack newOne.age = 50; newOne.sal = 1000; theMap.insert(Map::value_type(index, newOne)); index++; } -------- when printing out your map, your code as written assumes the One class has an overloaded operator <<; you can also print the indiviudals members like so (note the accessing of the One object's members): cout << endl << "The class "<< (*theIterator).first; cout << endl << "The class "<< ((*theIterator).second).age << " | " << ((*theIterator).second).sal << endl ; -- here's a cut and paste of my changes to your example: i compile it but did not execute it, btw... class One { public: One() {age = -1; sal = -1;}; //ctor init of members int age; int sal; protected: private: }; typedef map Map; int main() { Map theMap; Map::iterator theIterator; //Open the file //Read each line of file and store in class variables int index = 0; while(!file.eof()) { One newOne; newOne.age =50; newOne.sal = 1000; theMap.insert(Map::value_type(index, newOne)); index++; } //to get the class theIterator = theMap.find(0); //Get the entire first record ?How should I achieve this which is stored in a class? cout << endl << "The class "<< (*theIterator).first; cout << endl << "The class "<< ((*theIterator).second).age << " | " << ((*theIterator).second).sal << endl ; return 0; } sas

            S Offline
            S Offline
            sas2222
            wrote on last edited by
            #5

            my first posting had a cut and paste error in it; the map declaration should look like: typedef map Map; -- sas

            S 1 Reply Last reply
            0
            • S sas2222

              my first posting had a cut and paste error in it; the map declaration should look like: typedef map Map; -- sas

              S Offline
              S Offline
              sas2222
              wrote on last edited by
              #6

              my cut and paste of the map declaration is getting altered somehow; the angle brackets and template parameters are getting stripped out.. wierd.. try paranthesis for visual sake typedef map(int, One) Map sas

              1 Reply Last reply
              0
              • U User 1149186

                I need to map an int to a class. so this is the code.I hope I am clear. #include #include #include sing namespace std; class one { int age; int sal; }one1; typedef map Map; int main() { Map theMap; Map::iterator theIterator; //Open the file //Read each line of file and store in class variables index = 0; while(!file.eof()) { one.age =50; one sal = 1000; theMap.insert(Map::value_type(index,one)); index++; } //to get the class theIterator = theMap.find(0); //Get the entire first record ?How should I achieve this which is stored in a class? cout << endl << "The class "<< (*theIterator).first; cout << endl << "The class "<< (*theIterator).second << endl ; return 0; }

                P Offline
                P Offline
                Paul Ranson
                wrote on last edited by
                #7

                Does this help?

                #include <iostream>
                #include <map>

                class ClassOne
                {
                public :
                int age;
                int sal;
                } ;

                typedef std::map<int, ClassOne> Map ;

                int main()
                {
                Map theMap;
                Map::iterator theIterator;

                for ( int i = 0; i < 1000; ++i )
                {
                    ClassOne one ;
                    one.age = 40 ;
                    one.sal = 1000 ;
                    theMap.insert ( Map::value\_type ( i, one )) ;
                }
                
                theIterator = theMap.find(0);
                
                if ( theIterator != theMap.end ())
                {
                    std::cout << "The index " << (\*theIterator).first << std::endl ;
                    std::cout << "The class, age = " << (\*theIterator).second.age << ", sal = " << (\*theIterator).second.sal << std::endl ;
                }
                
                return 0;
                

                }

                There are other ways to manage insertion, for instance,

                >#include <iostream>
                #include <map>

                class ClassTwo
                {
                private :
                int age;
                int sal;
                public :
                ClassTwo () : age ( 0 ), sal ( 0 )
                {
                }
                void Set ( int a, int s )
                {
                age = a ;
                sal = s ;
                }
                int Age () const
                {
                return age ;
                }
                int Sal () const
                {
                return sal ;
                }
                } ;

                typedef std::map<int, ClassTwo> Map ;

                int _tmain(int argc, _TCHAR* argv[])
                {
                Map theMap;
                Map::iterator theIterator;

                for ( int i = 0; i < 1000; ++i )
                {
                    std::pair<Map::iterator,bool> itp = theMap.insert ( Map::value\_type ( i, ClassTwo ())) ;
                    if ( itp.second )
                    {
                        theIterator = itp.first ;
                        (\*theIterator).second.Set ( 40, 1000 ) ;
                    }            
                }
                
                theIterator = theMap.find(0);
                
                if ( theIterator != theMap.end ())
                {
                    std::cout << "The index " << (\*theIterator).first << std::endl ;
                    std::cout << "The class, age = " << (\*theIterator).second.age << ", sal = " << (\*theIterator).second.sal << std::endl ;
                }
                
                return 0;
                

                }

                Paul

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups