将string的成员函数用c_str()把string转换为const char *
string str1 = “ssd”;
string str2 = “sss”;
strcmp(str1.c_str(), str2.c_str());
#include
#include
#include
using namespace std;
int main()
{
string word;
int i = 0;
cout << “Enter words (to stop, type the word done):” << endl;
cin >> word;
for (i; strcmp(word.c_str(), “done”) != 0; i++)
{
cin >> word;
}
cout << "you entered a total of" << i << " word" << endl;
return 0;
}
本文介绍如何使用C_str()函数将String类型转换为const char*类型,并通过strcmp进行字符串比较。示例代码展示了在C++中读取用户输入的多个单词,直到输入特定停止词为止的过程。
2871

被折叠的 条评论
为什么被折叠?



