I have to save an increasing ID as key into a levelDB database. So what I get (and what I have to give to levelDB) is a string.
Question: Is there an elegant way to increase a number saved in a string?
Example:
std::string key = "123";
[..fancy code snipped to increase key by 1..]
std::cout << key << std::endl; // yields 124
Cheers!
PS: Would prefer to stay with standard compilation, i.e. no C++11.
#include <sstream>
std::string key = "123";
std::istringstream in(key);
int int_key;
in >> int_key;
int_key++;
std::ostringstream out;
out << int_key;
key = out.str();
std::cout << key << std::endl;
You can also do it using c style casting:
std::string key = "123";
int int_key = atoi(key.c_str());
int_key++;
char key_char[20];
itoa(int_key, key_char, 10);
key = key_char;
cout << key << endl;