c++11 最全新特性介绍!!!

自动类型推导(auto)

用途:允许编译器根据初始化表达式推断变量的类型。

示例:auto x = 5; // x 的类型为 int

初始化列表({})

用途:用于数组和容器的初始化,也可以用于类的构造函数。

示例:std::vector<int> v = {1, 2, 3};

引用别名(decltype 和 using)

用途:定义类型别名。

示例:

int i;

using IntRef = decltype(i); // IntRef 类型为 int&

统一初始化语法

用途:使用花括号 {} 进行对象的初始化。

示例:

struct S { int x, y; };

S s{1, 2}; // 统一初始化

右值引用和移动语义

用途:支持高效的临时对象处理和移动构造/赋值。

示例:

class MoveOnly {

public:

MoveOnly(MoveOnly&& other) noexcept : data_(other.data_) { other.data_ = nullptr; }

private:

int* data_;

};

Lambda 表达式

用途:在代码中定义简洁的一次性使用的匿名函数。

示例:

auto add = [](int x, int y) { return x + y; };

显式转换运算符(explicit)

用途:防止非显式的构造函数调用。

示例:

struct MyInt {

explicit MyInt(int i) : value(i) {}

int value;

};

委托构造函数

用途:一个构造函数可以调用另一个构造函数。

示例:

struct Point {

Point(int x, int y) : x_(x), y_(y) {}

Point(int x) : Point(x, x) {} // 委托构造函数

private:

int x_, y_;

};

非静态数据成员初始化

用途:在类定义时初始化成员变量。

示例:

struct Person {

int age = 20; // 直接初始化

};

nullptr 关键字

用途:替代 NULL 宏,提供更清晰的空指针表示。

示例:

int* p = nullptr;

线程支持(<thread>)

用途:提供了线程管理的基础功能。

示例:

#include <thread>

void hello() { std::cout << "Hello from thread" << std::endl; }

int main() {

std::thread t(hello);

t.join();

return 0;

}

原子操作(<atomic>)

用途:提供了原子操作的支持。

示例:

#include <atomic>

std::atomic<int> ai(0);

列表初始化(std::initializer_list)

用途:处理可变数量的参数列表。

示例:

void print(std::initializer_list<int> il) {

for (auto i : il) std::cout << i << " ";

std::cout << std::endl;

}

用户定义文字量(User-defined literals)

用途:允许用户扩展文字量。

示例:

namespace literals {

long operator"" _s(long x) { return x * 1000; }

}

int main() {

auto ms = 10_s;

return 0;

}

可变模板参数(template<typename... Ts>)

用途:支持可变数量的类型参数。

示例:

template<typename T, typename... Args>

void print(T first, Args... args) {

std::cout << first << " ";

print(args...);

}

局部类型别名(auto 和 using)

用途:局部作用域内定义类型别名。

示例:

auto sum = [](auto x, auto y) -> auto { return x + y; };

对齐属性(alignas)

用途:控制变量或类型的对齐方式。

示例:

alignas(16) int array[10];

noexcept 规格符

用途:指定函数抛出异常的可能性。

示例:

void safe_function() noexcept {}

标准文件系统库(<filesystem>)

注意:虽然 <filesystem> 在 C++17 中正式成为标准库的一部分,但在 C++11 中有一些实现作为实验性库存在。

用途:提供了文件系统操作的接口,如路径处理、文件创建、删除等。

示例:

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

fs::path p("example.txt");

if (fs::exists(p)) {

std::cout << "File exists." << std::endl;

}

内嵌命名空间(namespace)

用途:允许在现有命名空间内部定义新的命名空间。

示例:

namespace my_namespace {

namespace inner_ns {

void func() {}

}

}

constexpr 函数

用途:声明可以在编译期计算的函数。

示例:

constexpr int square(int x) {

return x * x;

}

static_assert(square(5) == 25, "square function failed");

局部类(Local Classes)

用途:允许在函数内部定义类。

示例:

void func() {

class LocalClass {

public:

void print() const { std::cout << "Hello from local class!" << std::endl; }

};

LocalClass lc;

lc.print();

}

枚举类(Enumerated Class)

用途:提供了更强类型的枚举,防止与其他整数或不同枚举混用。

示例:

enum class Color { Red, Green, Blue };

void printColor(Color c) {

switch (c) {

case Color::Red: std::cout << "Red"; break;

case Color::Green: std::cout << "Green"; break;

case Color::Blue: std::cout << "Blue"; break;

}

}

override 和 final 关键字

用途:override 指明派生类中的虚函数覆盖基类中的虚函数;final 防止进一步的继承。

示例:

class Base {

public:

virtual void func() {}

};

class Derived : public Base {

public:

void func() override {}

};

class FinalDerived : public Derived {

public:

void func() final {}

};

基于范围的 for 循环

用途:简化了迭代容器的过程。

示例:

std::vector<int> v = {1, 2, 3, 4, 5};

for (int x : v) {

std::cout << x << " ";

}

noexcept 规格符

用途:指定函数是否可能抛出异常。

示例:

void safe_function() noexcept {}

static_assert 断言

用途:确保编译时条件成立,否则编译失败。

示例:

template<typename T>

void ensure_is_integral() {

static_assert(std::is_integral<T>::value, "T must be an integral type.");

}

alignof 运算符

用途:获取类型的对齐要求。

示例:

int alignment = alignof(double);

外部模板(External Templates)

用途:允许模板的定义和实现分离到不同的文件中。

示例:

template<typename T>

T min(T a, T b);

extern template T min<int>(int, int);

可变模板参数(Variadic Templates)

用途:允许函数或类接受可变数量的模板参数。

示例:

template<typename T, typename... Args>

void print(T first, Args... args) {

std::cout << first << " ";

print(args...);

}

[[noreturn]] 属性

用途:标记不会返回的函数。

示例:

[[noreturn]] void exit_function() {

std::exit(EXIT_FAILURE);

}

__func__ 预处理器宏

用途:获取当前函数的名字。

示例:

void myFunction() {

std::cout << "Inside " << __func__ << std::endl;

}

_Pragma 运算符

用途:允许预处理器指令被包含在字符串中。

示示例:

#pragma once

#define MY_MACRO(x) _Pragma(#x)

MY_MACRO(message("This is a message"))

__cplusplus 宏

用途:获取当前使用的 C++ 标准版本的年份。

示例:

if (__cplusplus >= 201103L) {

std::cout << "Using C++11 or later." << std::endl;

}

alignas 和 alignof 关键字

用途:alignas 控制变量或类型的对齐方式;alignof 获取类型的对齐要求。

示例:

alignas(16) int array[10];

int alignment = alignof(double);

用户定义的文字量(User-defined Literals)

用途:允许用户扩展文字量。

示例:

namespace literals {

long operator"" _s(long x) { return x * 1000; }

}

int main() {

auto ms = 10_s;

return 0;

}

static_assert 断言

用途:确保编译时条件成立,否则编译失败。

示例:

template<typename T>

void ensure_is_integral() {

static_assert(std::is_integral<T>::value, "T must be an integral type.");

}

[[noreturn]] 属性

用途:标记不会返回的函数。

示例:

[[noreturn]] void exit_function() {

std::exit(EXIT_FAILURE);

}

noexcept 规格符

用途:指定函数是否可能抛出异常。

示例:

void safe_function() noexcept {}

thread_local 关键字

用途:指定变量在每个线程中具有单独的副本。

示例:

thread_local int thread_count = 0;

void increment() {

++thread_count;

}

__has_include 和 __has_cpp_attribute 预处理器宏

用途:检查头文件是否存在以及特定属性是否可用。

示例:

#if __has_include(<iostream>)

#include <iostream>

#endif

#if __has_cpp_attribute(noreturn)

[[noreturn]] void exit_function() {

std::exit(EXIT_FAILURE);

}

#endif

[[deprecated]] 属性

用途:标记即将废弃的函数或类型。

示例:

[[deprecated("Use new_function instead")]] void old_function() {}

[[fallthrough]] 属性

用途:明确指示 fall-through 在 switch 语句中是预期的行为。

示例:

void printColor(int color) {

switch (color) {

case 0:

std::cout << "Red";

[[fallthrough]];

case 1:

std::cout << "Green";

[[fallthrough]];

default:

std::cout << "Blue";

}

}

[[maybe_unused]] 属性

用途:抑制未使用的警告。

示例:

void func([[maybe_unused]] int x) {}

#c++##C++11新特性##c++后端##c++面经##c++新特性#
c++知识库 文章被收录于专栏

不定时更新一些学习c++的知识,整理不易,多多关注谢谢

全部评论

相关推荐

自从我室友在计算机导论课上听说了“刷&nbsp;LeetCode&nbsp;是进入大厂的敲门砖”,整个人就跟走火入魔了一样。他在宿舍门口贴了一张A4纸,上面写着:“正在&nbsp;DP,请勿打扰,否则&nbsp;Time&nbsp;Limit&nbsp;Exceeded。”日记本的扉页被他用黑色水笔加粗描了三遍:“Talk&nbsp;is&nbsp;cheap.&nbsp;Show&nbsp;me&nbsp;the&nbsp;code。”连宿舍聚餐,他都要给我们讲解:“今天的座位安排可以用回溯算法解决,但为了避免栈溢出,我建议用动态规划。来,这是状态转移方程:dp[i][j]&nbsp;代表第&nbsp;i&nbsp;个人坐在第&nbsp;j&nbsp;个位置的最优解。”我让他去楼下取个快递,他不直接去,非要在门口踱步,嘴里念念有词:“这是一个图的遍历问题。从宿舍楼(root)到驿站(target&nbsp;node),我应该用&nbsp;BFS&nbsp;还是&nbsp;DFS?嗯,求最短路径,还是广度优先好。”和同学约好出去开黑,他会提前发消息:“集合点&nbsp;(x,&nbsp;y),我们俩的路径有&nbsp;k&nbsp;个交点,为了最小化时间复杂度,应该在&nbsp;(x/2,&nbsp;y/2)&nbsp;处汇合。”有一次另一个室友低血糖犯了,让他帮忙找颗糖,他居然冷静地分析道:“别急,这是一个查找问题。零食箱是无序数组,暴力查找是&nbsp;O(n)。如果按甜度排序,我就可以用二分查找,时间复杂度降到&nbsp;O(log&nbsp;n)。”他做卫生也要讲究算法效率:“拖地是典型的岛屿问题,要先把连通的污渍区块都清理掉。倒垃圾可以用双指针法,一个指针从左往右,一个从右往左,能最快匹配垃圾分类。”现在我们宿舍的画风已经完全变了,大家不聊游戏和妹子,对话都是这样的:“你&nbsp;Two&nbsp;Sum&nbsp;刷了几遍了?”“别提了,昨天遇到一道&nbsp;Hard&nbsp;题,我连暴力解都想不出来,最后只能看题解。你呢?”“我动态规划还不行,总是找不到最优子结构。今天那道接雨水给我整麻了。”……LeetCode&nbsp;真的害了我室友!!!
老六f:编程嘉豪来了
AI时代还有必要刷lee...
点赞 评论 收藏
分享
评论
1
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务