C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string çæ¨å¯¼æå¼
- std::basic_string_view
- ç©ºç»æ¢åèå符串
- ç©ºç»æ¢å¤åèå符串
- ç©ºç»æ¢å®½å符串
- std::char_traits
- 注é
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >åç¬¦ä¸²åº >std::basic_string > std::to_string
std::to_string
| å®ä¹äºå¤´æä»¶ <string>
|
||
| std::string to_string( int value ); |
(1) | (C++11 èµ·) |
| std::string to_string( long value ); |
(2) | (C++11 èµ·) |
| std::string to_string( long long value ); |
(3) | (C++11 èµ·) |
| std::string to_string( unsigned value ); |
(4) | (C++11 èµ·) |
| std::string to_string( unsigned long value ); |
(5) | (C++11 èµ·) |
| std::string to_string( unsigned long long value ); |
(6) | (C++11 èµ·) |
| std::string to_string( float value ); |
(7) | (C++11 èµ·) |
| std::string to_string( double value ); |
(8) | (C++11 èµ·) |
| std::string to_string( long double value ); |
(9) | (C++11 èµ·) |
1) ææç¬¦å·åè¿å¶æ´æ°è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%d", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åã2) ææç¬¦å·åè¿å¶æ´æ°è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%ld", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åã3) ææç¬¦å·åè¿å¶æ´æ°è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%lld", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åã4) ææ ç¬¦å·åè¿å¶æ´æ°è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%u", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åã5) ææ ç¬¦å·åè¿å¶æ´æ°è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%lu", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åã6) ææ ç¬¦å·åè¿å¶æ´æ°è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%llu", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åã7,8) ææµ®ç¹å¼è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%f", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åã9) ææµ®ç¹å¼è½¬æ¢ä¸ºå符串ï¼ä¸ std::sprintf(buf, "%Lf", value) å¨æè¶³å¤å¤§ç
buf æ¶äº§ççå
容ç¸åãåæ°
| value | - | éè¦è½¬æ¢çæ°å¼ |
è¿åå¼
ä¸ä¸ªå å«è½¬æ¢åå¼çå符串
å¼å¸¸
å¯è½ä» std::string çæé 彿°æåº std::bad_alloc ã
注æ
- å¯¹äºæµ®ç¹ç±»åï¼
std::to_stringå¯è½äº§çä¸æå¾ çç»æï¼å 为è¿åçå符串ä¸çææä½æ°è½ä¸ºé¶ï¼è§ç¤ºä¾ã - è¿åå¼å¯ä»¥ææ¾å°æå«äº
std::coutæé»è®¤æå°çç»æï¼è§ç¤ºä¾ã -
std::to_stringç±äºæ ¼å¼åç®çä¾èµæ¬å°ç¯å¢ï¼ä»èä»å¤ä¸ªçº¿ç¨åæ¶è°ç¨std::to_stringå¯è½ä¼å¯¼è´è°ç¨çé¨ååºååç»æã C++17 æä¾é«æ§è½ãä¸ä¾èµæ¬å°ç¯å¢çæ¿ç¨å std::to_chars ã
示ä¾
è¿è¡æ¤ä»£ç
#include <iostream> #include <string>  int main() { double f = 23.43; double f2 = 1e-9; double f3 = 1e40; double f4 = 1e-40; double f5 = 123456789; std::string f_str = std::to_string(f); std::string f_str2 = std::to_string(f2); // 注æï¼è¿å "0.000000" std::string f_str3 = std::to_string(f3); // 注æï¼ä¸è¿å "1e+40". std::string f_str4 = std::to_string(f4); // 注æï¼è¿å "0.000000" std::string f_str5 = std::to_string(f5); std::cout << "std::cout: " << f << '\n' << "to_string: " << f_str << "\n\n" << "std::cout: " << f2 << '\n' << "to_string: " << f_str2 << "\n\n" << "std::cout: " << f3 << '\n' << "to_string: " << f_str3 << "\n\n" << "std::cout: " << f4 << '\n' << "to_string: " << f_str4 << "\n\n" << "std::cout: " << f5 << '\n' << "to_string: " << f_str5 << '\n'; }
è¾åºï¼
std::cout: 23.43 to_string: 23.430000 Â std::cout: 1e-09 to_string: 0.000000 Â std::cout: 1e+40 to_string: 10000000000000000303786028427003666890752.000000 Â std::cout: 1e-40 to_string: 0.000000 Â std::cout: 1.23457e+08 to_string: 123456789.000000
åé
| (C++11) |
è½¬æ¢æ´æ°ææµ®ç¹å¼ä¸º wstring (彿°) |
| (C++11)(C++11) |
转æ¢å符串为æ ç¬¦å·æ´æ° (彿°) |
| (C++11)(C++11)(C++11) |
转æ¢å符串为æç¬¦å·æ´æ° (彿°) |
| (C++11)(C++11)(C++11) |
转æ¢å符串为浮ç¹å¼ (彿°) |
| (C++17) |
è½¬æ¢æ´æ°ææµ®ç¹å¼å°å符åºå (彿°) |