您的位置首页百科知识

string类empty()函数

string类empty()函数

的有关信息介绍如下:

string类empty()函数

字符串是空的话返回是true

void Empty( );

说明:

此成员函数用来使CString对象成为一个空字符串,并释放相应的内存。

更多的信息,参见“Visual C++程序员指南”中的“字符串:CString异常清除”。

示例:下面的例子说明了如何使用CString::Empty。

// example for CString::Empty

CString s( "abc" );

s.Empty();

ASSERT( s.GetLength( ) == 0 );

请参阅 CString::IsEmpty

BOOL empty();

函数功能:

测试string是否为空,返回此string对象是否为空,(即string对象的长度是否为0)。

函数说明:

此函数不会修改string对象。若要清空string对象的内容,见string::clear。

返回值:

如果string长度为0,则返回true,反之则返回false。

例:

// string::empty

#include

#include

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;

}

此程序功能是一行一行读取用户输入并存入content变量,直到输入一个空行为止。