tags:

views:

111

answers:

4

Hi

I Have a list that contains dbfile,paths and i need to add them to a map inorder to get corresponding values. my LIST contains items as like

star
e:\data\star.mdf
e:\data\star.ldf
kiran
e:\data\kiran.mdf
e:\data\kiran.ldf  
hai
e:\data\hai.mdf
e:\data\hai.ldf

Now i need to insert them by populating a list as specified format

map<string,list<string>>
     |           |
    \/          \/
   databasename   paths info

how can i do this.

Plz help me

A: 

You could write the following:

std::list tempList;
tempList.push_back("e:\data\star.mdf");
tempList.push_back("e:\data\star.ldf");
m.insert(std::make_pair("star", tempList));

Note however that the tempList will be copied upon inserting. You might consider this to be an unacceptable performance overhead, which might be solved by using a smart pointer to the list. For example:

std::map>> m;

Dimitri C.
i have 100 database like that how to do it for 100
Cute
Then you'll have to put my sample code in a helper function.
Dimitri C.
How to do this one plz explain i have no idea.
Cute
You could use a loop for that.
sharptooth
That's really not hard. Just parameterize the strings.
Dimitri C.
"Give sample code plzzzzzzzz"
Cute
+1  A: 

When you read a string, like "star" (we'll assume into s), createa map entry:

mymap.insert( std::make_pair( s, list <string>() ) );

Now you will be reading directory names, into say d. As you have just created the entry, it's safe to use map's operator[]:

mymap[s].push_back( d );
anon
How to loop it for a min of 20 times using iterator
Cute
put the code I suggested in a loop.
anon
What's the problem? Iterate over the source data and fill the target.
sharptooth
problem is all that dbs and datapaths available in a list. in the order which i showed above.
Cute
What's the problem again? Loop over the source data. For strings with indices 0, 3, 6, etc create a new list, populate it with the two following strings and add to the map.
sharptooth
PLZ provide code snippedt...
Cute
A: 

I suppose that is what you want to do: You need to go through the file line-by-line using getline(), than you convert the buffer you used for getline to a string and check with a regular expression if it is a path or a datbasename (you might make your file format more verbose to express this more clearly or you use XML). Then you call the appropriate function to add to the map. In pseudocode with the assumption that there are only to paths per db:

infile.open(FILENAME);
while(infile.good()) {
getline(infile, buffer, blength)
string dbName = buffer
for(int i = 0; i < 2; i++) getline() and to string addStuffToMap();

Too lazy to write a working version right now...

pmr
A: 

What you need is a map of names (string) to a list of paths (string). All you need to do is declare exactly that: std::map< std::string, std::list< std::string> >. Easy as pie :-)

Try this:

#include <list>
#include <map>
#include <string>


int main()
{
    std::map<std::string,std::list<std::string> >     dbMap;

    dbMap["star"].push_back("e:\data\star.mdf");
    dbMap["star"].push_back("e:\data\star.ldf");

    dbMap["kiran"].push_back("e:\data\kiran.mdf");
    dbMap["kiran"].push_back("e:\data\kiran.ldf");

    dbMap["hai"].push_back("e:\data\hai.mdf");
    dbMap["hai"].push_back("e:\data\hai.ldf");
}
Martin York