5

I'm seeing a lot of options for converting a string to a number in C++.

Some of which are actually recommending the use of standard C functions such as atoi and atof.

I have not seen anyone suggesting the following option, which relies solely on C++ STL:

int Str2Num(const string& str) // can be called with a 'char*' argument as well
{
    int num;
    istringstream(str)>>num;
    return num;
}

Or more generally:

template <typename type>
type Str2Num(const string& str) // can be called with a 'char*' argument as well
{
    type num;
    istringstream(str)>>num;
    return num;
}

What are the disadvantages in the above implementation?

Is there a simpler / cleaner way to achieve this conversion?

14
  • 3
    If this is not a duplicate I would be deeply surprised. Commented Mar 1, 2014 at 12:30
  • Have a look at boost::lexical_cast: boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html Commented Mar 1, 2014 at 12:34
  • @user2672165, here is the first sentence in my question: "I'm seeing a lot of options for converting a string to a number in C++". And here is the third sentence in my question: "I have not seen anyone suggesting the following option, which relies solely on C++ STL". So obviously, I have posted this question after not being able to find an answer which fits the "pure C++ STL" requirements. Commented Mar 1, 2014 at 12:41
  • possible duplicate of How to parse a string to an int in C++? Commented Mar 1, 2014 at 12:42
  • @Ali, hi. Please read my comment to user2672165 above. Commented Mar 1, 2014 at 12:45

4 Answers 4

9

Since C++11, we have had std::stoi:

std::stoi(str)

There is also std::stol and std::stoll.

Sign up to request clarification or add additional context in comments.

2 Comments

Great!!! That's what I was looking for when I said "Is there a simpler / cleaner way to achieve this conversion?"... stod for double?
@barakmanos Yeah, there are floating point alternatives too.
4

In there's a number of numeric conversion functions defined in the std::string class itself:

Numeric conversions
stoi (C++11)
stol (C++11)
stoll (C++11)

converts a string to a signed integer

stoul (C++11)
stoull (C++11)

converts a string to an unsigned integer

stof (C++11)
stod (C++11)
stold (C++11)

converts a string to a floating point value

As for pre c++11 standards, I can't see any disadvantages from your template function sample.

Comments

1

#include <iostream>
#include <string>

int main() {

    std::string str = "123";
    int num;

    // using stoi() to store the value of str1 to x
    num = std::stoi(str);

    std::cout << num;

    return 0;
}

Comments

0
#include <sstream>
template <typename T>
inline bool StringToNumber(const std::string& sString, T &tX)
{
    std::istringstream iStream(sString);
    return !(iStream >> tX).fail();not
}

Then call

double num{};
StringToNumber(std::string{"580.8"}, num);

float fnum{};
StringToNumber(std::string{"5e+1.0"}, fnum);

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
How this answers the question asked by OP?
what does not in ` return !(iStream >> tX).fail();not`?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.