c++11新特性随笔
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,
// 值捕获2int 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=100value2 += 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) { // 注意:不再是constvalue2 += 111;std::cout<<"value2="<<value2<<std::endl;}
};
e.引用捕获
引用修改的是捕获的变量的本身,如下:
// 引用捕获int quote = 100;std::cout<<"before modify quote="<<quote<<std::endl;auto pFunQuote = ["e](){quote += 20; // 引用修改的是捕获的变量的本身,如下输出结果://before modify quote=100//quote=120//after modify quote=120std::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; // 输出20return 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; // 总是2000return 0;
}输出:
root@ubuntu:~/mySpace/other/ERT# ./a.out
2000
root@ubuntu:~/mySpace/other/ERT#
g.异步操作
字段(Future/Promise/Async) std::future
、std::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,执行后变为 1std::thread t2(increment); // t2 的 tls_var 初始为 0,执行后变为 1t1.join();t2.join();return 0;
}
b.类的静态线程局部变量
#include <iostream>
#include <thread>class Counter {
public:// 每个线程有自己的 countstatic 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 → 1std::thread t1([&c]() {c.increment(); }); // t2 的 count 初始 0 → 1std::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新特性总结笔记,可能还未完全,后续继续完善!欢迎学习指点!共同进步!!!