map
-
How to read the contents of a text file and insert in a map? The text file format is name1value1value2 name2value1value2
What map are you planning to use? An STL map? Each map entry is a pair of key and value. So what would be the expected key and value pairs?
-
What map are you planning to use? An STL map? Each map entry is a pair of key and value. So what would be the expected key and value pairs?
-
yes an STL map. the key will be the name and the value will be the values. text file format is name1value1value2 name2value1value2
-
yes an STL map. the key will be the name and the value will be the values. text file format is name1value1value2 name2value1value2
Following is the simplest way to use a map. #include <map> using namespace std; ... map<string, string> mp; mp["name1"] = "Value"; mp["name2"] = "value2";
-
How to read the contents of a text file and insert in a map? The text file format is name1value1value2 name2value1value2
ratprita wrote:
name1value1value2 name2value1value2
How can you tell among name and _value_s (there are no separators in your file)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
ratprita wrote:
name1value1value2 name2value1value2
How can you tell among name and _value_s (there are no separators in your file)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
ratprita wrote:
name1value1value2 name2value1value2
How can you tell among name and _value_s (there are no separators in your file)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]The name1 is
"name1"
, the value1 is"value1"
. Just kidding! :laugh:Maxwell Chen
-
yes there are seperators. name1 and value1 are seperated by tab. value1 and value2 are seperated by space. The first line(name1 tab value1 space value2)\n second line(name2 tab value1 space value2)
You may use a
map
whoose key is astring
containing the name and whose value is avector
ofstring
(each of the latter containing one of 'value1', 'v_alue2_', etc...). For instance:#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;int main()
{
// the map containing the 'name' and a vector of 'value's
map <string, vector <string > > item;
const int SIZE=0x100;
char buf[SIZE];
ifstream ifs("foo.txt");
while (ifs.getline(buf,SIZE).good())
{
size_t m, n;
string s(buf);
string name;
vector <string> value;
if ( (m=s.find('\t')) != string::npos)
{
name = s.substr(0,m);
while ((n=s.find(' ',m+1)) !=string::npos)
{
value.push_back( s.substr(m+1,(n-m)));
m=n;
}
value.push_back(s.substr(m+1));
item.insert(make_pair(name,value));
}
}// now dump all found items
map <string, vector <string > >::iterator itm;
for ( itm = item.begin(); itm != item.end(); itm++)
{
cout << "name = " << itm->first << endl;
vector <string>::iterator itv;
for ( itv = itm->second.begin(); itv != itm->second.end(); itv++)
{
cout << '\t' << *itv << endl;
}
}
}Please note: code should be made more robust (it isn't robust at all). That's left as exercise to the reader. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]