Run ID:7700

提交时间:2025-05-16 13:27:33

#include <cstring> // 包含cstring头文件以使用strcmp函数 #include <iostream> int main() { const char* str1 = "hello"; const char* str2 = "world"; int result = strcmp(str1, str2); if(result == 0) { std::cout << "The strings are equal." << std::endl; } else if(result < 0) { std::cout << "str1 is less than str2." << std::endl; } else { std::cout << "str1 is greater than str2." << std::endl; } return 0; } 复制 Cpp 以下是使用std::string类的compare方法的例子1: #include <string> #include <iostream> int main() { std::string str1 = "hello"; std::string str2 = "world"; int result = str1.compare(str2); if(result == 0) { std::cout << "The strings are equal." << std::endl; } else if(result < 0) { std::cout << "str1 is less than str2." << std::endl; } else { std::cout << "str1 is greater than str2." << std::endl; } return 0; }