@Jonathan Mee: Actually your post is really elegant (you might get into trouble, if the parsed format does not match). Hence my answer is: There is no better way. +1
Edit:
#include <iostream>
#include <map>
#include <sstream>
int main() {
std::istringstream input(
"one : 1\n"
"two : 2\n"
"three:3\n"
"four : 4\n"
"invalid key : 5\n"
"invalid_value : 6 6 \n"
);
std::string key;
std::string value;
std::map<std::string, int > map;
while(std::getline(input, key, ':') && std::getline(input, value))
{
std::istringstream k(key);
char sentinel;
k >> key;
if( ! k || k >> sentinel) std::cerr << "Invalid Key: " << key << std::endl;
else {
std::istringstream v(value);
int i;
v >> i;
if( ! v || v >> sentinel) std::cerr << "Invalid value:" << value << std::endl;
else {
map[key] = i;
}
}
}
for(const auto& kv: map)
std::cout << kv.first << " = " << kv.second << std::endl;
return 0;
}