贵阳网站开发人员工资,网站右侧浮动微信二维码,网站建设宽带,山西官方网站有哪些在写程序的过程中#xff0c;经常会遇到要比较两个字符串是否相等的情况。如果要比较的对象是char字符串#xff0c;则利用 int strcmp(const char s1,const char* s2) 当s1 s2时#xff0c;返回为负数#xff1b; 当s1 s2时#xff0c;返回值 0#xff1b; 当s1 经常会遇到要比较两个字符串是否相等的情况。如果要比较的对象是char字符串则利用 int strcmp(const char s1,const char* s2) 当s1 s2时返回为负数 当s1 s2时返回值 0 当s1 s2时返回正数。 即两个字符串自左向右逐个字符相比按ASCII值大小相比较直到出现不同的字符或遇’\0’为止。如 “A”“B” “a”“A” “computer”“compare” 特别注意strcmp(const char *s1,const char * s2)这里面只能比较字符串即可用于比较两个字符串常量或比较数组和字符串常量不能比较数字等其他形式的参数。 ANSI标准规定返回值为正数负数0 。而确切数值是依赖不同的C实现的。 如果要比较的对象是两个string则利用函数compare()。若要比较string s1和s2则写为s1.compare(s2)若返回值为0则两者相等。 注意在比较两个字符串时不能利用符号 “ ”“ ”符号比较的是两个字符串的地址是否相等。 若是比较两个字符则可以用直接进行比较~ 但在比较两个string对象时是可以利用“”的相等的话则表达式的返回值为1不等为0。
#include iostream
#include ctime
#include string
#include stdlib.h
#include string.h
using std::string;
using namespace std;
int main(int argc, char *argv[]){int a strcmp(Assa,assa);coutaendl;return 0;
}写完用g编译出现error: ‘strcmp’ was not declared in this scope
上网查找发现必须再加上#include string.h才能正确编译执行即同时存在
#include string.h #include using std::string; 也就是说strcmp不在C标准库中需要单独包含strcmp所在的头文件