size_type size() const;
(1) (noexcept since C++11)size_type length() const;
(2) (noexcept since C++11)Returns the number of CharT
elements in the string, i.e. std::distance(begin(), end()).
(none)
[edit] Return valueThe number of CharT
elements in the string.
Unspecified
(until C++11)Constant
(since C++11) [edit] NotesFor std::string, the elements are bytes (objects of type char), which are not the same as characters if a multibyte encoding such as UTF-8 is used.
[edit] Example#include <cassert> #include <iterator> #include <string> int main() { std::string s("Exemplar"); assert(8 == s.size()); assert(s.size() == s.length()); assert(s.size() == static_cast<std::string::size_type>( std::distance(s.begin(), s.end()))); std::u32string a(U"ããã¼ã»ã¯ã¼ã«ã"); // 8 code points assert(8 == a.size()); // 8 code units in UTF-32 std::u16string b(u"ããã¼ã»ã¯ã¼ã«ã"); // 8 code points assert(8 == b.size()); // 8 code units in UTF-16 std::string c("ããã¼ã»ã¯ã¼ã«ã"); // 8 code points assert(24 == c.size()); // 24 code units in UTF-8 #if __cpp_lib_char8_t >= 201907L std::u8string d(u8"ããã¼ã»ã¯ã¼ã«ã"); // 8 code points assert(24 == d.size()); // 24 code units in UTF-8 #endif }[edit] See also checks whether the string is empty
std::basic_string_view<CharT,Traits>
) [edit]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4