22

Is there a way to convert std::string to size_t? The problem is that size_t is platform dependable type (while it is the result of the sizeof). So, I can not guarantee that converting string to unsigned long or unsigned int will do it correctly.

EDIT: A simple case is:

std::cout<< "Enter the index:";
std::string input;
std::cin >> input;
size_t index=string_to_size_t(input);
//Work with index to do something
7
  • 3
    What do you mean by converting string to size_t are you trying to convert a string of digits? Please post some code and explain what you're asking
    – EdChum
    Dec 2, 2015 at 13:26
  • Have a look at en.cppreference.com/w/cpp/string/basic_string/stol
    – 0x6773
    Dec 2, 2015 at 13:31
  • What's stopping someone from inputting a huge number that exceeds your numeric limits?
    – EdChum
    Dec 2, 2015 at 13:32
  • This another problem. For now is there a stol like method that work for size_t Dec 2, 2015 at 13:34
  • 5
    What is the problem with just doing size_t index; cin >> index;? Dec 2, 2015 at 13:40

6 Answers 6

22

you can use std::stringstream

std::string string = "12345";
std::stringstream sstream(string);
size_t result;
sstream >> result;
std::cout << result << std::endl;
12

You may want to use sscanf with the %zu specifier, which is for std::size_t.

sscanf(input.c_str(), "%zu", &index);

Have a look here.

Literally, I doubt that there is an overloaded operator >> of std::basic_istringstream for std::size_t. See here.

1
9

Let us assume for a minute that size_t is a typedef to an existing integer, i.e. the same width as either unsigned int, unsigned long, or unsigned long long.

AFAIR it could be a separate (larger still) type as far as the standard wording is concerned, but I consider that to be highly unlikely.

Working with that assumption that size_t is not larger than unsigned long long, either stoull or strtoull with subsequent cast to size_t should work.


From the same assumption (size_t defined in terms of either unsigned long or unsigned long long), there would be an operator>> overload for that type.

5
  • Tha is exactly what I am doing right now.. but I was wondering if there is a cleaner solution Dec 2, 2015 at 13:44
  • I'd plump for %zd: that's been around since the C99 standard. Not sure about C++ but I imagine it's been around fort the same amount of time.
    – Bathsheba
    Dec 2, 2015 at 13:48
  • @Bathsheba: I have a deep, almost genetic dislike for the whole *scanf() family. I happen to have implemented them, so it's not a dislike-from-ignorance, but quite the opposite. I might use them in a pinch, but you will never see me recommending them to anyone, especially not in a C++ context. ;-)
    – DevSolar
    Dec 2, 2015 at 13:51
  • I do agree with you although I cannot claim to have implemented the family myself. But I do feel that this particular case is an exception to the rule. Plus one, since you answer obviously contains hidden depths
    – Bathsheba
    Dec 2, 2015 at 13:53
  • 1
    @Bathsheba: Related SO question by myself, back from when I was implementing *scanf(). The summary is that the input string "0xz", if parsed by *scanf(), is a matching failure leaving the argument undefined (and the input pointer being at 'x'...) -- unless, of course, you're looking at a non-compliant implementation like glibc-2.8 or newlib 1.14. ;-) That's only one thing that made me detest *scanf(). ;-)
    – DevSolar
    Dec 2, 2015 at 13:59
7

You can use %zd as the format specifier in a scanf-type approach.

Or use a std::stringstream which will have an overloaded >> to size_t.

6
  • I doubt that there is an overloaded operator >> for std::size_t. See here.
    – Lingxi
    Dec 2, 2015 at 13:39
  • As I understood, There is no overload for size_t... but since Size_t is uint or ulong or ulongong... it would work for the right type of size_t ? Dec 2, 2015 at 13:43
  • Insofar that std::size_t will probably (perhaps certainly; need to check) be defined in terms of one of the primitive integral types, there will be. A compilation error would result if not.
    – Bathsheba
    Dec 2, 2015 at 13:46
  • @Bathsheba: I only have the C99 paper here, but that doesn't say anything about size_t except where it is defined, that it is an unsigned integer type, the return type of sizeof(), and has a maximum value (SIZE_MAX) of no less than 65535.
    – DevSolar
    Dec 2, 2015 at 14:24
  • Correct specifier for size_t is %zu, not %zd. Feb 13, 2021 at 4:50
2
/**
   * @brief  Convert const char* to size_t
   * @note   When there is an error it returns the maximum of size_t
   * @param  *number: const char* 
   * @retval size_t
   */
  size_t to_size_t(const char *number) {
    size_t sizeT;
    std::istringstream iss(number);
    iss >> sizeT;
    if (iss.fail()) {
      return std::numeric_limits<size_t>::max();
    } else {
      return sizeT;
    }
  }
1
#include <sstream>

std::istringstream iss("a");
size_t size;
iss >> size;

By using iss.fail(), you check failure. Instead of ("a"), use value you want to convert.

1
  • I think you mean >>, not >.
    – Bathsheba
    Dec 2, 2015 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.