Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>:: size, std::basic_string<CharT,Traits,Allocator>:: length

From cppreference.net
std::basic_string
size_type size ( ) const ;
(1) (C++11부터 noexcept)
(C++20부터 constexpr)
size_type length ( ) const ;
(2) (C++11부터 noexcept)
(C++20부터 constexpr)

문자열 내 CharT 요소의 개수를 반환합니다. 즉, std:: distance ( begin ( ) , end ( ) ) 입니다.

목차

매개변수

(없음)

반환값

문자열 내 CharT 요소의 개수입니다.

복잡도

미지정

(C++11까지)

상수

(C++11부터)

참고 사항

std::string 의 경우, 요소들은 바이트( char 타입의 객체)이며, UTF-8과 같은 멀티바이트 인코딩이 사용될 경우 문자와 동일하지 않습니다.

예제

#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
}

참고 항목

문자열이 비어 있는지 확인합니다
(public member function)
최대 문자 수를 반환합니다
(public member function)
문자 수를 반환합니다
( std::basic_string_view<CharT,Traits> 의 public member function)