C++-字符串(string) 布尔类型(bool)

字符串string 可以进行相加操作, s.size(), s.length(),s.c_str() 转换为c语言类型

/*
字符串演示
*/
#include <iostream>
#include <cstring>
using namespace std; 


int main(void) {
    string s = "hello"; 
    s += " world"; 
    cout << s << endl; 

    string s2; 
    s2 = s;
    cout << s << endl; 
    //进行比较 
    string s3 = "hello world"; 

    if (s == s3) {
        cout << "两个字符串内容相同" << endl; 
    }
    cout << s.size() << endl;
    cout << s.length() << endl; 
    cout << strlen(s.c_str()) << endl;  
}

布尔类型 bool,真表示1,假表示0

/*
bool类型
*/
#include <iostream>

using namespace std; 


int main(void) {
    bool b = false; 
    cout << boolalpha << b << endl; //使得打印出来的数据是bool类型 boolalpha 
    cout << sizeof(b) << endl; 

    b = 3 + 5; 
    cout << "b=" << b << endl; 

    b = 1.2 * 3.4;
    cout << "b=" << b << endl; 

    char* p = NULL; 
    b = p; 
    cout << "b=" << b << endl;  //地址是空的话表示负数 
    return 0; 

}

原文链接: https://www.cnblogs.com/hyq-lst/p/12597198.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/195038

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午6:52
下一篇 2023年2月12日 下午6:52

相关推荐