c++ - When I use std::map to read values from a file, the value takes "," with it because the keys and values are separated by commas -


i have tried best on code not desired job done.

  1. please me read map without "," file.
  2. how traverse output map again.
  3. if there multiple keys how value of first key e.g key = 90, value == "hello" & key = 90, value = world. need output of first key.

this i've got far:

#include <fstream>  #include <map> #include <utility> #include <vector> #include <iostream> #include <string> using namespace std;  int main() {     std::map<int, std::string> mymap;     std::map<int, std::string>::iterator it;      fstream textfile;     textfile.open("map.txt");  // opening file       if (!textfile)     {         cout << " error, not open file. " << endl;     }      int key;     string value;      while (textfile >> key >> value)     {         mymap[key] = value;     }      // traverse map       = mymap.find(90);     if (it != mymap.end())     {         cout << " value @ 90 = " << it->second << endl;     }     else     {         cout << "the value @ 90 not avaible " << endl;     }      = mymap.find(9999);     if (it != mymap.end())     {         cout << " value @ 9999 = " << it->second << endl;     }     else     {         cout << "the value @ 9999 not avaible " << endl;     }       system("pause"); }` 

please me read map without "," file.

you can first reading string comma using getline , converting int.

getline(textfile,key,',');//read line upto comma file int ikey=atoi(key.c_str());//convert int 

if there multiple keys how value of first key e.g key = 90, value == "hello" & key = 90, value = world. need output of first key.

use find function value of first element given key.

mymap.find(ikey)->second 

how traverse output map again.

you can uses maps iterator in loop that.

so may this:

#include <string> #include <fstream>  ....  ifstream textfile("your file name"); multimap<int,string> mymap;//key value can duplicate here typedef pair <int, string> intstr_pair;//this used insert key/value pairs map int ikey; string key,value;  if(textfile) {//file opened     while (!textfile.eof())     {         getline(textfile,key,',');//read next line upto comma file, next key         textfile>>value;//read next value file          ikey=atoi(key.c_str());//convert key integer          mymap.insert(intstr_pair(ikey,value));//insert map     } }  multimap<int,string>::iterator it; for(it=mymap.begin();it!=mymap.end();it++)     cout<<it->first<<"......."<<it->second<<endl;//display key/value pair 

to first value of specific key,say 90 in case, do:

mymap.find(90)->second 

to values of elements given key, can use multimap::equal_range function , this(this similar using lower_bound , upper_bound function):

typedef pair<multimap<int,string>::iterator,multimap<int,string>::iterator> itpair;//iterator pair  itpair range=test.equal_range(90);//get range of elements given key(90 in case)  for( ; range.first!=range.second ; range.first++)//iterate through range display elements     cout<<range.first->first<<"....."<<range.first->second<<endl;//display key/value pairs 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -