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::getline
std::getline
| å®ä¹äºå¤´æä»¶ <string>
|
||
| template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input, |
(1) | |
| template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input, |
(1) | (C++11 èµ·) |
| template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input, |
(2) | |
| template< class CharT, class Traits, class Allocator > std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>&& input, |
(2) | (C++11 èµ·) |
getline ä»è¾å
¥æµè¯»åå符并å°å®ä»¬æ¾è¿ string ï¼
input.gcount() ãæé å¹¶æ£æ¥ sentry 对象åï¼è¿è¡ä¸åæä½ï¼input éåºå符并åéå®ä»¬å° str ï¼ç´è³åçä¸åæ¡ä»¶ä¹ä¸ï¼æé¡ºåºæ£æ¥ï¼ï¼delim ï¼ä»¥ Traits::eq(c, delim) æµè¯ï¼è¯¥æ
åµä¸ä» input éåºåéå符ï¼ä½ä¸åéå®å° str ãgetline 设置 failbit å¹¶è¿åãåæ°
| input | - | è·åæ°æ®æ¥æºçæµ |
| str | - | æ¾ç½®æ°æ®çç®æ string |
| delim | - | åéå符 |
è¿åå¼
input
注解
æ¶è空ç½ç¬¦åéçè¾å
¥ï¼ä¾å¦ int n; std::cin >> n; ï¼æ¶ï¼ä»»ä½åéç空ç½ç¬¦ï¼å
æ¬æ¢è¡ç¬¦é½ä¼è¢«ç卿µä¸ãç¶åå½åæ¢å°é¢åè¡çè¾å
¥æ¶ï¼ä»¥ getline åå¾çé¦è¡åªä¼æ¯è¯¥ç©ºç½ç¬¦ã夿°æ
åµä¸è¿æ¯ä¸æ³è¦çè¡ä¸ºï¼å¯è½çè§£æ³å
æ¬ï¼
- 对
getlineçæ¾å¼çé¢å¤åå§è°ç¨ - 以 std::cin >> std::ws ç§»é¤é¢å¤ç空ç½ç¬¦
- 以 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 忽ç¥è¾å ¥è¡ä¸å©ä¸çå ¨é¨å符
示ä¾
ä¸åä»£ç æ¼ç¤ºå¦ä½ç¨ getline 彿°è¯»åç¨æ·è¾å
¥ä»¥åå¦ä½éè¡å¤çæä»¶ã
#include <string> #include <iostream> #include <sstream>  int main() { // é®åç¨æ· std::string name; std::cout << "What is your name? "; std::getline(std::cin, name); std::cout << "Hello " << name << ", nice to meet you.\n";  // éè¡è¯»æä»¶ std::istringstream input; input.str("1\n2\n3\n4\n5\n6\n7\n"); int sum = 0; for (std::string line; std::getline(input, line); ) { sum += std::stoi(line); } std::cout << "\nThe sum is: " << sum << "\n"; }
å¯è½çè¾åºï¼
What is your name? John Q. Public Hello John Q. Public, nice to meet you. Â The sum is 28
åé
| ä¸ç´è¯»å¹¶åèµ°å符ï¼ç´è³æ¾å°ç»å®å符 ( std::basic_istream<CharT,Traits> çå
¬å¼æå彿°) |