Convert str to int.

Hi everyone. Also a very stupid question: which function does convert string to int? Like strtoint in Pascal. And which converts them backwards?

Last edited on
you can use atoi:

atoi( str.c_str() )
You could also use a stringstream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <sstream>

int main()
{   
    std::string myStr = "123456789";
    int myInt = 0;
    std::istringstream os(myStr);
    os >> myInt;

    std::cout << myInt << std::endl;

    return 0;
}
123456789


http://www.cplusplus.com/reference/sstream/istringstream/
Last edited on
or:
1
2
string str = "123";
int i = stoi(str);
THanks a lot!)
closed account (SECMoG1T)
Also you might also like to use the stringstream objects , i mean they are very friendly to me when a want conversion from any type to any other
Topic archived. No new replies allowed.