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::basic_string<CharT,Traits,Allocator>::basic_string
std::basic_string<CharT,Traits,Allocator>::basic_string
  basic_string( Allocator() ) {}
(C++20 å)
  basic_string( Allocator() ) {}
       CharT ch,
            CharT ch,
       size_type pos,
       size_type count = std::basic_string::npos,
       size_type pos,
(C++20 å)
            size_type pos,
       size_type pos,
       size_type count,
(C++20 å)
            size_type pos,
            size_type count,
       size_type count,
            size_type count,
       const Allocator& alloc = Allocator() );
            const Allocator& alloc = Allocator() );
basic_string( InputIt first, InputIt last,
constexpr basic_string( InputIt first, InputIt last,
(C++20 å)
(C++20 å)
(C++20 å)
       const Allocator& alloc = Allocator() );
(C++20 å)
            const Allocator& alloc = Allocator() );
explicit basic_string( const T& t, const Allocator& alloc = Allocator() );
(C++20 å)
explicit constexpr basic_string( const T& t,
basic_string( const T& t, size_type pos, size_type n,
(C++20 å)
constexpr basic_string( const T& t, size_type pos, size_type n,
ä»åç§æ°æ®æºæé æ° string ï¼å¯éå°ä½¿ç¨ç¨æ·æä¾çåé
å¨ alloc ã
ch ç count ä¸ªå¯æ¬ç string ãè¥ä¼æ¨å¯¼åºä¸è¶³ä»¥ä½ä¸ºåé
å¨ç Allocator ç±»åï¼åæ¤æé 彿°ä¸ç¨äºç±»æ¨¡æ¿å®åæ¨å¯¼ã (C++17 èµ·)other çå串 [pos, pos+count) æé string ãè¥ count == npos ææªæå® count ï¼æè¥è¯·æ±çå串è¶è¿å符串çç»å°¾ï¼å产ççå串为 [pos, other.size()) ãs ææåçå符串çé¦ count 个å符æé string ã s è½å
å«ç©ºå符ã string çé¿åº¦ä¸º count ãè¥ [s, s + count) 䏿¯åæ³èå´åè¡ä¸ºæªå®ä¹ãs ææåçç©ºç»æ¢å符串ç坿¬æåå§åçå
容æé string ã以é¦ä¸ªç©ºå符确å®å符串çé¿åº¦ãè¥ [s, s + Traits::length(s)) 䏿¯åæ³èå´ï¼ä¾å¦è¥ s æ¯ç©ºæéï¼åè¡ä¸ºæªå®ä¹ãè¥ä¼æ¨å¯¼åºä¸è¶³ä»¥ä½ä¸ºåé
å¨ç Allocator ç±»åï¼åæ¤æé 彿°ä¸ç¨äºç±»æ¨¡æ¿å®åæ¨å¯¼ã (C++17 èµ·)[first, last) å
容ç string ã
|
è¥ |
(C++11 å) |
|
æ¤æé 彿°ä»
è¥ |
(C++11 èµ·) |
other å
容坿¬ç string ãother å
容ç string ãå° other çå¨åæ³ä½æªæå®çç¶æãilist å
容ç string ãt 为 string_view sv ï¼ç¶åå¦åç¨ basic_string(sv.data(), sv.size(), alloc) ï¼ä»¥ sv çå
容åå§å string ãæ¤éè½½ä»
è¥ std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true ä¸ std::is_convertible_v<const T&, const CharT*> 为 false æåä¸éè½½å³è®®ãt 为 string_view sv ï¼ç¶åå¦åç¨ basic_string(sv.substr(pos, n), a) ï¼ä»¥ sv çåèå´ [pos, pos + n) åå§å string ãæ¤éè½½ä»
è¥ std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true æåä¸éè½½å³è®®ã åæ°
| alloc | - | ç¨äºæ¤ string ææå ååé çåé å¨ |
| count | - | 产çç string å¤§å° |
| ch | - | åå§å string æç¨çå¼ |
| pos | - | è¦å å«çé¦å符ä½ç½® |
| first, last | - | å¤å¶åç¬¦çæ¥æºèå´ |
| s | - | æåç¨ä½æºåå§å string çå符æ°ç»çæé |
| other | - | ç¨ä½æºåå§å string çå¦ä¸ string |
| ilist | - | åå§å string æç¨ç std::initializer_list |
| t | - | åå§å string æç¨ç对象ï¼å¯è½¬æ¢ä¸º std::basic_string_view ï¼ |
å¤æåº¦
count æçº¿æ§s çé¿åº¦æçº¿æ§first å last é´çè·ç¦»æçº¿æ§other çå¤§å°æçº¿æ§alloc ä¸ alloc != other.get_allocator() å为线æ§ilist çå¤§å°æçº¿æ§å¼å¸¸
è¥æé çå符串é¿åº¦ä¼è¶
åº max_size() ï¼ä¾å¦å¯¹äº (2) ï¼è¥ count > max_size() ï¼åæåº std::length_error ãè°ç¨ Allocator::allocate å¯è½æåºã
注æ
以å«å
åµ '\0' å符çå符串åé¢éåå§å string ä¼ä½¿ç¨éè½½ (5) ï¼å¹¶å¨é¦ä¸ªç©ºåç¬¦åæ¢ãè¿å¯éè¿æå®ä¸åçæé 彿°ï¼æéè¿ä½¿ç¨ operator""s é¿å
ï¼
std::string s1 = "ab\0\0cd"; // s1 å« "ab" std::string s2{"ab\0\0cd", 6}; // s2 å« "ab\0\0cd" std::string s3 = "ab\0\0cd"s; // s3 å« "ab\0\0cd"
ç¼ºé·æ¥å
ä¸åæ´æ¹è¡ä¸ºçç¼ºé·æ¥å追溯å°åºç¨äºä»¥ååºçç C++ æ åã
| DR | åºç¨äº | åºçæ¶çè¡ä¸º | æ£ç¡®è¡ä¸º |
|---|---|---|---|
| LWG 2193 | C++11 | é»è®¤æé 彿°ä¸º explicit | 使ä¹ä¸ºé explicit |
| LWG 2946 | C++17 | string_view éè½½å¨ä¸äºæ
åµä¸å¯¼è´æ§ä¹
|
éè¿ä½¿ä¹ä¸ºæ¨¡æ¿æ¥é¿å |
| LWG 3076 | C++17 | äºä¸ªæé 彿°å¯è½å¨ç±»æ¨¡æ¿å®åæ¨å¯¼ä¸å¯¼è´æ§ä¹ | å·²å¶çº¦ |
示ä¾
#include <iostream> #include <cassert> #include <iterator> #include <string> #include <cctype>  int main() { { // string::string() std::string s; assert(s.empty() && (s.length() == 0) && (s.size() == 0)); }  { // string::string(size_type count, charT ch) std::string s(4, '='); std::cout << s << '\n'; // "====" }  { std::string const other("Exemplary"); // string::string(string const& other, size_type pos, size_type count) std::string s(other, 0, other.length()-1); std::cout << s << '\n'; // "Exemplar" }  { // string::string(charT const* s, size_type count) std::string s("C-style string", 7); std::cout << s << '\n'; // "C-style" }  { // string::string(charT const* s) std::string s("C-style\0string"); std::cout << s << '\n'; // "C-style" }  { char mutable_c_str[] = "another C-style string"; // string::string(InputIt first, InputIt last) std::string s(std::begin(mutable_c_str)+8, std::end(mutable_c_str)-1); std::cout << s << '\n'; // "C-style string" }  { std::string const other("Exemplar"); std::string s(other); std::cout << s << '\n'; // "Exemplar" }  { // string::string(string&& str) std::string s(std::string("C++ by ") + std::string("example")); std::cout << s << '\n'; // "C++ by example" }  { // string(std::initializer_list<charT> ilist) std::string s({ 'C', '-', 's', 't', 'y', 'l', 'e' }); std::cout << s << '\n'; // "C-style" }  { // éè½½å³è®®éæ© string(InputIt first, InputIt last) [with InputIt = int] // è¿è¡¨ç°ä¸ºå¦åè°ç¨ string(size_type count, charT ch) std::string s(3, std::toupper('a')); std::cout << s << '\n'; // "AAA" } }
è¾åºï¼
==== Exemplar C-style C-style C-style string Exemplar C++ by example C-style AAA
åé
| èµå¼å符ç»å符串 (å ¬å¼æå彿°) | |
| 为å符串èµå¼ (å ¬å¼æå彿°) | |
| (C++11) |
è½¬æ¢æ´æ°ææµ®ç¹å¼ä¸º string (彿°) |
| (C++11) |
è½¬æ¢æ´æ°ææµ®ç¹å¼ä¸º wstring (彿°) |