|
-
May 15th, 2006, 11:59 PM
#1
Stopping Sorting Feature of Maps,
Hello
I am using a MAP as an associative array. The map automatically sorts by key. How can I stop this feature. I will may value as FIFO. I mean if I have the keys
Key=value
SSN=000-00-0000
Name=JAA
Age=66
When I iterate through the map I don't want may data to print as
Age
NAME
SSN
I want it to be as is:
SSN
NAME
AGE
-
May 16th, 2006, 12:09 AM
#2
Re: Stopping Sorting Feature of Maps,
The map automatically sorts by key. How can I stop this feature.
You can't. A map is a Sorted Associative Container. The keys in a map
are inherently ordered automatically.
If you don't want them to be ordered, you are going to need to use another container type.
But I think you will be ok here because only the keys are ordered,
not the data which they contain.
So, for example, if you were maintaining a log book of emplyee records.
The key could be their employee number, which would be numerically sorted.
Then, the data would be their ssn, age, and name which could be displayed
in any order you want while iterating through the map.
Last edited by dcjr84; May 16th, 2006 at 12:18 AM.
Please rate my post if you felt it was helpful
-
May 16th, 2006, 12:17 AM
#3
Re: Stopping Sorting Feature of Maps,
Like what? Please help me.
-
May 16th, 2006, 12:20 AM
#4
Re: Stopping Sorting Feature of Maps,
Well, this is tough to talk about abstractly.
I will need to see your code to make more sense of this.
Remember to use code tags please
Please rate my post if you felt it was helpful
-
May 16th, 2006, 12:39 AM
#5
Re: Stopping Sorting Feature of Maps,
I see you decided to go with maps! Good thinking. Any ways, here's the easy way to get past this limitation.
Create a structure to hold the SSN, Name, and Age
Create a map that associates a key with that structure.
Now, when you look up the key, they map class will return a structure with all of the information you need.
-
May 16th, 2006, 12:43 AM
#6
Re: Stopping Sorting Feature of Maps,
 Originally Posted by binarybob0001
