-3

At How to convert a command-line argument to int? it is shown how to assing a comand line argument to an int.

I'm looking for short way. Not using atoi().

int size;
istringstream s(argv[1]);
s >> size;

With all the cool C++11/14/17 I still need 3 LOC for that? Or is there a int size = magic(argv[1]) around?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • 3
    Your answer is already provided on your linked question `int val = stoi(argv[1]);` – NathanOliver Jul 09 '18 at 12:50
  • The linked question has a C / C++98 answer. C++11 way is similar, but technically different. – lisyarus Jul 09 '18 at 12:52
  • @lisyarus there are few things that are legal C++98, and *differently behaving* in C++11. This is not one of them – Caleth Jul 09 '18 at 13:02
  • @NathanOliver Yes but its not the accepted answer there (because this question is different, it asks for an alternative to `atoi`, shorter then 3LOC). – KcFnMi Jul 09 '18 at 13:07
  • @KcFnMi That doesn't really matter. The accepted answer is just the answer the OP found most helpful. It doesn't mean it the "the answer" to the question. – NathanOliver Jul 09 '18 at 13:10
  • 2
    I did ask the answerer if they would add it so it could be in the top position. – NathanOliver Jul 09 '18 at 13:11

1 Answers1

-1

You can use std::atoi:

int size = std::atoi(argv[1]);

Beware that it provides awful error reporting - it just returns 0 is no conversion can be performed. A better alternative is std::stoi, which accepts an std::string and throws an exception on error:

int size = std::stoi(argv[1]); // note implicit conversion from const char * to std::string
lisyarus
  • 15,025
  • 3
  • 43
  • 68
  • Will I be wrong if I say this `atoi` is not C++? – KcFnMi Jul 09 '18 at 12:52
  • 1
    @KcFnMi - Looks C++ enough to me. It is declared in a C++ standard library header, and is in namespace `std`. – StoryTeller - Unslander Monica Jul 09 '18 at 12:53
  • @KcFnMi I suppose you will, indeed. What makes you say this? – lisyarus Jul 09 '18 at 12:53
  • @KcFnMi It is a C function that was brought into the C++ standard. So, technically, it is a C++ function but it does have a C interface. – NathanOliver Jul 09 '18 at 12:54
  • @KcFnMi I think you are mistaking it for `itoa` which is indeed not standard. – user7860670 Jul 09 '18 at 12:55
  • @lisyarus it comes from `C` `stdlib.h` which in `C++` is provided by `cstdlib`. And some say you should not use `atoi` anymore. But if u guys say `atoi` is the only 1LOC way to do it, even nowadasys, thenI'll accept this as the answer – KcFnMi Jul 09 '18 at 12:56
  • 1
    @KcFnMi `atoi` is a `C` function from ``. `std::atoi` is a `C++` function from ``. They are indeed the same underneath, but `std::atoi` is still a standard `C++` function. – lisyarus Jul 09 '18 at 12:58
  • 1
    It is important to note that `std::stoi` only throws if it can't parse any of the string into a number, `std::stoi("1zzzz")` will not throw and return 1, you need to use the optional pos parameter if you want to ensure the whole string was parsed successfully. – Alan Birtles Jul 09 '18 at 13:59