Advertisement
Naohiro19

C++で引数の数値を進数文字列に変換(C++17必須)

Jan 26th, 2022
1,052
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <iostream>
  3. #include <iterator>
  4. #include <string>
  5. #include <algorithm>
  6. #include <string_view>
  7. #include <charconv>
  8.  
  9. std::string to_decimal_string(const int& number, const int& base)
  10. {
  11.     char out[50]{};
  12.     auto begin = std::begin(out);
  13.     auto end = std::end(out);
  14.     std::string res;
  15.     if (auto [ptr, ec] = std::to_chars(begin, end, number, base); ec == std::errc{}) {
  16.         res.assign(begin, ptr - begin);
  17.     }
  18.     return res;
  19. }
  20.  
  21. int main()
  22. {
  23.     std::cout << to_decimal_string(100, 2) << std::endl;
  24.  
  25. }
  26.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement