c++11新特性随笔

news2025/7/13 14:53:54

1.统一初始化特性

c98中不支持花括号进行初始化,编译时会报错,在11当中初始化可以通过{}括号进行统一初始化。

c98编译报错

c++11: 

#include <iostream>
#include <set>
#include <string>
#include <vector>

int main()
{

    std::string str = {"111"};
    std::vector<std::string> vec = {"aaa","bbb","cc"};

    std::cout<<"str = "<<str.c_str()<<std::endl;

    for(auto it : vec){
        std::cout<<"it = "<<it.c_str()<<std::endl;
    }
    
    return 0;
}

输出:

root@ubuntu:~/mySpace/other/ERT# ./a.out 
str = 111
it = aaa
it = bbb
it = cc
root@ubuntu:~/mySpace/other/ERT# 

2.关键提auto自动推导变量类型

#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <map>

int main()
{

    std::vector<std::string> vec = {"aaa","bbb","cc"};//
    // 直接根据实际情况推导相关变量类型
    for(auto it : vec){
        std::cout<<"it = "<<it.c_str()<<std::endl;
    }
    std::map<std::string,std::string> m = {{"11","111"},{"33","333"}};
    for(auto &it : m){
        std::cout<<"first = "<<it.first.c_str()<<" "<<"second = "<<it.second.c_str()<<std::endl;
    }

    for(auto it = m.begin();it !=m.end();++it){
        std::cout<<"first = "<<it->first.c_str()<<" "<<"second = "<<it->second.c_str()<<std::endl;
    }

    //c++17.c++11不支持该属性
    // for (const auto& [key, value] : myMap) {
    //     std::cout << "Key: " << key << ", Value: " << value << std::endl;
    // }

    return 0;
}

 输出:

root@ubuntu:~/mySpace/other/ERT# ./a.out 
it = aaa
it = bbb
it = cc
first = 11 second = 111
first = 33 second = 333
first = 11 second = 111
first = 33 second = 333
root@ubuntu:~/mySpace/other/ERT#

3.Lambda表达式

        定义匿名函数对象,常用于简化代码,尤其是在需要传递函数作为参数的场景。在编译器编译过程中会将lambda表达式转化成一个匿名类,并且这个匿名类重载了operator()运算符,使得可以像函数一样被调用(类的仿制函数一样)。

        lamda表达式函数会自动推倒返回值无需编写,带返回值的写法,如下:

语法结构表达:

    // lamda表达式函数会自动推到返回值无需编写,带返回值的写法
    auto pFunRet = []() -> std::string {
        std::string s("rrrrr");
        return s;
    }; 
    std::cout<<"pFunRet() = "<<pFunRet().c_str()<<std::endl;

a.无参表达

 // 无参Lamda函数表达式
    auto pFun = [](){
        std::cout<<"This is lamda fun"<<std::endl;
    };
    pFun();

输出:

root@ubuntu:~/mySpace/other/ERT# ./a.out 
This is lamda fun
root@ubuntu:~/mySpace/other/ERT#

如上表示无参数表达式,编译器会将上述函数转化成一个匿名累,如下,将函数转化成匿名类,直接像函数一样调用即可。

class __lambda_anonymous {
public:
    int operator()() const { std::cout<<"This is lamda fun"<<std::endl; }
};

b.有参数表达

   // 带参数
    auto pFunParams = [](int a,std::string s){
         std::cout<<"Params lamda="<<a<<"-"<<s.c_str()<<std::endl;
    };
    pFunParams(100,"LX");

输出:   

root@ubuntu:~/mySpace/other/ERT# ./a.out 
Params lamda=100-LX
root@ubuntu:~/mySpace/other/ERT#

 转化匿名类形式:

class __lambda_anonymous {
public:
    int operator()(int a,std::string s) const { std::cout<<"Params lamda="<<a<<"-"<<s.c_str()<<std::endl; }
};

c.值捕获

        (值捕获,捕获作用域所有值,=可以替换成某个具体的值,表示单个变量捕获)

  // 值捕获,捕获作用域所有值,=可以替换成某个具体的值,表示单个变量捕获
    int val_0 = 1;
    int val_2 = 2;
    auto pFunValue = [=](){
        // 值捕获都是只读权限,强行改变会编译报错,提醒read-only 'val_0'
        // val_0 += 1;
        // val_2 += 2;
        std::cout<<"val_0="<<val_0<<" val_2="<<val_2<<std::endl;
    };
    pFunValue();

输出: 

root@ubuntu:~/mySpace/other/ERT# ./a.out 
val_0=1 val_2=2
root@ubuntu:~/mySpace/other/ERT#

  编译器转化:

class __lambda_anonymous {
private:
    int val_0 = 1;
    int val_2 = 2;
public:
    __lambda_anonymous(int val_0,int val_2) : val_0(val_0),val_2(val_0) {}  // 构造函数
    int operator()() const { std::cout<<"val_0="<<val_0<<" val_2="<<val_2<<std::endl;; }
};

d.mutble关键字

        通过该关键字修饰等价于函数内部做了一份拷贝,之后再对拷贝进行操作,故而可以修改其捕获value2, 

   // 值捕获2
    int value2 = 100;
    std::cout<<"before modify value2="<<value2<<std::endl;
    auto pFunValue2 = [value2]() mutable { 
        // mutable 修饰等价于函数内部做了一份拷贝,之后再对拷贝进行操作,故而可以修改其捕获value2,如下输出结果展示:
        // before modify value2=100
        // value2=211
        // after modify value2=100
        value2 += 111;
        std::cout<<"value2="<<value2<<std::endl;
    };
    pFunValue2();
    std::cout<<"after modify value2="<<value2<<std::endl;

输出:

root@ubuntu:~/mySpace/other/ERT# ./a.out 
before modify value2=100
value2=211
after modify value2=100
root@ubuntu:~/mySpace/other/ERT#

编译器匿名类:

class __lambda_unnamed {
    int value2 ;
public:
    __lambda_unnamed(int value2 ) : value2 (value2 ) {}
    int operator()(int value2) {  // 注意:不再是const
        value2 += 111;
        std::cout<<"value2="<<value2<<std::endl;
    }
};

e.引用捕获

        引用修改的是捕获的变量的本身,如下:

  // 引用捕获
    int quote = 100;
    std::cout<<"before modify quote="<<quote<<std::endl;
    auto pFunQuote = [&quote](){
        quote += 20; // 引用修改的是捕获的变量的本身,如下输出结果:
        //before modify quote=100
        //quote=120
        //after modify quote=120
        std::cout<<"quote="<<quote<<std::endl;
    };
    pFunQuote();
    std::cout<<"after modify quote="<<quote<<std::endl;
    return 0;

输出:   

root@ubuntu:~/mySpace/other/ERT# ./a.out 
before modify value2=100
value2=211
after modify value2=100
root@ubuntu:~/mySpace/other/ERT# 

 编译器转换:

class __lambda_anonymous {
private:
    int quote;  // 值捕获的成员变量
public:
    __lambda_anonymous(int quote) : quote(quote) {}  // 构造函数
    int operator()(int quote) const { quote += 1;std::cout<<"quote="<<quote<<std::endl;; }
};

4.并发编程

   a.创建线程:

#include <iostream>
#include <thread>

void hello() {
    std::cout << "Hello from thread!\n";
}

int main() {
    std::thread t(hello);  // 创建线程并启动
    t.join();              // 等待线程结束

    // 分离线程,线程结束后自动释放资源
    // 注意:分离后不能再join()
    // t.detach();  
    return 0;
}

   b.带参数线程处理函数

#include <iostream>
#include <thread>
void print(int id, const std::string& name) {
    std::cout << "ID: " << id << ", Name: " << name << "\n";
}

int main() {
    std::thread t(print, 1, "Thread1");
    t.join();
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
ID: 1, Name: Thread1
root@ubuntu:~/mySpace/other/ERT#

   c.参数引用传递

#include <iostream>
#include <thread>
void modify(int& x) { 
    x *= 2; 
}

int main() {
    int x = 10;
    std::thread t(modify, std::ref(x));
    t.join();
    std::cout << x <<std::endl;  // 输出20
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
20
root@ubuntu:~/mySpace/other/ERT# 

   d.线程id和当前线程

#include <iostream>
#include <thread>

int main() {
    std::thread::id main_thread_id = std::this_thread::get_id(); // 当前线程
    std::thread t([&]() {
        if (std::this_thread::get_id() == main_thread_id) {
            std::cout << "This is the master thread\n";
        } else {
            std::cout << "This is a son thread\n";
        }
    });
    t.join();
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
This is a son thread
root@ubuntu:~/mySpace/other/ERT# 

e.互斥锁 

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;
int shared_data = 0;

void increment() {
    mtx.lock();
    std::thread::id current_id = std::this_thread::get_id();
    std::cout<<"current id = "<<current_id<<std::endl;
    ++shared_data;
    mtx.unlock();
}

// 更安全的方式:使用RAII锁
void safe_increment() {
    // 离开作用域时自动解锁
    std::lock_guard<std::mutex> lock(mtx);
    ++shared_data;
}

int main() {
    std::thread::id main_thread_id = std::this_thread::get_id(); // 当前线程
    std::thread t1(increment);
    std::thread t2(increment);
    t1.join();
    t2.join();
    std::cout<<"shared_date ="<<shared_data<<std::endl;
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
current id = 140137569199872
current id = 140137560807168
shared_date =2
root@ubuntu:~/mySpace/other/ERT#

f.原子操作

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>

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

void increment() {
    for (int i = 0; i < 1000; ++i) {
        ++counter;  // 原子操作,无需额外同步
    }
}

int main() {
    std::thread t1(increment);
    std::thread t2(increment);
    t1.join();
    t2.join();
    std::cout << counter<<std::endl;  // 总是2000
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
2000
root@ubuntu:~/mySpace/other/ERT#

g.异步操作

        字段(Future/Promise/Async) std::futurestd::promise 和 std::async 来简化异步编程,它们共同构成了 C++ 的 并发编程模型,用于处理多线程任务和异步操作

std::async 简化异步任务的启动

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <future>

int compute() {
    // 模拟耗时计算
    std::this_thread::sleep_for(std::chrono::seconds(1));
    return 42;
}

void fun(){
    std::cout<<"This fun"<<std::endl;
}
int main() {
    std::future<int> result = std::async(std::launch::async, compute);
    // 可以做其他工作创建一个线程运行
    std::thread t(fun);
    t.join();
    std::cout << "Result: " << result.get() << "\n";  // 阻塞直到结果就绪
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
This fun
Result: 42
root@ubuntu:~/mySpace/other/ERT#

 std::future: 用于获取异步计算的结果(阻塞等待)-未来值

  • 功能:表示一个 异步计算的结果(可能在另一个线程中计算)。

  • 特点

    • 通过 get() 获取结果(如果结果未就绪,会阻塞当前线程直到计算完成)。

    • 只能获取一次结果(第二次调用 get() 抛出异常)。

    • 通过 wait() 等待结果就绪(不获取值)。

  • 适用场景:获取异步任务(如 std::async 或 std::promise)的返回值 

        案例:

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <future>

int compute() {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "during: " << 2 << std::endl;
    return 42;  // 模拟耗时计算
}

int main() {
    std::future<int> fut = std::async(std::launch::async, compute);// 异步启动一个任务
    std::cout << "Waiting for result..." << std::endl;
    int result = fut.get();  // 阻塞直到 compute() 完成
    std::cout << "Result: " << result << std::endl;
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
Waiting for result...
during: 2
Result: 42
root@ubuntu:~/mySpace/other/ERT# 

 std::promise: 用于线程间传递数据(生产者-消费者模式)

  • 功能:用于 在线程间传递数据,允许一个线程设置值,另一个线程通过 std::future 获取该值。

  • 特点

    • promise.set_value() 设置值,并通知 future 可以获取。

    • 如果 promise 被销毁但未设置值,future.get() 会抛出 std::future_error

  • 适用场景:线程间通信,特别是当一个线程需要等待另一个线程的计算结果时。

案例:

        

#include <iostream>
#include <thread>
#include <mutex>
#include <atomic>
#include <future>

void compute(std::promise<int> prom) {
    prom.set_value(42);  // 设置值
}

int main() {
    // 创建promise对象
    std::promise<int> prom;
    // 通过future获取值
    std::future<int> fut = prom.get_future(); 

    // 通过移动给prom赋值
    std::thread t(compute, std::move(prom));  // 传递 promise
    // 阻塞直到 compute() 设置值
    int result = fut.get();  
    std::cout << "Result: " << result << std::endl;

    t.join();
    return 0;
}

输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out 
Result: 42
root@ubuntu:~/mySpace/other/ERT# 

8. 线程局部存储 

        a.局部全局变量

        thread_local int tls_var = 0;   每个线程都有自己的 tls_var

#include <iostream>
#include <thread>

// 每个线程都有自己的 tls_var,跟linux系统中__thread关键类似,多线程中,每个线程都独有一份全局变量。
thread_local int tls_var = 0;  

void increment() {
    tls_var++;  // 修改的是当前线程的副本
    std::cout << "Thread " << std::this_thread::get_id() 
              << ": tls_var = " << tls_var << std::endl;
}

int main() {
    std::thread t1(increment);  // t1 的 tls_var 初始为 0,执行后变为 1
    std::thread t2(increment);  // t2 的 tls_var 初始为 0,执行后变为 1
    t1.join();
    t2.join();
    return 0;
}

        b.类的静态线程局部变量

        

#include <iostream>
#include <thread>

class Counter {
public:
    // 每个线程有自己的 count
    static thread_local int count;  

    void increment() {
        count++;
        std::cout << "Thread " << std::this_thread::get_id() 
                  << ": count = " << count << std::endl;
    }
};

// 静态成员类外初始化
thread_local int Counter::count = 0;  

int main() {
    Counter c;
    // t1 的 count 初始 0 → 1
    std::thread t1([&c]() {
        c.increment(); 
        });  

    // t2 的 count 初始 0 → 1
    std::thread t2([&c]() { 
        c.increment(); 
        }); 

    t1.join();
    t2.join();
    return 0;
}

输出:每个线程都独享一份变量count
root@ubuntu:~/mySpace/other/ERT# ./a.out 
Thread 140491193476864: count = 1
Thread 140491185084160: count = 1
root@ubuntu:~/mySpace/other/ERT#

以上就是c++11新特性总结笔记,可能还未完全,后续继续完善!欢迎学习指点!共同进步!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2343561.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++23 中 constexpr 的重要改动

文章目录 1. constexpr 函数中使用非字面量变量、标号和 goto (P2242R3)示例代码 2. 允许 constexpr 函数中的常量表达式中使用 static 和 thread_local 变量 (P2647R1)示例代码 3. constexpr 函数的返回类型和形参类型不必为字面类型 (P2448R2)示例代码 4. 不存在满足核心常量…

全面解析React内存泄漏:原因、解决方案与最佳实践

在开发React应用时&#xff0c;内存泄漏是一个常见但容易被忽视的问题。如果处理不当&#xff0c;它会导致应用性能下降、卡顿甚至崩溃。由于React的组件化特性&#xff0c;许多开发者可能没有意识到某些操作&#xff08;如事件监听、异步请求、定时器等&#xff09;在组件卸载…

【FreeRTOS】事件标志组

文章目录 1 简介1.1事件标志1.2事件组 2事件标志组API2.1创建动态创建静态创建 2.2 删除事件标志组2.3 等待事件标志位2.4 设置事件标志位在任务中在中断中 2.5 清除事件标志位在任务中在中断中 2.6 获取事件组中的事件标志位在任务中在中断中 2.7 函数xEventGroupSync 3 事件标…

超级扩音器手机版:随时随地,大声说话

在日常生活中&#xff0c;我们常常会遇到手机音量太小的问题&#xff0c;尤其是在嘈杂的环境中&#xff0c;如KTV、派对或户外活动时&#xff0c;手机自带的音量往往难以满足需求。今天&#xff0c;我们要介绍的 超级扩音器手机版&#xff0c;就是这样一款由上海聚告德业文化发…

【数据可视化-27】全球网络安全威胁数据可视化分析(2015-2024)

&#x1f9d1; 博主简介&#xff1a;曾任某智慧城市类企业算法总监&#xff0c;目前在美国市场的物流公司从事高级算法工程师一职&#xff0c;深耕人工智能领域&#xff0c;精通python数据挖掘、可视化、机器学习等&#xff0c;发表过AI相关的专利并多次在AI类比赛中获奖。CSDN…

【6G 开发】NV NGC

配置 生成密钥 API Keys 生成您自己的 API 密钥&#xff0c;以便通过 Docker 客户端或通过 NGC CLI 使用 Secrets Manager、NGC Catalog 和 Private Registry 的 NGC 服务 以下个人 API 密钥已成功生成&#xff0c;可供此组织使用。这是唯一一次显示您的密钥。 请妥善保管您的…

SIEMENS PLC程序解读 -Serialize(序列化)SCATTER_BLK(数据分散)

1、程序数据 第12个字节 PI 2、程序数据 第16个字节 PI 3、程序数据 第76个字节 PO 4、程序代码 2、程序解读 图中代码为 PLC 梯形图&#xff0c;主要包含以下指令及功能&#xff1a; Serialize&#xff08;序列化&#xff09;&#xff1a; 将 SRC_VARIABLE&#xff…

宁德时代25年时代长安动力电池社招入职测评SHL题库Verify测评语言理解数字推理真题

测试分为语言和数字两部分&#xff0c;测试时间各为17分钟&#xff0c;测试正式开始后不能中断或暂停

【硬核解析:基于Python与SAE J1939-71协议的重型汽车CAN报文解析工具开发实战】

引言&#xff1a;重型汽车CAN总线的数据价值与挑战 随着汽车电子化程度的提升&#xff0c;控制器局域网&#xff08;CAN总线&#xff09;已成为重型汽车的核心通信网络。不同控制单元&#xff08;ECU&#xff09;通过CAN总线实时交互海量报文数据&#xff0c;这些数据隐藏着车…

Uniapp 自定义 Tabbar 实现教程

Uniapp 自定义 Tabbar 实现教程 1. 简介2. 实现步骤2.1 创建自定义 Tabbar 组件2.2 配置 pages.json2.3 在 App.vue 中引入组件 3. 实现过程中的关键点3.1 路由映射3.2 样式设计3.3 图标处理 4. 常见问题及解决方案4.1 页面跳转问题4.2 样式适配问题4.3 性能优化 5. 扩展功能5.…

记录一次使用面向对象的C语言封装步进电机驱动

简介 (2025/4/21) 本库对目前仅针对TB6600驱动下的42步进电机的基础功能进行了一定的封装, 也是我初次尝试以面向对象的思想去编写嵌入式代码, 和直流电机的驱动步骤相似在调用stepmotor_attach()函数和stepmotor_init()函数之后仅通过结构体数组stepm然后指定枚举变量中的id即…

Spark-streaming核心编程

1.导入依赖‌&#xff1a; <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming-kafka-0-10_2.12</artifactId> <version>3.0.0</version> </dependency> 2.编写代码‌&#xff1a; 创建Sp…

vue3+TS+echarts 折线图

需要实现的效果如下 <script setup lang"ts" name"RepsSingleLineChart">import * as echarts from echartsimport { getInitecharts } from /utils/echartimport type { EChartsOption } from echarts// 定义 props 类型interface Props {id: strin…

小火电视桌面TV版下载-小火桌面纯净版下载-官方历史版本安装包

别再费心地寻找小火桌面的官方历史版本安装包啦&#xff0c;试试乐看家桌面吧&#xff0c;它作为纯净版本的第三方桌面&#xff0c;具有诸多优点。 界面简洁纯净&#xff1a;乐看家桌面设计简洁流畅&#xff0c;页面简洁、纯净无广告&#xff0c;为用户打造了一个干净的电视操…

androidstudio安装配置

B站配置视频AndroidStudio安装配置教程&#xff08;最新版本教程&#xff09;3分钟搞定 快速安装使用_哔哩哔哩_bilibili 1、环境变量 D:\AndroidSdk ANDROID_HOME ANDROID_SDK_HOME 2、新建 3、配置 distributionUrlhttps://mirrors.cloud.tencent.com/gradle/gradle-8.11.1-…

《AI大模型趣味实战》基于RAG向量数据库的知识库AI问答助手设计与实现

基于RAG向量数据库的知识库AI问答助手设计与实现 引言 随着大语言模型&#xff08;LLM&#xff09;技术的快速发展&#xff0c;构建本地知识库AI问答助手已成为许多企业级应用的需求。本研究报告将详细介绍如何基于FLASK开发一个使用本地OLLAMA大模型底座的知识库AI问答助手&…

BeeWorks Meet:私有化部署视频会议的高效选择

在数字化时代&#xff0c;视频会议已成为企业沟通协作的重要工具。然而&#xff0c;对于金融、政务、医疗等对数据安全和隐私保护要求极高的行业来说&#xff0c;传统的公有云视频会议解决方案往往难以满足其严格的安全标准。此时&#xff0c;BeeWorks Meet 私有化部署视频会议…

IPv6 技术细节 | 源 IP 地址选择 / Anycast / 地址自动配置 / 地址聚类分配

注&#xff1a;本文为 “IPv6 技术细节” 相关文章合集。 部分文章中提到的其他文章&#xff0c;一并引入。 略作重排&#xff0c;未整理去重。 如有内容异常&#xff0c;请看原文。 闲谈 IPv6 - 典型特征的一些技术细节 iteye_21199 于 2012-11-10 20:54:00 发布 0. 巨大的…

【工具】使用 MCP Inspector 调试服务的完全指南

Model Context Protocol (MCP) Inspector 是一个交互式开发工具&#xff0c;专为测试和调试 MCP 服务器而设计。本文将详细介绍如何使用 Inspector 工具有效地调试和测试 MCP 服务。 1. MCP Inspector 简介 MCP Inspector 提供了直观的界面&#xff0c;让开发者能够&#xff…

【音视频】AVIO输入模式

内存IO模式 AVIOContext *avio_alloc_context( unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t (*seek)(…