I see you decided to go with maps! Good thinking. Any ways, here's the easy way to get past this limitation.
Create a structure to hold the SSN, Name, and Age
Create a map that associates a key with that structure.
Now, when you look up the key, they map class will return a structure with all of the information you need.
Exactly
Please rate my post if you felt it was helpful
-
May 16th, 2006, 01:43 AM
#7
Re: Stopping Sorting Feature of Maps,
This is just a quick example I came up with.
I used objects here instead of structs, but you could
easily do this with structs if you wanted to.
It uses employee numbers as the key, and associates
all other information with that key.
Hope this helps 
Code:
#include <map>
#include <iostream>
#include <string>
using namespace std;
//**************************************************************
class Employee
{
public:
Employee();
Employee( string, string, int );
string Name();
string SSN();
int Age();
private:
string name;
string ssn;
int age;
};
Employee :: Employee()
{
name = "John Doe";
ssn = "000-00-0000";
age = 100;
}
Employee :: Employee( string tName, string tSsn, int tAge )
{
name = tName;
ssn = tSsn;
age = tAge;
}
string Employee :: Name()
{
return name;
}
string Employee :: SSN()
{
return ssn;
}
int Employee :: Age()
{
return age;
}
//**************************************************************
int main( int argc, char* argv[] )
{
typedef map< string, Employee> map_t;
map_t myMap;
myMap["1"] = Employee("Quincy Adams", "123-45-6789", 50 );
myMap["2"] = Employee("John Jones", "321-78-4816", 75 );
myMap["3"] = Employee("Dave Watson", "569-09-1136", 60 );
for( map_t::iterator it = myMap.begin(); it != myMap.end(); ++it)
{
cout << "SSN: " << it->second.SSN() << endl;
cout << "Name: " << it->second.Name() << std::endl;
cout << "Age: " << it->second.Age() << std::endl;
cout << endl;
}
cin.get();
return 0;
}
Last edited by dcjr84; May 16th, 2006 at 02:58 AM.
Please rate my post if you felt it was helpful
-
May 16th, 2006, 01:45 AM
#8
Re: Stopping Sorting Feature of Maps,
I did not really want to post everything because people tend not to read complex issues but I am really stuck and I need help.
I have one file that has the names of two other files:
Filename:tbl.def
EMP
DEPT
The other two files are:
filename:EMP
ssn;I;10;NO;YES;NA;
name;s;25;NO;YES;NA;
nation;s;10;YES;YES;AMERICAN;
age;d;8;YES;NO;NA;
filename:DEPT
deptno;I;10;NO;YES;NA;
deptname;s;25;NO;YES;NA;
The structure is as follows:
fieldname;type;size;null;index;default
Now, I need to read file tbl.def. Then create a likedlist to hold its content which is EMP and DEPT. I have done this step.
Based on the content of tbl.def, I have to read the other two files, slice each line as
fieldname=ssn
type=I (int)
size= 10
and so
Then I need to create either a linkedlist, map, etc. to hold these data.
Why? because I need to create another linked list to act as a table(records). I need to use this data to check for entries from the user. The user will try to open a database (tbl.def) which has two tables (EMP and DEPT). Then start to enter data in the EMP or DEPT table.
My program should ask the user to enter ssn, name, nation and age.
Do you see that my data structure is known only at runtime.
I am making a very simple memory-based database.
Now, I am stuck on making a map to hold the table structure i.e
fieldname=ssn
type=i
size=10
etc.
for table EMP and DEPT
Notice that I might have any number of tables. File tbl.def might have:
EMP
DEPT
PROJECT
then I will have to have a third file called project.
The code that I am stuck at is
Code:
map<pair<int,string>,map<string,string> > tmpmap;
map<string,map<string,string> >::iterator opos;
map<string, string>::iterator ipos;
void AddRecords(string mnu_in)
{
int is_topen, cc;
string s1,s2,sk,sv;
char iabuffer[30]; //temp string
cc=0;
is_topen=TRUE;
if(!is_topen)
{
cout<<"Table "<<mnu_in<<" is not open";
system("pause");
}else{
system("cls");
cc=0;
for(iter2=tbldef.begin();iter2 !=tbldef.end();iter2++)
{
s1=MyToUpper(iter2->td_tblname);
s2=MyToUpper(mnu_in);
if(strcmp(s1.c_str(),s2.c_str())==0)
{
cc++;
tmpmap[make_pair(cc,iter2->td_fname)]["tblname"] =iter2->td_tblname;
tmpmap[make_pair(cc,iter2->td_fname)]["fname"] =iter2->td_fname;
tmpmap[make_pair(cc,iter2->td_fname)]["type"] =iter2->td_type;
itoa(iter2->td_size,iabuffer,10);
tmpmap[make_pair(cc,iter2->td_fname)]["size"] =iabuffer;
tmpmap[make_pair(cc,iter2->td_fname)]["null"] =iter2->td_null;
tmpmap[make_pair(cc,iter2->td_fname)]["index"] =iter2->td_index;
tmpmap[make_pair(cc,iter2->td_fname)]["default"] =iter2->td_default;
}
}
}
for( opos = tmpmap.begin();opos!=tmpmap.end();++opos)
{
for(ipos = (opos->second).begin(); ipos!=(opos->second).end(); ++ipos)
{
if(ipos->first=="fname")
{
//cout<<"Enter "<<MyToUpper(ipos->second)<<":\n";
//cin >> sk;
//make_pair(
cout<<opos->first<<"=="<<ipos->first<<"=="<<ipos->second<<"\n\n";
}
}
}
}
Because MAPs are ordered by nature, I made its index as a pair<int,...> as shown on the top of the code.
No error was generated; however, I could not iterate and print the content of this map
Last edited by HAOBBOY; May 16th, 2006 at 03:05 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|