[go: up one dir, main page]

Skip to content

zxj5470/learn-cpp

Repository files navigation

Learn C++ (C++11)

1. C++ 11

1.1. auto 关键字

  • 类型推导
auto i = 0;
  • 对于STL中迭代器的类型书写简化
map<int,int> m;
auto it = m.begin();

std::unordered_multimap<int, int> um;
// before
std::pair<
    std::unordered_multimap<int, int>::iterator,
    std::unordered_multimap<int, int>::iterator> range = um.equal_range(key);

// after
auto range2 = um.equal_range(key);

例: main_010_auto.cpp

1.2. decltype 关键字

  • 获知表达式类型
int x = 0;
decltype(x) y = 1;// y -> int

例: main_011_decltype.cpp

1.3. template default value 模板默认值

template <typename R = int, typename U>
R func(U val){
    return val;
}

int main(){
    auto ret1 = func(123);// return `int`
    printType(ret1);// i

    auto ret2 = func<long>(123);// return `long`
    printType(ret2);// l

}

例: main_012_template_default_value.cpp

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published