string判断相等
时间: 2023-07-21 20:15:01 浏览: 45
在C++中,可以使用`==`运算符来判断两个字符串是否相等。例如:
```c++
#include <iostream>
#include <string>
int main() {
std::string str1 = "hello";
std::string str2 = "world";
if (str1 == str2) {
std::cout << "str1 is equal to str2" << std::endl;
} else {
std::cout << "str1 is not equal to str2" << std::endl;
}
return 0;
}
```
运行结果为:
```
str1 is not equal to str2
```
相关问题
string 判断相等
在 C++ 中,可以使用`==`运算符来判断两个字符串是否相等。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = "Hello";
if (str1 == str2) {
std::cout << "The strings are equal." << std::endl;
} else {
std::cout << "The strings are not equal." << std::endl;
}
return 0;
}
```
在上面的例子中,我们声明了两个字符串`str1`和`str2`,并使用`==`运算符比较它们。如果两个字符串相等,就会输出"The strings are equal.",否则输出"The strings are not equal."。
c++ string 判断相等
在 C++ 中,可以使用 `==` 运算符来判断两个字符串是否相等。例如:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "hello";
string s2 = "world";
string s3 = "hello";
if (s1 == s2) {
cout << "s1 and s2 are equal" << endl;
} else {
cout << "s1 and s2 are not equal" << endl;
}
if (s1 == s3) {
cout << "s1 and s3 are equal" << endl;
} else {
cout << "s1 and s3 are not equal" << endl;
}
return 0;
}
```
输出结果为:
```
s1 and s2 are not equal
s1 and s3 are equal
```