C++ std::string::size()函数(返回字符串的长度,以字节为单位)(与std::string::length()函数相同)

cppman std::string::size

std::string::size(3)                                                  C++ Programmer's Manual                                                  std::string::size(3)



NAME
       std::string::size - Return length of string	//返回字符串长度

TYPE
       public member function

SYNOPSIS
       #include <string>


       C++98
       size_t size() const;
       size_t size() const noexcept;

DESCRIPTION
       Returns the length of the string, in terms of bytes.
       This is the number of actual bytes that conform the contents of the string, which is not necessarily equal to its storage capacity.
       Note  that  string  objects  handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the
       value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8).
       Both string::size and string::length are synonyms and return the same value.
       //根据字节返回字符串的长度。
        //这是符合字符串内容的实际字节数,不一定等于其存储容量。
        //请注意,字符串对象处理字节而不知道最终可以用来编码其包含的字符的编码。 因此,返回的值可能与多字节或可变长度字符(例如UTF-8)的序列中的实际编码字符数对应。
        //string::size和string::length是同义词,并返回相同的值。

PARAMETERS
       none

RETURN VALUE
       The number of bytes in the string.
       size_t is an unsigned integral type (the same as member type string::size_type).
       //字符串中的字节数。
        //size_t是一种无符号的积分类型(与成员类型字符串:: size_type相同)。

EXAMPLE
         // string::size
         #include <iostream>
         #include <string>
         int main ()
         {
           std::string str ("Test string");
           std::cout << "The size of str is " << str.size() << " bytes.\n";
           return 0;
         }

       Output:
         The size of str is 11 bytes

COMPLEXITY
       C++98 Unspecified.

       C++11 Constant.

ITERATOR VALIDITY
       No changes.

DATA RACES
       The object is accessed.

EXCEPTION SAFETY
       No-throw guarantee: this member function never throws exceptions.

SEE ALSO
       string::size(3)
              Return length of string  (public member function)

       string::resize(3)
              Resize string  (public member function)

       string::max_size(3)
              Return maximum size of string  (public member function)

       string::capacity(3)
              Return size of allocated storage  (public member function)
              //分配存储的返回大小

REFERENCE
       cplusplus.com, 2000-2015 - All rights reserved.



cplusplus.com                                                                2022-05-13                                                      std::string::length(3)
(END)



上面文档代码示例

#pragma warning(disable : 4996)
// string::size
#include <iostream>
#include <string>
int main()
{
    std::string str("Test string");
    std::cout << "The size of str is " << str.size() << " bytes.\n";    //The size of str is 11 bytes.
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值