栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

侯捷c++11&14新标准

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

侯捷c++11&14新标准

侯捷c++11&14新标准

文章目录
    • 侯捷c++11&14新标准
      • 引言
      • variadic templates
      • Spaces in Template Expressions
        • nullptr
        • auto
        • 初始化(initializer_list<>)
        • explicit(for ctors taking more than one argument)
        • Range-based for statement
        • 全局函数 begin() end()
        • =default =delete
        • Alias Template 化名
        • template template parameter
        • Type Alias, noexcept, override, final
          • Type Alias
          • noexcept
          • override
          • final
        • decltype
        • Lambdas
        • Variadic Templates
      • 标准库
        • Rvalue reference
        • array forward_list
        • Hashtable
        • Hash function
        • tuple

引言

新特性包括语言和标准库两个部分

标准库是以头文件的形式呈现(不加.h)

cplusplus.com

variadic templates Spaces in Template Expressions nullptr

nullptr代替 0或者NULL

auto

不确定变量类型 让编译器自己推

比如:代替迭代器、lambda表达式

初始化(initializer_list<>)

变量的后面直接放大括号

int values[]{1, 2, 3}

initializer_list val {?, ?, ?, …} 用一个array来接收 其实是保存头指针

cout << max({ 1, 2, 3 }) << " " << min({string(“a”), string(“b”), string(“c”) }) << endl;

explicit(for ctors taking more than one argument)

2.0之前让编译器不要自作主张

#include 
using namespace std;
 
class A
{
public:
    //这里用explicit关键词来修饰类构造函数.
    explicit A(int i = 5, int j = 10)
    {
        m_a = i;
        m_b = j;
    }
private:
    int m_a;
    int m_b;
};
 
int main()
{
    A s;
    //这样直接赋值,会被提示错误,因为explicit抑制隐式转换的进行
    s = 10;//这样会报错!!!
    //当然显示转换还是可以的.
    s = A(20);
 
    system("pause");
    return 0;
}
Range-based for statement
for(dec1 : col){
	statement
}
全局函数 begin() end()

e.g. begin(vector…) 相当于 vector.begin()

=default =delete

=default 表示子类直接继承父类

=delete 表示子类不写这个函数

有指针的类就有big three(即默认构造,拷贝构造,析构)

Alias Template 化名
template
using Vec = vector>;
Vec coll;
等价于
vector> coll;
template template parameter

template

template typename Container>
class XCls{
    private:
    	container c;
};
template
class test{
    private:
    	T t;
};
template
using mlst = list>;// Alias Template 化名

int main(){
    XCls myContain;
    // XCls mylist; // 错误 因为list中有两个参数
    XCls mylist;
}
Type Alias, noexcept, override, final Type Alias
using func = void (*)(int, int);
//等价于
typedef void (*func)(int, int);
noexcept
void foo() noexcept(true){}// 不抛出异常
// move过程中不抛出异常,让程序放心。有move的时候程序有限调用move 而不是copy construct
MyString(MyString&& str) noexcept{}
MyString& operator=(MyString&& str) noexcept{}
override

帮助编译器检查是否为重写

final
class Base1 final{};// 没有人能继承Base1
virtural void f() final{};// 没有人能重写f()
decltype

获得变量类型

map coll;
decltype(coll)::value_type elem;
// 相当于map::value_type elem;

Template
auto add(T1 a, T2, b) -> decltype(a + b)
Lambdas
[&id]()mutable{++id;}// 才能改变id值

int tobefound = 5;
auto lambda1 = [tobefound](int val) {return val == tobefound; };
cout << lambda1(5) << " " << lambda1(10) << endl;

属于inline内联函数

Variadic Templates

递归

void func(){}
template
void func(const T& firstArg, const Types& ...args){
    func(args...);
}

template 
class tuple{
public:
    size_t seed = 0;
    tuple(const Value&... args){tuple(seed, args...)}
};

// 特化
template<>
class tuple<>{};

template
class tuple:private tuple{
    typedef tuple inherited;
protect:
    Head m_head;
public:
    tuple(){};
    tuple(Head v, Tail... vtail):m_head(v), inherited(vtail){}
    // typename Head::type head(){return m_head};
    auto head()->decltype(m_head){return m_head};
    inherited& tail(){return* this;}
}

标准库 Rvalue reference

copy 和 move

insert(cons_iterator _position, const value_type& _x);
insert(cons_iterator _position, const value_type&& _x);
关键字 move forward才能完美转移

array forward_list

array数组 int a[10]; int* a = new int [10]

forward_list就是list改成单项列表

Hashtable Hash function tuple
tuple t1(41, 6.3, "nice");
get<0>(t1);  get<1>(t1);  get<2>(t1);
auto t2 = make_tuple(41, 6.3, "nice");
get<1>(t1) = get<>(t2);
t1 = t2;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/846758.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号