std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
size_type size() const; |
(C++11未満) | |
size_type size() const noexcept; |
(C++11以上) (C++20未満) |
|
constexpr size_type size() const noexcept; |
(C++20以上) | |
size_type length() const; |
(C++11未満) | |
size_type length() const noexcept; |
(C++11以上) (C++20未満) |
|
constexpr size_type length() const noexcept; |
(C++20以上) | |
文字列内の CharT 要素の数、すなわち std::distance(begin(), end()) を返します。
引数
(なし)
戻り値
文字列内の CharT 要素の数。
計算量
|
未規定。 |
(C++11未満) |
|
一定。 |
(C++11以上) |
ノート
std::string の要素は「バイト」 (char 型のオブジェクト) であり、これは UTF-8 のようなマルチバイトエンコーディングが使用される場合、「文字」と同じではありません。
例
Run this code
#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(u8"ハロー・ワールド"); // 8 code points
assert(24 == c.size()); // 24 code units in UTF-8
}
関連項目
| 文字列が空かどうか調べます (パブリックメンバ関数) | |
| 最大文字数を返します (パブリックメンバ関数) |