The C++ Standard Library 学习笔记(一)1 - 4 章

第二章 C++和标准库简介

2.2 C++语言新特性

2.1.1 模板

几乎所有STL库都是模板。

编译器不会为每种类型都生成代码,而只是为所要用的类型生成,因此你必须让你的模板函数在被调用的时候其实现是可见的,编译器才能编译该函数为特定的类型。因此,要使得模板可移植,你需要将函数的实现包含在头文件中,并且是内联(inline)函数。

无类型模板参数

即模板参数为值,而不是类型,编译时会被替换为常量。

例如:

bitset<32> flags32;

bitset<64> flags64;

注意:这里flags32flags64是不同的类型。

Typename 关键字

关键字typename 指明它后面的标识符是一种类型。

例如:

template <class T>

class MyClass {

    typename T::SubType *ptr;

};

这里指定SubType为一种类型,而不是T的静态变量。

成员模板

成员模板经常用于自动类型转换。

例如:

template <class T>

class MyClass {

public:

    MyClass() {

        T _value = T();

    }

 

    template <class U>

    MyClass(const MyClass<U>& u_) {

        assign(u_);

    }

 

    template <class U>

    void assign(const MyClass<U>& u_) {

        _value = u_.getValue();

    }

 

    T getValue() const {

        return _value;

    }

 

private:

    T _value;

};

void foo() {

    MyClass<double> dm;

    MyClass<double> dm1(dm);

}

2.6.6 关键字explicit

关键字explicit可以防止将一个值隐式转换为一个对象,如果这个对象的类的构造函数是单个参数。一个常见的例子:一个集合的类用一个大小作为初始化参数,

class Stack {

    explicit Stack(int size_); //创建一个大小为size_的栈

}

Stack s;

s = 4; //如果没有explicit 那么将创建一个大小为4的新栈并赋给s

第四章 工具

4.1 pairs

pairs的比较,first的优先级最高,比较时,先考虑first的大小,如果first相等,再比较second.

4.2 auto_ptr

不推荐使用,建议用boost smart pointer 代替。

4.3 Numeric Limits

C++标准库提供了numeric_limits,来替代和补充C宏。

numeric_limits 提供了类型安全,并且让程序员能写模板评估(evaluate)这些限制。

4.4 辅助函数(Auxiliary Functions

std::max的参数必须是同一种类型,如果不同类型,要显示指定类型做隐式转换。  

int i = 5;

long l = 10L;

//long max = std::max(i, l); Error

long max = std::max<long>(i,l);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life. Extending the overall content by about 50%, the book now also covers all the new C++11 library components, including Concurrency Fractional arithmetic Clocks and Timers Random numbers and distributions New smart pointers Regular expressions New STL containers, such as arrays, forward lists, and unordered containers New STL algorithms Tuples Type traits and type utilities

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值