C++程序设计(II)兼谈对象模型(侯捷)—— 13. 关于C++标准库 & 14.C++11的三个主题:数量不定的模板参数(参数包),auto和for range循环

关于C++标准库

标准库里提供的,我们可以直接使用的就是数据结构(容器)和算法。

推荐书籍:Algorithms + Data Structures = Programs

仿函数Functors → 算法Algorithms → 迭代器Iterators → 容器Containers

都自己用一遍这些容器、算法

如何确定自己的编译器是否支持C++11?

cout << __cplusplus << endl;

C++11输出201103,C++98和C++03的C++输出199711,

 __cplusplus 是一个宏

数量不定的模板参数 variadic template (since C++11)

typename...  (写的比较好的博客)

#include <iostream>
#include <bitset> 

using namespace std;

void print() {
	cout << __cplusplus << endl;
}

template<typename T, typename... Types>
void print(const T& firstArg, const Types&... args) {
	cout << "firstArg = " << firstArg << "   sizeof...(args) = " << sizeof...(args)<< endl;
	print(args...); // 有种递归的感觉,但是每次递归的参数类型是变化的
}

int main() {
	print(7.5, "hello", bitset<16>(377), 42); //bitset中应该重载<<
	return 0;
}

输出:

firstArg = 7.5 sizeof...(args) = 3

firstArg = hello sizeof...(args) = 2

firstArg = 0000000101111001 sizeof...(args) = 1

firstArg = 42 sizeof...(args) = 0

201402

最后一行输出的 201402 是因为调用了print()

sizeof...可以得到pack中有多少个参数

STL中很多地方用typename...翻新了

auto (since C++11)

不赋值的时候不能用auto,编译器无法推断类型。

初学者最好少用auto,要自己清楚自己每个变量的类型。

for range 循环(since C++11)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值