Click to See Complete Forum and Search --> : how to declare a map of linked lists with stl?


Sno Kart
December 15th, 2002, 11:05 AM
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.


#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

Andreas Masur
December 15th, 2002, 11:37 AM
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...

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;
}

Sno Kart
December 15th, 2002, 01:46 PM
That's it! Thank you! I failed to see the forest for the trees...

Thanks again!