none
string to integer in C++

    Question

  • Hi,

    I'm trying to convert string to integer in C++.
    Can anyone tell how to do that?

    Suppose the string is "5 1 2 3 4"
    First task is to split this string ,space is the separator.
    Then how can i make them from string to integer?(not using atoi)
    Using C++ only.

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<sstream>
    #include<stdlib.h>
    using namespace std;
    
    int main() {        
        string str, buf;
        vector<string>tokens;
        while (std::getline(cin, str, '\n')) {
            istringstream isscin(str),iton;
            tokens.clear();
            while (std::getline(isscin, buf, ' ')) {
                cout << buf << endl;            
            }
            cout << endl;
        }
        return 0;
    }
    

    Monday, July 27, 2009 5:29 PM

Answers

  • #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<sstream>
    #include<stdlib.h>
    using namespace std;
    
    int main() {        
        string str, buf;
        vector<string>tokens;
        while (std::getline(cin, str, '\n')) {
            istringstream isscin(str),iton;
            while (std::getline(isscin, buf, ' ')) {
                int number;
                stringstream strstream(buf);
                strstream >> number;
                // number should have your integer here....
               cout << number << endl;
            }
            cout << endl;
        }
        return 0;
    }
    

    This is just a slight modification of your previous code... it adds the creation of the stringstream off the token (buf) and pulls it into an integer for you.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Iceweasel Monday, July 27, 2009 6:28 PM
    Monday, July 27, 2009 6:05 PM

All replies


  • If you want to avoid atoi() [which is perfectly reasonable, IMO], you can do this with stringstream:

    string str = "5";
    stringstream sstream(str); 
    
    int i;
    sstream >> i;
    
    // i == 5 now
    



    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, July 27, 2009 5:38 PM

  • If you want to avoid atoi() [which is perfectly reasonable, IMO], you can do this with stringstream:

    string str = "5";
    stringstream sstream(str); 
    
    int i;
    ss >> i;
    
    // i == 5 now
    



    Reed Copsey, Jr. - http://reedcopsey.com
    Thanks for your reply.
    After spliting the string how can do this with stringstream?
    Monday, July 27, 2009 5:40 PM
  • You need to do this on each individual token of the split string.  Once you tokenize the string, just construct a stringstream from each token, and use the code above.

    Alternatively, if you know how many numbers are in the string, you can just use one stringstream, and do not split the string - Just do multiple sstream >> integer calls;



    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, July 27, 2009 5:47 PM
  • You need to do this on each individual token of the split string.  Once you tokenize the string, just construct a stringstream from each token, and use the code above.

    Alternatively, if you know how many numbers are in the string, you can just use one stringstream, and do not split the string - Just do multiple sstream >> integer calls;



    Reed Copsey, Jr. - http://reedcopsey.com
    Though i'm trying and searching can you please post the complete code?
    It would be helpful for me.
    Monday, July 27, 2009 5:51 PM
  • #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<sstream>
    #include<stdlib.h>
    using namespace std;
    
    int main() {        
        string str, buf;
        vector<string>tokens;
        while (std::getline(cin, str, '\n')) {
            istringstream isscin(str),iton;
            while (std::getline(isscin, buf, ' ')) {
                int number;
                stringstream strstream(buf);
                strstream >> number;
                // number should have your integer here....
               cout << number << endl;
            }
            cout << endl;
        }
        return 0;
    }
    

    This is just a slight modification of your previous code... it adds the creation of the stringstream off the token (buf) and pulls it into an integer for you.
    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by Iceweasel Monday, July 27, 2009 6:28 PM
    Monday, July 27, 2009 6:05 PM
  • Thank you.
    The problem is solved.
    Monday, July 27, 2009 6:09 PM