Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::to_string

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      Defined in header<string>
      std::string to_string(int value);
      (1)(since C++11)
      std::string to_string(long value);
      (2)(since C++11)
      std::string to_string(longlong value);
      (3)(since C++11)
      std::string to_string(unsigned value);
      (4)(since C++11)
      std::string to_string(unsignedlong value);
      (5)(since C++11)
      std::string to_string(unsignedlonglong value);
      (6)(since C++11)
      std::string to_string(float value);
      (7)(since C++11)
      std::string to_string(double value);
      (8)(since C++11)
      std::string to_string(longdouble value);
      (9)(since C++11)

      Converts a numeric value tostd::string.

      Letbuf be an internal to the conversion functions buffer, sufficiently large to contain the result of conversion.

      1) Converts a signed integer to a string as if bystd::sprintf(buf,"%d", value).
      2) Converts a signed integer to a string as if bystd::sprintf(buf,"%ld", value).
      3) Converts a signed integer to a string as if bystd::sprintf(buf,"%lld", value).
      4) Converts an unsigned integer to a string as if bystd::sprintf(buf,"%u", value).
      5) Converts an unsigned integer to a string as if bystd::sprintf(buf,"%lu", value).
      6) Converts an unsigned integer to a string as if bystd::sprintf(buf,"%llu", value).
      7,8) Converts a floating point value to a string as if bystd::sprintf(buf,"%f", value).
      9) Converts a floating point value to a string as if bystd::sprintf(buf,"%Lf", value).
      (until C++26)
      1-9) Converts a numeric value to a string as if bystd::format("{}", value).
      (since C++26)

      Contents

      [edit]Parameters

      value - a numeric value to convert

      [edit]Return value

      A string holding the converted value.

      [edit]Exceptions

      May throwstd::bad_alloc from thestd::string constructor.

      [edit]Notes

      • With floating point typesstd::to_string may yield unexpected results as the number of significant digits in the returned string can be zero, see the example.
      • The return value may differ significantly from whatstd::cout prints by default, see the example.
      • std::to_string relies on the current C locale for formatting purposes, and therefore concurrent calls tostd::to_string from multiple threads may result in partial serialization of calls.
        • The results of overloads for integer types do not rely on the current C locale, and thus implementations generally avoid access to the current C locale in these overloads for both correctness and performance. However, such avoidance is not guaranteed by the standard.
      (until C++26)

      C++17 providesstd::to_chars as a higher-performance locale-independent alternative.

      Feature-test macroValueStdFeature
      __cpp_lib_to_string202306L(C++26)Redefiningstd::to_string in terms ofstd::format

      [edit]Example

      Run this code
      #include <cstdio>#include <format>#include <initializer_list>#include <iostream>#include <string> #if __cpp_lib_to_string >= 202306Lconstexprauto revision(){return" (post C++26)";}#elseconstexprauto revision(){return" (pre C++26)";}#endif int main(){for(constdouble f:{1.23456789555555,23.43,1e-9,1e40,1e-40,123456789.0}){std::cout<<"to_string:\t"<< std::to_string(f)<< revision()<<'\n'; // Before C++26, the output of std::to_string matches std::printf.std::printf("printf:\t\t%f\n", f); // As of C++26, the output of std::to_string matches std::format.std::cout<<std::format("format:\t\t{}\n", f); std::cout<<"std::cout:\t"<< f<<"\n\n";}}

      Possible output:

      to_string:      1.234568 (pre C++26)printf:         1.234568format:         1.23456789555555std::cout:      1.23457 to_string:      23.430000 (pre C++26)printf:         23.430000format:         23.43std::cout:      23.43 to_string:      0.000000 (pre C++26)printf:         0.000000format:         1e-09std::cout:      1e-09 to_string:      10000000000000000303786028427003666890752.000000 (pre C++26)printf:         10000000000000000303786028427003666890752.000000format:         1e+40std::cout:      1e+40 to_string:      0.000000 (pre C++26)printf:         0.000000format:         1e-40std::cout:      1e-40 to_string:      123456789.000000 (pre C++26)printf:         123456789.000000format:         123456789std::cout:      1.23457e+08

      [edit]See also

      (C++11)
      converts an integral or floating-point value towstring
      (function)[edit]
      (C++11)(C++11)
      converts a string to an unsigned integer
      (function)[edit]
      (C++11)(C++11)(C++11)
      converts a string to a signed integer
      (function)[edit]
      (C++11)(C++11)(C++11)
      converts a string to a floating point value
      (function)[edit]
      (C++17)
      converts an integer or floating-point value to a character sequence
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/to_string&oldid=171806"

      [8]ページ先頭

      ©2009-2025 Movatter.jp