Skip to content

Instantly share code, notes, and snippets.

Created February 18, 2013 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/145e2c524aa7a9bdc9f6 to your computer and use it in GitHub Desktop.
Save anonymous/145e2c524aa7a9bdc9f6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <sstream>
std::vector<std::pair<std::string, std::string> >
parse_data (std::string const& data)
{
std::vector<std::pair<std::string,std::string> > values;
std::istringstream iss (data);
std::string line;
std::string key, value;
while (std::getline (iss, line)) {
std::istringstream inner_iss (line);
if (std::getline (inner_iss, key, '=') && std::getline (inner_iss, value)) {
values.push_back (std::make_pair (key, value));
}
}
return values;
}
int
main (int argc, char *argv[])
{
typedef std::pair<std::string,std::string> StrPair;
std::vector<StrPair> values = parse_data (
"hello=world" "\n"
"foo=bar" "\n"
);
for (std::vector<StrPair>::iterator it = values.begin (); it != values.end (); ++it) {
std::cerr << it->first << " = " << it->second << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment