首页 > 编程知识 正文

bool类型输出格式,无法将void隐式转换为bool

时间:2023-05-04 12:33:20 阅读:137564 作者:4528

bool类型bool a; //a的可取值仅为0(false )、1 )1(true ) bool,可以是整数变量bool的形式,也可以是cout ) endl; bool a=true; bool a=false; 例如,bool a=true; a=35; coutaendl; //a=1 a=0; coutaendl; //a=0第一种: boola(inta,int b ) { return ab; (第二个) inta(inta,int b ) { return ab; }很明显,第一个比较好。 bool类型变量的值只有0,1,因此与int类型变量相比,可以更直观、更准确地排列函数,从而提高可读性。 字符串类型(包含在#includestring头文件中) ) )。

要用//c语言表示字符串,请使用string在char*c中表示字符串类型。 例如,string str='nihao '; 数组名称是常数,位于常数区域,表示整个数组。 在c中用strcat连接字符串,在c中用“”连接字符串。 String类型有很多函数。 例如,字符串str; str.length (; 和str.size (; 两者都用于返回字符串的长度,数值相同。

有关其他字符串函数,请参阅以下参考文档: c API

关于字符串类型的使用示例,代码如下所示。

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 '; 8str.swap(str2; 9 cout str 't ' str2 endl; 10coutstr.compare(str2) endl; 11coutstr.compare(Hello ) ) endl; 12 size _ TD=str.find _ first _ of (AEO ); 13 cout d endl; 14 size _ td2=str.find _ first _ of (zzcf ); 15if(D2==string:3360npos ) { 16 cout 'no find!' endl; 17 } 18 else { 19 cout的位置为' d2 endl; 20 ) 21str2='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}; 26size_tnum=str2.copy(cbuff,5,2 ); 27 cout num cbuff endl; 28 )将字符串类型转换为const char*的两种方法:

1 str='interst '; 2 const char* c=str.data (; 或const char* c=str.c_str (; 字符串构造函数:

1 string (; 创建空字符串,如string str; 2string(constchar*s ); //使用字符串s初始化3字符串(ConstStringStr ); //使用一个string对象初始化另一个对象的4string(intn,char c ); 使用//n字符c初始化引用类型引用类型,为了简化指针的格式引入了引用。 虽然很容易使用,但本质是面向指针操作的。

int a; //类型名称中的与取地址无关,表示这是引用类型。 如int*,表示这是int类型的指针类型。 //对引用变量的所有操作都将传输到指定的变量。 不需要添加引用类型使用示例。 代码如下所示。

1voidswap0(int*pa,int* pb ) { 2 int tem=*pa; 3 *pa=*pb; 4 *pb=tem; 5}67voidswap1(inta,int b ) )8inttem=a; 9 a=b; 10 b=tem; 11 ) 1213voidswap2(inta,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; 双精度d=52; double rd=d; //给变量d取别名,可以理解为和使用rd一样参照模型的三要素:

1、定义必须初始化

2、引用不可改变方向

3、引用不能为空

用于表示连接字符串并确定空。

1 void qm () { 2 string a='happy '; 3字符串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; 在10if(b=='new ' ) string中,“==”也被赋予了新的含义,11 cout 'equal' endl,循环比较各个字符是否相等; 12 ) 13else{14cout'noequal'endl; 15 ) 16c=' '; 17if(c.empty () ) { //c.empty ) )的返回值仅为0,1 18 cout ' c为空' endl; 19 } 20 else { 21 cout 'c非空' endl; 22 ) 23stringd='nihao '; 24d.push_back('m ); //字符串d末尾为push_back ) )中的字符' m' 25 cout d endl; 26d.insert(d.Begin ) ) 5,' n '; //迭代器begin (,索引“5”,插入字符' n ) (也可以是字符串) 27 cout d endl; 28 }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。