c++学习笔记

1.strlen和sizeof的区别
char str[20]="0123456789";
int a=strlen(str); //a=10;
int b=sizeof(str); //而b=20;
2.在VS中新建C++源文件
第一步,打开主界面,文件--》新建-》工程  选择VC++下面的,win32,win32控制台应用程序,输入工程名,选择空项目
第二步,在左边资源管理器的源文件点击右键,添加,添加CPP文件即可
运行为了不让黑色的框一闪而过,可以使用ctr+f5运行
3 。char charr[20];
 cout << "Length of string in charr before input: " 
         << strlen(charr) << endl;
输出结果是随机的,因为没有对数组赋初值
4.输出文本
#include <iostream>
int main()
{
    using namespace std;
    char ch;
    int count = 0;      // use basic input
    cout << "Enter characters; enter # to quit:\n";
    cin >> ch;          // get a character
	while(ch!='#')
	{
		cout<<ch;
		++count;
		cin>>ch;
	}
	cout << endl << count << "characters read \n";
	return 0;
}

运行结果:
cin将忽略空格和换行符
cin.get()可以识别空格和换行符
cin.get(ch);          // get a character
5.cout的一些用法
cout << fixed;
fixed 使用小数记数法
cout.precision(2);
precision(2) 设置2为新的浮点数精度值, 并返回原值
setioflags(ios::fixed) 固定的浮点显示 
setioflags(ios::scientific) 指数表示 
setiosflags(ios::left) 左对齐 
setiosflags(ios::right) 右对齐 
setiosflags(ios::skipws 忽略前导空白 
setiosflags(ios::uppercase) 16进制数大写输出 
setiosflags(ios::lowercase) 16进制小写输出 
setiosflags(ios::showpoint) 强制显示小数点 
setiosflags(ios::showpos) 强制显示符号 
例子:
cout<<setiosflags(ios::fixed)<<setprecision(3)<<1.2345<<endl;输出"1.235" 
cout<<setiosflags(ios::scientific)<<12345.0<<endl;//输出"1.234500e+004 " 
cout<<setprecision(3)<<12345.0<<endl;//输出"1.235e+004 " 


#include <iostream>
#include <fstream>                  // for file I/O

int main()
{
    using namespace std;
	double a_price;
    cout << "Enter the original asking price: ";
    cin >> a_price;
    cout << fixed;
	cout.precision(3);
	cout.setf(ios_base::showpoint);
	cout<<"price="<<a_price<<endl;
	return 0;

}

6.参数为数组的函数
void show_array(const double ar[ ],int n);
该声明表明,指针ar指向的是数组只能读,不能写,不能试图改变。
7.const和指针
方式一:
#include <iostream>
int main()
{
    using namespace std;
	int a=16;
	int b=12;
	const int *p=&a;
	*p=20;
    cout << "a="<<a<<"b="<<b<<endl;
	return 0;

}


运行结果:error C3892: 'p' : you cannot assign to a variable that is const
换为p=&b;之后程序运行没有错误
禁止修改p指向的值,但是p可以指向其他变量
方式二:
        int a=16;
	int b=12;
	int * const p=&a;
	*p=20;

可以修改p指向的值,但是禁止p指向其他变量
方式三:
       int a=16;
	const int * const p=&a;
禁止修改p指向的值,禁止p指向其他变量
8.函数无法返回字符串,但是可以返回字符串的地址
9.对于带参数列表的函数,必须从右向左添加默认值。也就是说,要为某个参数设置默认值,则必须为它右边的所有参数提供默认值:
int harpo(int n ,int m=4,int j=5);
int chico(int n ,int m=6,int j);这样是错误的
实参按从左向右的顺序依次被赋给相应的形参,而不能跳过任何参数。 




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值