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; 

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM