C++11常用特性

作者: wolfaherd | 来源:发表于2022-12-31 10:05 被阅读0次

    需要记忆的(面试七剑下天山)

    lambda
    语言级别线程支持
    右值引用
    智能指针
    常量表达式
    无序容器
    类型推导

    核心语言特性

    • 类型推导(auto/decltype)

    • 类型别名(using/typedef)

    • 长整型(long long)

    • 可变模板(...)

    template<class... Types>
    struct Tuple {};
     
    Tuple<> t0;           // Types contains no arguments
    Tuple<int> t1;        // Types contains one argument: int
    Tuple<int, float> t2; // Types contains two arguments: int and float
    Tuple<0> t3;          // error: 0 is not a type
    
    • 常量表达式(constexpr)
    constexpr int factorial(int n)
    {
        return n <= 1 ? 1 : (n * factorial(n - 1));
    }
    

    声明可以在编译时计算函数或变量的值

    • 空指针值(nullptr)

    • 右值引用(&&)

    • 统一码字面值

    u8"s-char-sequence(optional)"//UTF-8
    u"s-char-sequence(optional)" //UTF-16
    U"s-char-sequence(optional)"//UTF-32
    prefix(optional) R"d-char-sequence(optional)
    (r-char-sequence(optional))d-char-sequence(optional)"
    
    const char* s1 = R"foo(
    Hello
      World
    )foo";
    // same as
    const char* s2 = "\nHello\n  World\n";  
    
    • lambda表达式

    • 范围for

    std::vector<int> v = {0, 1, 2, 3, 4, 5};
    for (const int& i : v)
      std::cout << i << ' ';
    
    • static_assert

    编译期断言检查。

    标准库特性

    • array

    静态连续数据

    • forward_list

    forward_list是一个容器,支持从容器中的任何地方快速插入和删除元素。不支持快速随机访问。它被实现为一个单链表。与std::list相比,当不需要双向迭代时,该容器提供了更有效的空间存储。

    • tuple

    异构值集合

    • unordered_map和unordered_set

    无序容器。搜索、插入和删除元素的平均时间复杂度为常数。在内部,元素不是按任何特定的顺序排序,而是组织成桶。一个元素被放入哪个存储桶完全取决于它的键的哈希值。具有相同哈希码的键出现在同一个bucket中。这允许快速访问各个元素,因为一旦计算哈希,它就会引用元素所在的桶。unordered_map相对于unordered_set多了个value值。

    • atomic

    如果一个线程写入一个atomic对象,而另一个线程从中读取,则该行为定义良好

    • condition_variable

    条件变量,与std::mutex一起使用,以促进线程间通信。

    • future

    类模板std::future提供了一种访问异步操作结果的机制

    • mutex

    线程支持库。提供基本的互斥功能,常结合lock_guard使用。

    • thread

    语言级线程支持

    相关文章

      网友评论

        本文标题:C++11常用特性

        本文链接:https://www.haomeiwen.com/subject/phkixdtx.html