C++基础篇 Day2

bool:

#include<iostream>
using namespace std;
#include<windows.h>
//1.BOOL本质是int ; typedef int BOOL bool是关键字
//2.所占字节数不同BOOL占四个 bool占一个

int main()
{
	BOOL B = TRUE;
	cout << sizeof(B) <<" "<< sizeof(BOOL) << endl;
	bool b = true;
	b = false;
	cout << sizeof(b) <<" "<< sizeof(bool) << endl;

	return 0;
}

注:使用BOOL时需要调用头文件#include<windows.h> 

string:

#include<iostream>
#include<string>
using namespace std;
void fun(const char*p)
{
	if (p)
	{
		cout <<p << endl;
	}
}
int main06()
{
	char* p1 = (char*)"123";
	//p1[1] = 'a';//运行时 非法
	cout << p1 << endl;
	p1 =(char*) "456";
	cout << p1 << endl;

	char arr[5] = "1234";
	arr[1] = 'a';
	cout << arr << endl;
	//arr = "4567";//编译时 非法
	//if (arr == p1){}//这里比较的是地址而不是字符串本身
	string str = "1234";
	str[1] = 'a';
	cout << str << endl;
	str = "6789";
	cout << str << endl;
	string str2 = "6799";
	if (str == str2)
	{
		cout << "str==str2" << endl;
	}
	else
	{
		cout << "str!=str2" << endl;
	}
	//-----------------------------------------------
	string str3 = str + "abcd" + p1;   //拼接
	cout << str3 << endl;
	str3=str3.substr(2, 5);//截取
	cout << str3 << endl;
	str3=str3.substr(2, 50);
	cout << str3 << endl;
	//str3.substr(20, 5);
	cout << str3.size() << " " << str3.length() << endl;
	fun(str3.c_str());   //将string类型转换成const char*
	return 0;
}

输出结果:

123
456
1a34
1a34
6789
str!=str2
6789abcd456
89abc
abc
3 3
abc

for

#include<iostream>
using namespace std;
int main()
{
	int arr[5] = { 1,2,3,4,5 };
	//增强的范围for循环
	for (int v : arr)
	{
		cout << v << " " ;
	}
	cout << endl;
	char* p = new char[5] {"1234"};
	//for (char c : p){} //无法遍历
	string str = "1234";
	for (char a: str)
	{
		cout << a <<" ";
	}
	cout << endl;
	return 0;
}

函数参数默认值

#include<iostream>
using namespace std;
void fun1(int a = 1)
{
	cout << __FUNCTION__ << " " << a << endl;  //fun1 10  fun1 1
}
void fun2(int a = 1, int b = 2)  //在传参时 从左向右传
{
	cout << __FUNCTION__ << " " << a <<" "<<b<< endl; //fun2 1 2  fun2 2 2
}
void fun3(int a,int b=1)  //如果多个参数指定默认值,从右向左一次指定,中间不得有间断
{
	cout << __FUNCTION__ << " " << a <<" " <<b<< endl;//fun3 2 1
}
//void fun4(int a, int b = 2, int c) {};  //中间不得有间断
int main()
{
	fun1(10);
	fun1();  //不传实参 形参取默认值
	//---------------------------------
	fun2();
	fun2(2);
	//---------------------------------
	fun3(2);
	return 0;
}

声明和定义同时存在时,在声明处指定函数参数默认值,不能同时在声明和定义处指定,也不能单独在定义处指定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值