C++ std::string::size()函数(返回字符串的长度,以字节为单位)(与std::string::length()函数相同)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
以上是关于C++ std::string::size()函数(返回字符串的长度,以字节为单位)(与std::string::length()函数相同)的主要内容,如果未能解决你的问题,请参考以下文章