C++中compare函数的使用
生活随笔
收集整理的這篇文章主要介紹了
C++中compare函数的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自 https://blog.csdn.net/qq_35038153/article/details/78191574
compare函數用來進行字符串以及其子串的比較,示例如下:
#include <iostream> #include <string> #include <cctype> using std::cout; using std::endl; using std::cin; using std::string; int main(void){string str1="hi,test,hello";string str2="hi,test";//字符串比較if(str1.compare(str2)>0)printf("str1>str2\n");else if(str1.compare(str2)<0)printf("str1<str2\n");elseprintf("str1==str2\n");//str1的子串(從索引3開始,包含4個字符)與str2進行比較if(str1.compare(3,4,str2)==0)printf("str1的指定子串等于str2\n");elseprintf("str1的指定子串不等于str2\n");//str1指定子串與str2的指定子串進行比較if(str1.compare(3,4,str2,3,4)==0)printf("str1的指定子串等于str2的指定子串\n");elseprintf("str1的指定子串不等于str2的指定子串\n");//str1指定子串與字符串的前n個字符進行比較if(str1.compare(0,2,"hi,hello",2)==0)printf("str1的指定子串等于指定字符串的前2個字符組成的子串\n");elseprintf("str1的指定子串不等于指定字符串的前2個字符組成的子串\n");return 0;}總結
以上是生活随笔為你收集整理的C++中compare函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信编辑器是做什么的呢?
- 下一篇: matlab输入二项分布函数,MATLA