std::string::empty

std::string::empty

public member function - 公开成员函数

1. std::string::empty

C++98
bool empty() const;

C++11
bool empty() const noexcept;

Test if string is empty - 测试字符串是否为空

检查字符串是否为空,检查 string 是否无字符,即是否 begin() == end()

Returns whether the string is empty (i.e. whether its length is 0).
返回字符串是否为空 (即其长度是否为 0)。

This function does not modify the value of the string in any way. To clear the content of a string, see string::clear.
此函数不会以任何方式修改字符串的值。要清除字符串的内容,请参见 string::clear

dereference [ˌdiːˈrefrəns]:v.	 间接引用,间接访问,解引用
reallocate [ˌriːˈæləkeɪt]:v. 重新分配,再指派

2. Parameters

none - 无

3. Return value

true if the string length is 0, false otherwise.
如果字符串长度为 0,则为 true;否则为 false

若 string 为空则为 true,否则为 false。

4. Examples

4.1 std::string::empty

//============================================================================
// Name        : std::string::empty
// 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 content;
	std::string line;

	std::cout << "Please introduce a text. Enter an empty line to finish:\n";

	do
	{
		getline(std::cin, line);
		content += line + '\n';
	} while (!line.empty());

	std::cout << "The text you introduced was:\n" << content;

	return 0;
}

This program reads the user input line by line and stores it into string content until an empty line is introduced.
该程序逐行读取用户输入并将其存储为字符串内容,直到输入空行为止。

Please introduce a text. Enter an empty line to finish:
cheng
yong
qiang

The text you introduced was:
cheng
yong
qiang


4.2 std::string::empty

//============================================================================
// Name        : std::string::empty
// 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;
	std::boolalpha(std::cout);
	std::cout << "str.empty():" << str.empty() << "\t s:'" << str << "'\n";

	str = "Exemplar";
	std::cout << "str.empty():" << str.empty() << "\t s:'" << str << "'\n";

	str = "";
	std::cout << "str.empty():" << str.empty() << "\t s:'" << str << "'\n";

	return 0;
}

str.empty():true	 s:''
str.empty():false	 s:'Exemplar'
str.empty():true	 s:''

5. Complexity - 复杂度

C++98
Unspecified, but generally constant.
未指定,但通常常数。

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. 任务,布置,赋值

References

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

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

猜你喜欢

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