accelerated c++

accelerated c++ 学习笔记
 
第0章
main函数的返回值类型
标准c++  为int main()返回值0表示成功。
在纯C的编译环境中一般会使用void main,也即标准的C要求main函数是没有返回值的,而在C++中一般使用int main,C++要求main函数要有返回值,这也是标准C和C++main函数区别之一,即标准C++中要求使用int作为函数返回值;
如果你在VC++环境下编译,以上的分别是看不出来的,因为其支持以上的两种方式;但是如果找一个标准的C环境的话,就不一定通的过编译了!一般不必分的很清楚,只是到了不同的环境时才会显现的出来!
 
第一章
int main()
{
	string str;
	cin >> str;
	cout << str << endl;
}
cin 将读到的字符存储在变量str中,它首先会略去输入开始时碰到的空白字符(空白,制表键,回退键或换行符),直到遇到另一个空白字符或文件结束标记为止。
输入 abc dde dddee ddd
输出 abc
习题4
int main()
{


	{
		const std::string s = "a string";
		std::cout << s << std::endl;
	}
	{
		const std::string s = "another string";
		std::cout << s << std::endl;
	}
	
}
此程序有效 
int main()
{


		const std::string s = "a string";
		std::cout << s << std::endl;
	
		const std::string s = "another string";
		std::cout << s << std::endl;
	
}
编译出现错误
此程序无效 string s重定义
习题5
int main()
{


	{
		const std::string s = "a string";
		{
			std::string x = s + ", really";
			std::cout << s << std::endl;
		}
		std::cout << x << std::endl;


}
x的作用范围失效,相当于
std::cout << x << std::endl中
x变量没有定义

 
 
第三章
本章对vector有一个简单的介绍
double x;
vector<double> homework;
while(cin >> x)
homework.push_back(x);
	streamsize prec = cout.precision();
	cout << "your final grade is " << setprecision(3) 
		<< 0.2*midterm + 0.4*final + 0.4*median
		<< setprecision(prec) <<endl;
	return 0;
setprecision 头文件<iomanip> 这个控制器允许我们指明输出所包含的有效位数。
streamsize包含在头文件<ios>中 ,输入输出库就是用这个类型来表示长度。
排序函数sort在<algorithm>中
int main()
{
	int a[5] = {4, 5, 2, 3, 1};
	sort(a, a+5);
	for(int i = 0; i < 5; i++)
		cout << a[i] << endl;
	return 0;
}结果1 2 3 4 5
 
第四章
double median(vector<double> vec)
{
	typedef vector<double>::size_type vec_size;
	vec_size size = vec.size();
	if(size == 0)
		throw domain_error("median of an empty vector");
	sort(vec.begin(), vec.end());
	vec_size mid = size/2;
	return size%2 == 0 ? (vec[mid] + vec[mid - 1])/2 : vec[mid];
}
throw domain_error("median of an empty vector");
domain_error抛出异常 ,头文件<stdexcept>
setw <iomanip>设置宽度 注意:cout << setw(2) << i ;//i = 12345;输出为12345
cout << setw(5) << i;i = 123;输出为__123;(前面是两个空格);
cout.width();与setw()效果一样。
inline函数
 
第五章
<list> 模板库
string中的 s.substr(i,n)创建一个新的字符串来保存s的在区间[i, i+n)中的索引所指示的字符的一个副本。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值