bool类型、string类型、引用类型

bool类型

 bool a; //a的取值只有0(false)、1(true)
 bool是一种整型变量
 bool 形式也可以为cout<<(3<7)<<endl;

 bool a=true;
 bool a=false;
 例如,
    bool a=true;
    a=35;
    cout<<a<<endl; // a=1
    a=0;
     cout<<a<<endl; //a=0

第一种: bool A(int a,int b){
                            return a>b;
                   }

第二种: int A(int a,int b){
                            return a>b;
                   }
显然第一种会更好,因为bool类型变量的取值只有0、1,相较于int类型变量,更能直观准确地定位该函数的功能,可读性更强。

String类型

(包含在#include<string>头文件中)

//c语言中表示字符串用char*
c++中表示字符串类型用string
例如,string str = "nihao";
数组名是常量,放在常量区,表示整个数组。

c中用strcat来连接字符串,c++中用 " + " 来连接字符串。

String 类型有很多函数,例如:string str;

 str.length();和str.size();俩都是用来返回字符串长度,且数值相同。

其他string类型函数见如下参考文件:c++ API

关于String类型的使用举例,代码如下:

    1 	void q2() {
    2 	        string str;
    3 	        str = "nihao";
    4 	        cout << str << endl;
    5 	        cout << "字符串长度是" << str.length() << endl;
    6 	        cout << "字符串长度是" << str.size() << endl;
    7 	        string str2 = "hello";
    8 	        str.swap(str2);
    9 	        cout << str << "\t "<< str2 << endl;
   10 	        cout << str.compare(str2) << endl;
   11 	        cout << str.compare("hello") << endl;
   12 	        size_t d = str.find_first_of("aeo");
   13 	        cout << d << endl;
   14 	        size_t d2 = str.find_first_of("zzcf");
   15 	        if (d2 == string::npos) {
   16 	                cout << "no find!" << endl;
   17 	        }
   18 	        else {
   19 	                cout << "位置是" << d2 << endl;
   20 	        }
   21 	        str2 = "teststring";
   22 	        const char* c_str = str2.data();
   23 	        cout << __LINE__ << c_str << endl;//用"__LINE__"来返回该行代码所在行数
   24 	        cout << __LINE__ << str2 << endl;
   25 	        char cbuff[50] = {123};
   26 	        size_t num = str2.copy(cbuff, 5, 2);
   27 	        cout << num << cbuff << endl;
   28 	}

string类型转换为const char*的两种方式:

    1 	    str = "interst";
    2 	    const char* c = str.data();
    3 	 或const char* c = str.c_str();

string类型构造函数:

    1 	 string();//创建一个空的字符串 例如:string str;
    2 	 string(const char* s);//使用字符串s初始化
    3 	 string(const string& str);//使用一个string对象初始化另一个对象
    4 	 string(int n,char c);//使用n个字符c初始化

引用类型

引用类型,为了简化指针的写法,引入了引用。用起来方便,但是本质对指针操作。

 int& a;//类型名& 中的&和取地址没有关系,表示这是一个引用类型 。类似于int* ,表示这是一个int类型的指针类型。

//所有对引用变量的操作,都会转接给指向的变量,不需要加*了

引用类型使用举例,代码如下: 

    1 	void swap0(int* pa,int* pb) {
    2 	        int tem = *pa;
    3 	        *pa = *pb;
    4 	        *pb = tem;
    5 	}
    6 	
    7 	void swap1(int& a,int& b) {
    8 	        int tem = a;
    9 	        a = b;
   10 	        b = tem;
   11 	}
   12 	
   13 	void swap2(int a,int b) {
   14 	        int tem = a;
   15 	        a = b;
   16 	        b = tem;
   17 	}

main()函数里的代码:

int a = 2, b = 5;
swap0(&a,&b);
cout << "a=" << a << "\t" << "b=" << b << endl;
swap1(a,b);
cout << "a=" << a << "\t" << "b=" << b << endl;
swap2(a,b);//不存在交换,只是将地址传过去了
cout << "a=" << a << "\t" << "b=" << b << endl;
 double d=52;
 double& rd=d;//可以理解为给变量d取别名,用rd和用d效果一样

引用类型三要素:

1、定义必须初始化

2、引用不可改变指向

3、引用不能为空

用来表示连接字符串,以及判空:

    1 	void qm() {
    2 	        string a = "happy ";
    3 	        string b = "new ";
    4 	        cout << a[2] << endl;
    5 	        cout << a + b << endl;//字符串连接用'+'
    6 	        string c = (a + b) + "year";
    7 	        cout << c << endl;
    8 	        c += " !";
    9 	        cout << c << endl;
   10 	        if (b == "new ") {//在string中“==”也被重新赋予含义,功能是循环的比较每个字符是否相等
   11 	                cout << "equal" << endl;
   12 	        }
   13 	        else {
   14 	                cout << "no equal" << endl;
   15 	        }
   16 	        c = "";
   17 	        if (c.empty()) {  //c.empty()返回值只有0、1
   18 	                cout << "c为空" << endl;
   19 	        }
   20 	        else {
   21 	                cout << "c非空" << endl;
   22 	        }
   23 	        string d = "nihao";
   24 	        d.push_back('m'); //字符串d尾部用push_back()写入一个字符'm'
   25 	        cout << d << endl;
   26 	        d.insert(d.begin()+5,'n');//迭代器begin(),索引“5”,插入字符'n'(也可字符串)
   27 	        cout << d << endl;
   28 	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦偏执狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值