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::stoi, std::stol, std::stoll
std::stoi, std::stol, std::stoll
| å®ä¹äºå¤´æä»¶ <string>
|
||
| int    stoi( const std::string& str, std::size_t* pos = 0, int base = 10 ); int    stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); |
(1) | (C++11 èµ·) |
| long    stol( const std::string& str, std::size_t* pos = 0, int base = 10 ); long    stol( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); |
(2) | (C++11 èµ·) |
| long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 ); long long stoll( const std::wstring& str, std::size_t* pos = 0, int base = 10 ); |
(3) | (C++11 èµ·) |
转è¯å符串 str ä¸çæç¬¦å·æ´æ°å¼ã
è弿æç©ºç½ç¬¦ï¼ä»¥è°ç¨ isspace() é´å«ï¼ï¼ç´å°æ¾å°é¦ä¸ªé空ç½ç¬¦ï¼ç¶ååå°½å¯è½å¤çåç¬¦ç»æåº n ï¼å
¶ä¸ n=base ï¼çæ´æ°è¡¨ç¤ºï¼å¹¶å°å®ä»¬è½¬æ¢æä¸ä¸ªæ´æ°å¼ãåæ³çæ´æ°å¼ç±ä¸åé¨åç»æï¼
- (å¯é)æ£æè´å·
- (å¯é)æç¤ºå
«è¿å¶åºçåç¼ï¼
0ï¼ï¼ä» å½åºä¸º 8 æ â0â æ¶åºç¨ï¼ - (å¯é)æç¤ºåå
è¿å¶åºçåç¼ï¼
0xæ0Xï¼ï¼ä» å½åºä¸º 16 æ â0â æ¶åºç¨ï¼ - ä¸ä¸ªæ°ååºå
åºç忳鿝 {0,2,3,...,36} ãåæ³æ°åé对äºåº 2 æ´æ°æ¯ {0,1}ï¼å¯¹äºåº3æ´æ°æ¯ {0,1,2} ï¼ä»¥æ¤ç±»æ¨ã对äºå¤§äº 10 çåºï¼åæ³æ°åå
å«åæ¯å符ï¼ä»å¯¹äºåº 11 æ´æ°ç Aa å°å¯¹äºåº36æ´æ°ç Zz ã忽ç¥å符大å°åã
å½åå®è£ ç C æ¬å°ç¯å¢å¯è½æ¥åå¦å¤çæ°åæ ¼å¼ã
è¥ base 为 â0â ï¼åèªå¨æ£æµæ°å¼è¿å¶ï¼è¥åç¼ä¸º 0 ï¼ååºä¸ºå
«è¿å¶ï¼è¥åç¼ä¸º 0x æ 0X ï¼ååºä¸ºåå
è¿å¶ï¼å¦ååºä¸ºåè¿å¶ã
è¥ç¬¦å·æ¯è¾å ¥åºåçä¸é¨åï¼å坹仿°ååºå计ç®å¾æ¥çæ°åå¼ååï¼å¦åç¨ç»æç±»åçä¸å åã
è¥ pos 䏿¯ç©ºæéï¼åæé ptr ââ转æ¢å½æ°å
é¨è
ââå°æ¥æ¶ str.c_str() ä¸é¦ä¸ªæªè½¬æ¢å符çå°åï¼å°è®¡ç®è¯¥å符ç䏿 å¹¶åå¨ä¹äº *pos ï¼è¯¥å¯¹è±¡ç»åºè½¬æ¢æå¤ççå符æ°ã
åæ°
| str | - | è¦è½¬æ¢çå符串 |
| pos | - | åå¨å·²å¤çå符æ°çæ´æ°çå°å |
| base | - | æ°çåº |
è¿åå¼
å¯¹åº str å
å®¹çæ´æ°å¼ã
å¼å¸¸
- è¥ä¸è½è¿è¡è½¬æ¢å为 std::invalid_argument
- è¥è½¬æ¢å¼ä¼è½å¨ç»æç±»åçèå´å¤ï¼æè¥åºå±å½æ°ï¼ std::strtol æ std::strtoll ï¼è®¾ç½® errno 为 ERANGE å为 std::out_of_range ã
示ä¾
#include <iostream> #include <string>  int main() { std::string str1 = "45"; std::string str2 = "3.14159"; std::string str3 = "31337 with words"; std::string str4 = "words and 2";  int myint1 = std::stoi(str1); int myint2 = std::stoi(str2); int myint3 = std::stoi(str3); // éè¯¯ï¼ 'std::invalid_argument' // int myint4 = std::stoi(str4);  std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n'; std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n'; std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n'; //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n'; }
è¾åºï¼
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337åé
| 转æ¢åèåç¬¦ä¸²ä¸ºæ´æ°å¼ (彿°) | |
| (C++11)(C++11) |
转æ¢å符串为æ ç¬¦å·æ´æ° (彿°) |
| (C++11)(C++11)(C++11) |
转æ¢å符串为浮ç¹å¼ (彿°) |
| (C++11) |
è½¬æ¢æ´æ°ææµ®ç¹å¼ä¸º string (彿°) |
| (C++17) |
转æ¢å符åºåå°æ´æ°ææµ®ç¹å¼ (彿°) |