query reg STL maps
-
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
-
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
You seem to have a confusion between
Map1
andMap
, betweenone1
andone
, or are these just typos in the post? What are your errors? Paul -
You seem to have a confusion between
Map1
andMap
, betweenone1
andone
, or are these just typos in the post? What are your errors? PaulI 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; }
-
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; }
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
-
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
-
my first posting had a cut and paste error in it; the map declaration should look like: typedef map Map; -- sas
-
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; }
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