美文网首页
bool型,string(字符串)

bool型,string(字符串)

作者: ie大博 | 来源:发表于2016-11-10 10:32 被阅读0次

bool形

int main()
{
    bool bsex=true;
    cout<<"请输入性别";
    cin>>bsex;
    cout<<"我叫alex,是一位"<<(bsex?"男士":"女士")<<endl;
    cout <<"true="<<true<<endl;
    cout<<"false="<<false<<endl;

    return 0;
}

string:字符串

int main()
{
    string s;
    s="hello shanghai!";
    s +="haha";

    cout<<s<<endl;
    s = "hello";
    cout<<"size ="<<s.size()<<endl;
    cout<<"s是否为空:"<<(s.empty()?"空":"非空")<<endl;
    cout <<"s="<<s<<endl;

    const char * buf =s.c_str();//s.c_str()是成员函数,帮助其转换成c语言里面的字符串,
c_str是系统给的,相当于类里面的函数。
    cout <<"buf="<<buf<<endl;

    if(s=="hello")
    {
        cout<<"s == hello"<<endl;
    }
    cout<<"s[3]="<<s[3]<<endl;
    cout<<"s[3]="<<s.at(3)<<endl;//安全的模式打印第三个元素,如果越界会报错
    return 0;
    
}

相关文章

网友评论

      本文标题:bool型,string(字符串)

      本文链接:https://www.haomeiwen.com/subject/vjzzuttx.html