std::string::size

std::string::size

public member function - 公开成员函数

1. std::string::size

C++98
size_t size() const;

C++11
size_t size() const noexcept;

Return length of string - 返回字符串的长度

返回字符数。返回 string 中的 CharT 元素数,即 std::distance(begin(), end())

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).
请注意,字符串对象在不了解可能最终用于对其包含的字符进行编码的情况下处理字节。因此,返回的值可能不对应于多字节或可变长度字符 (例如 UTF-8) 序列中编码字符的实际数目。

Both string::size and string::length are synonyms and return the same value.
string::size and string::length 都是同义词,并且返回相同的值。

conform [kənˈfɔːm]:vi. 符合,遵照,适应环境 vt. 使遵守,使一致,使顺从 adj. 一致的,顺从的
dereference [ˌdiːˈrefrəns]:v.	 间接引用,间接访问,解引用
reallocate [ˌriːˈæləkeɪt]:v. 重新分配,再指派

2. Parameters

none - 无

3. 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 是无符号整数类型 (与成员类型 string::size_type 相同)。

对于 std::string,元素是字节 (char 类型对象),若使用如 UTF-8 的多字节编码,则它与字符不同。

4. Examples

4.1 std::string::size

//============================================================================
// Name        : std::string::size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>

int main()
{
	std::string str("Test string");
	std::cout << "The size of str is " << str.size() << " bytes.\n";

	return 0;
}

The size of str is 11 bytes.

4.2 std::string::size

//============================================================================
// Name        : std::string::size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cassert>
#include <iterator>
#include <string>

int main()
{
	std::string str("example");

	assert(7 == str.size());
	assert(str.size() == str.length());
	assert(str.size() == static_cast<std::string::size_type>(std::distance(str.begin(), str.end())));

	std::u32string a(U"cheng"); // 5 code points
	assert(5 == a.size()); // 5 code units in UTF-32
	std::cout << "The size of str is " << a.size() << " bytes.\n";

	std::u16string b(u"yong"); // 4 code points
	assert(4 == b.size()); // 4 code units in UTF-16
	std::cout << "The size of str is " << b.size() << " bytes.\n";

	std::string c(u8"qiang"); // 5 code points
	assert(5 == c.size()); // 5 code units in UTF-8
	std::cout << "The size of str is " << c.size() << " bytes.\n";

	std::cout << "yongqiang." << "\n";

	return 0;
}

The size of str is 5 bytes.
The size of str is 4 bytes.
The size of str is 5 bytes.
yongqiang.

5. Complexity - 复杂度

C++98
Unspecified.

C++11
Constant. - 常数。

destruction [dɪˈstrʌkʃn]:n. 破坏,毁灭,摧毁
trivially [ˈtrɪviəli]:adv. 琐细地,平凡地,无能地
destructible [dɪ'strʌktɪb(ə)l]:adj. 可破坏的,易损坏的

6. Iterator validity - 迭代器有效性

No changes.

7. Data races - 数据竞争

The object is accessed.
对象被访问。

8. Exception safety - 异常安全性

No-throw guarantee: this member function never throws exceptions.
无抛出保证:此成员函数从不抛出异常。

assignment [ə'saɪnmənt]:n. 任务,布置,赋值

9. Trouble Shooting

//============================================================================
// Name        : std::string::size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

//#include <iostream>
#include <cassert>
#include <iterator>
#include <string>

int main()
{
	std::string str("example");

	assert(7 == str.size());
	assert(str.size() == str.length());
	assert(str.size() == static_cast<std::string::size_type>(std::distance(str.begin(), str.end())));

	std::u32string a(U"cheng"); // 5 code points
	assert(5 == a.size()); // 5 code units in UTF-32
	std::cout << "The size of str is " << a.size() << " bytes.\n";

	std::u16string b(u"yong"); // 4 code points
	assert(4 == b.size()); // 4 code units in UTF-16
	std::cout << "The size of str is " << b.size() << " bytes.\n";

	std::string c(u8"qiang"); // 5 code points
	assert(5 == c.size()); // 5 code units in UTF-8
	std::cout << "The size of str is " << c.size() << " bytes.\n";

	std::cout << "yongqiang." << "\n";

	return 0;
}

22:56:59 **** Build of configuration Debug for project hello_world ****
make all 
Building file: ../src/hello_world.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello_world.d" -MT"src/hello_world.o" -o "src/hello_world.o" "../src/hello_world.cpp"
../src/hello_world.cpp: In function ‘int main()’:
../src/hello_world.cpp:24:2: error: ‘cout’ is not a member of ‘std’
  std::cout << "The size of str is " << a.size() << " bytes.\n";
  ^
../src/hello_world.cpp:28:2: error: ‘cout’ is not a member of ‘std’
  std::cout << "The size of str is " << b.size() << " bytes.\n";
  ^
../src/hello_world.cpp:32:2: error: ‘cout’ is not a member of ‘std’
  std::cout << "The size of str is " << c.size() << " bytes.\n";
  ^
../src/hello_world.cpp:34:2: error: ‘cout’ is not a member of ‘std’
  std::cout << "yongqiang." << "\n";
  ^
make: *** [src/hello_world.o] Error 1
src/subdir.mk:18: recipe for target 'src/hello_world.o' failed

22:56:59 Build Finished (took 621ms)
//============================================================================
// Name        : std::string::size
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cassert>
#include <iterator>
#include <string>

int main()
{
	std::string str("example");

	assert(7 == str.size());
	assert(str.size() == str.length());
	assert(str.size() == static_cast<std::string::size_type>(std::distance(str.begin(), str.end())));

	std::u32string a(U"cheng"); // 5 code points
	assert(5 == a.size()); // 5 code units in UTF-32
	std::cout << "The size of str is " << a.size() << " bytes.\n";

	std::u16string b(u"yong"); // 4 code points
	assert(4 == b.size()); // 4 code units in UTF-16
	std::cout << "The size of str is " << b.size() << " bytes.\n";

	std::string c(u8"qiang"); // 5 code points
	assert(5 == c.size()); // 5 code units in UTF-8
	std::cout << "The size of str is " << c.size() << " bytes.\n";

	std::cout << "yongqiang." << "\n";

	return 0;
}

References

http://www.cplusplus.com/reference/string/string/size/
https://en.cppreference.com/w/cpp/string/basic_string/size

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104381119