首页 > 编程语言 >c++ string find 查找失败时 应该注意的地方

c++ string find 查找失败时 应该注意的地方

时间:2023-02-21 17:34:33浏览次数:29  
标签:main cout int namespace c++ include find string

当字符串查找失败的时候

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <limits.h>
using namespace std;

int main() {

    string aa = "sssss";
    cout<<( aa.find('a')==-1 )<<endl;
    cout<<( aa.find('a')< 0 )<<endl;
    cout<<( (int)aa.find('a')< 0 )<<endl;
    return 0;
}

c++ string find 查找失败时 应该注意的地方_#include

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <limits.h>
using namespace std;

int main() {
    cout<<"0xFFFFFFFF\t"<<0xFFFFFFFF<<endl;
    cout<<"(int)0xFFFFFFFF\t"<<(int)0xFFFFFFFF<<endl;
    cout<<"0x7FFFFFFF\t"<<0x7FFFFFFF<<endl;
    cout<<"(int)0xFFFFFFFF\t"<<(int)0xFFFFFFFF<<endl;
    cout<<"(int)0x7FFFFFFF\t"<<(int)0x7FFFFFFF<<endl;
    cout<<"INT_MAX\t\t"<<INT_MAX<<endl;
    cout<<"INT_MIN\t\t"<<INT_MIN<<endl;
    cout<<"0xFFFFFFFF+1\t\t"<<0xFFFFFFFF+1<<endl;
    cout<<"0x10000000\t"<<0x10000000<<endl;
    cout<<"(int)0x80000000\t"<<(int)0x80000000<<endl;

    cout<<"(int)0x80000000\t"<<(int)0x80000000<<endl;
    cout<<"(int)0x80000000\t"<<(int)0x80000000<<endl;

    cout<<"string::npos\t"<<string::npos<<endl;
    cout<<"0xFFFFFFFF==-1 "<<(0xFFFFFFFF==-1)<<endl;
    cout<<"0xFFFFFFFF<0 "<<(0xFFFFFFFF<0)<<endl;
    cout<<"(int)0xFFFFFFFF<0 "<<((int)0xFFFFFFFF<0)<<endl;
    cout<<"INT_MAX==-1 "<<(INT_MAX==-1)<<endl;

    return 0;
}

c++ string find 查找失败时 应该注意的地方_返回值_02
32位二进制位全1的时候表示的-1,但是未转成int之前 只可以与-1做相等判断,不可以与0做大小比较!!

标签:main,cout,int,namespace,c++,include,find,string
From: https://blog.51cto.com/liyunhao/6076890

相关文章