**std::stod**
double stod (const string& str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
Example
#include <iostream>
#include <string>
int main ()
{
std::string orbits ("365.24 29.53");
std::string::size_type sz;
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
return 0;
}
Edit & Run
Possible output:
The moon completes 12.3684 orbits per Earth year.
**std::stof**
float stof (const string& str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
Example
#include <iostream>
#include <string>
int main ()
{
std::string orbits ("686.97 365.24");
std::string::size_type sz;
float mars = std::stof (orbits,&sz);
float earth = std::stof (orbits.substr(sz));
std::cout << "One martian year takes " << (mars/earth) << " Earth years.\n";
return 0;
}
Edit & Run
Possible output:
One martian year takes 1.88087 Earth years.
**std::stoi**
int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Example
#include <iostream>
#include <string>
int main ()
{
std::string str_dec = "2001, A Space Odyssey";
std::string str_hex = "40c3";
std::string str_bin = "-10010110001";
std::string str_auto = "0x7f";
std::string::size_type sz;
int i_dec = std::stoi (str_dec,&sz);
int i_hex = std::stoi (str_hex,nullptr,16);
int i_bin = std::stoi (str_bin,nullptr,2);
int i_auto = std::stoi (str_auto,nullptr,0);
std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
std::cout << str_hex << ": " << i_hex << '\n';
std::cout << str_bin << ": " << i_bin << '\n';
std::cout << str_auto << ": " << i_auto << '\n';
return 0;
}
Edit & Run
Output:
2001, A Space Odyssey: 2001 and [, A Space Odyssey]
40c3: 16579
-10010110001: -1201
0x7f: 127
**std::stol**
long stol (const string& str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
Example
#include <iostream>
#include <string>
int main ()
{
std::string str_dec = "1987520";
std::string str_hex = "2f04e009";
std::string str_bin = "-11101001100100111010";
std::string str_auto = "0x7fffff";
std::string::size_type sz;
long li_dec = std::stol (str_dec,&sz);
long li_hex = std::stol (str_hex,nullptr,16);
long li_bin = std::stol (str_bin,nullptr,2);
long li_auto = std::stol (str_auto,nullptr,0);
std::cout << str_dec << ": " << li_dec << '\n';
std::cout << str_hex << ": " << li_hex << '\n';
std::cout << str_bin << ": " << li_bin << '\n';
std::cout << str_auto << ": " << li_auto << '\n';
return 0;
}
Edit & Run
Output:
1987520: 1987520
2f04e009: 788848649
-11101001100100111010: -956730
0x7fffff: 8388607
**std::stold**
long double stold (const string& str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);
Example
#include <iostream>
#include <string>
int main ()
{
std::string orbits ("90613.305 365.24");
std::string::size_type sz;
long double pluto = std::stod (orbits,&sz);
long double earth = std::stod (orbits.substr(sz));
std::cout << "Pluto takes " << (pluto/earth) << " years to complete an orbit.\n";
return 0;
}
Possible output:
Pluto takes 248.093 years to complete an orbit.