|
-
December 15th, 2002, 12:05 PM
#1
how to declare a map of linked lists with stl?
I have included a rough example for purposes of illistration.
I want to look up an item based on the key in the map and search through the linked list.....help...there is a problem with the declaration.
Thank you for your time and effort.
Code:
#include <iostream>
#include <map>
#include <list>
#include <string>
using namespace std;
class book
{
public:
string author;
long ISBN;
float price;
};
using namespace std;
void main() {
book bk;
list<book> record;
list<book>::const_iterator iter1;
//here is the problem.............................
map<book,record> track;//why doesn't this work????
//I wnat a map of linked lists....HELP!
for(int j = 0; j < 3; j++)
{
cout<<"Enter author,isbn,and price"<<endl;
cin>>bk.author;
cin>>bk.ISBN;
cin>>bk.price;
record.push_back(bk);
}
float search;
cout<<"Enter Price to search for"<<endl;
cin>>search;
for(iter1 = record.begin();iter1 != record.end(); iter1++)
{
if((*iter1).price == search )
{
cout<<bk.author<<" "<<bk.ISBN<<" "<<bk.price<<endl;
}
}
//this is a working portion of map example
map<string, int> freq;
string word;
for(int i = 0; i < 3; i++)
{cout<<"enter words for mapping"<<endl;
cin >> word;
freq[word]++;
}
cout << "Number of words = " << freq.size() << endl;
map<string, int>::const_iterator iter;
for (iter=freq.begin(); iter != freq.end(); iter++)
{
cout << iter->second << " " << iter->first << endl;
if(iter->second == 4)
{
freq[word]--;
cout << iter->second << " " << iter->first << endl;
}
}
}//end main
-
December 15th, 2002, 12:37 PM
#2
I did not look over all the code but you are trying to pass a variable ('record') as a template argument which will not work...
Typedef your containers...
Code:
class book
{
public:
string author;
long ISBN;
float price;
};
typedef list<book> RECORD_TYPE;
typedef map<book, RECORD_TYPE> TRACK_TYPE;
int main()
{
book bk;
RECORD_TYPE record;
RECORD_TYPE::const_iterator iter1;
TRACK_TYPE track;
....
return 0;
}
-
December 15th, 2002, 02:46 PM
#3
That's it! Thank you! I failed to see the forest for the trees...
Thanks again!
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
|