Splitting Strings in a map<string, string>

B

byte8bits

Hey guys, I'm new to cpp. Trying to split a string (on whitespace)
into a map<string, string> and return map<string, string> from within
a function. I've cobbled this together from examples on the web and
experimenting myself:

map<string, string> get_areas_groups()
{
const char* file_name = "areas_groups.txt";

// area_group should be a string that looks like this: xxx xx
// Three chars a space followed by two chars.
string area_group;

// ag is a mapping of xxx to xx
map<string, string> ag;

ifstream fin (file_name);
while (!fin.eof())
{
getline(fin, area_group);
string buf;
stringstream ss(area_group);
while (ss >> buf)
{
cout << buf << endl;
// Here's where I can't figure things out.
// The split works OK, but not in a way conducive
// for a map insert as buf is replaced. A vector<string>
// works great here.
ag.insert(pair<string, string>("xxx", "xx"));
}
}
fin.close();
cout << ag.size() << endl;
return ag;
}

Any tips are appreciated. If I'm approaching this entirely wrong, I'm
open to pointers on how to do it better. Here is an example of the
actual file I'm feeding to this function:

http://16systems.com/find_ssns/areas_groups.txt


Thanks,

Brad
 
K

kasthurirangan.balaji

Hey guys, I'm new to cpp. Trying to split a string (on whitespace)
into a map<string, string> and return map<string, string> from within
a function. I've cobbled this together from examples on the web and
experimenting myself:

map<string, string> get_areas_groups()
  {
    const char* file_name = "areas_groups.txt";

    // area_group should be a string that looks like this: xxx xx
    // Three chars a space followed by two chars.
    string area_group;

    // ag is a mapping of xxx to xx
    map<string, string> ag;

    ifstream fin (file_name);
    while (!fin.eof())
      {
        getline(fin, area_group);
        string buf;
        stringstream ss(area_group);

seeing your input file, you may want to use
string buf, buf1;
ss >> buf >> buf1;
ag.insert(pair<string,string>(buf,buf1));

I assume you have all these in place.
#include <string>
#include said:
        while (ss >> buf)
          {
          cout << buf << endl;
          // Here's where I can't figure things out.
          // The split works OK, but not in a way conducive
          // for a map insert as buf is replaced. A vector<string>
          // works great here.
          ag.insert(pair<string, string>("xxx", "xx"));
          }
      }
    fin.close();
    cout << ag.size() << endl;
    return ag;
  }

Any tips are appreciated. If I'm approaching this entirely wrong, I'm
open to pointers on how to do it better. Here is an example of the
actual file I'm feeding to this function:

http://16systems.com/find_ssns/areas_groups.txt

Thanks,

Brad
an unanswered question is
when the input file contains numbers and why would you want to use
strings?

Thanks,
Balaji.
 
J

James Kanze

Hey guys, I'm new to cpp. Trying to split a string (on whitespace)
into a map<string, string> and return map<string, string> from within
a function. I've cobbled this together from examples on the web and
experimenting myself:
map<string, string> get_areas_groups()
{
const char* file_name = "areas_groups.txt";
// area_group should be a string that looks like this: xxx xx
// Three chars a space followed by two chars.
string area_group;
// ag is a mapping of xxx to xx
map<string, string> ag;

I would strongly recommend a typedef here:

typedef std::map< std::string, std::string >
Map ;
Map area_groups ;

ifstream fin (file_name);
while (!fin.eof())

This line is definitely wrong. What you want is

while ( std::getline( fin, area_group ) ) {
{
getline(fin, area_group);

And no getline here, of course.
string buf;
stringstream ss(area_group);
while (ss >> buf)

Why not:

std::string key ;
std::string value ;
if ( ss >> key >> value >> std::ws && ss.get() == EOF ) {
area_group[ key ] = value ;
} else {
// syntax error in the line...
}

?

More likely, you want to detect duplicate keys. In that case,
inside the if:

if ( ! area_group.insert( Map::value_type( key, value )
.second ) {
// duplicate entry ...
}

At any rate, if you want exactly two entries, a loop is not the
solution.
 
B

byte8bits

Thanks for the input. I used the suggestions to make it work. I
appreciate it.

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top