1 新增关键字及新用法
========================================================================================================
1.1 auto关键字及用法
#include <iostream>
#pragma once
auto AddTest(int a, int b)
{
return a + b;
}
int main()
{
auto index = 10;
auto str = "abc";
auto ret = AddTest(1,2);
std::cout << "index:" << index << std::endl;
std::cout << "str:" << str << std::endl;
std::cout << "res:" << ret << std::endl;
}
1.2 nullptr关键字及用法
#include <iostream>
class Test
{
public:
void TestWork(int index)
{
std::cout << "TestWork 1" << std::endl;
}
void TestWork(int* index)
{
std::cout << "TestWork 2" << std::endl;
}
};
int main()
{
Test test;
test.TestWork(nullptr);
}在这里插入代码片
1.3 for循环语法
#include <iostream>
int main()
{
int numbers[] = { 1,2,3,4,5 };
std::cout << "numbers:" << std::endl;
for (auto number : numbers)
{
std::cout << number << std::endl;
}
return 0;
}
========================================================================================================
2 新增STL容器
2.1 array
#include <array>
#include <iostream>
int main(){
std::array<int, 4> arrayDemo = {1,2,3,4};
std::cout << "arrayDemo:" << std::endl;
for(auto itor : arrayDemo)
{
std::cout << itor << std::endl;
}
int arrayDemoSize = sizeof(arrayDemo);
std::cout << "arrayDemo size:" << arrayDemoSize << std::endl;
return 0;
}
在这里插入代码片
2.2 forward_list
#include <forward_list>
#include <iostream>
int main()
{
std::forward_list<int> numbers = {1,2,3,4,5,4,4};
std::cout << "numbers:" << std::endl;
for (auto number : numbers)
{
std::cout << number << std::endl;
}
numbers.remove(4);
std::cout << "numbers after remove:" << std::endl;
for (auto number : numbers)
{
std::cout << number << std::endl;
}
return 0;
}
在这里插入代码片
2.3 unordered_map
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
std::unordered_map<std::string, std::string> mymap =
{
{ "house","maison" },
{ "apple","pomme" },
{ "tree","arbre" },
{ "book","livre" },
{ "door","porte" },
{ "grapefruit","pamplemousse" }
};
unsigned n = mymap.bucket_count();
std::cout << "mymap has " << n << " buckets.\n";
for (unsigned i = 0; i<n; ++i)
{
std::cout << "bucket #" << i << " contains: ";
for (auto it = mymap.begin(i); it != mymap.end(i); ++it)
std::cout << "[" << it->first << ":" << it->second << "] ";
std::cout << "\n";
}
return 0;
}
在这里插入代码片
2.4 unordered_set
#include <string>
#include <unordered_set>
#include <set>
int main()
{
std::unordered_set<int> unorder_set;
unorder_set.insert(7);
unorder_set.insert(5);
unorder_set.insert(3);
unorder_set.insert(4);
unorder_set.insert(6);
std::cout << "unorder_set:" << std::endl;
for (auto itor : unorder_set)
{
std::cout << itor << std::endl;
}
std::set<int> set;
set.insert(7);
set.insert(5);
set.insert(3);
set.insert(4);
set.insert(6);
std::cout << "set:" << std::endl;
for (auto itor : set)
{
std::cout << itor << std::endl;
}
}在这里插入代码片
========================================================================================================
3 多线程
3.1 thread
#include <iostream>
#include <thread>
void threadfun1()
{
std::cout << "threadfun1 - 1\r\n" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "threadfun1 - 2" << std::endl;
}
void threadfun2(int iParam, std::string sParam)
{
std::cout << "threadfun2 - 1" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "threadfun2 - 2" << std::endl;
}
int main()
{
std::thread t1(threadfun1);
std::thread t2(threadfun2, 10, "abc");
t1.join();
std::cout << "join" << std::endl;
t2.detach();
std::cout << "detach" << std::endl;
}
3.2 atomic
#include <atomic>
#include <stdio.h>
std::atomic_bool bIsReady = false;
std::atomic_int iCount = 100;
void threadfun1()
{
if (!bIsReady) {
std::this_thread::yield();
}
while (iCount > 0)
{
printf("iCount:%d\r\n", iCount--);
}
}
int main()
{
std::atomic_bool b;
std::list<std::thread> lstThread;
for (int i = 0; i < 10; ++i)
{
lstThread.push_back(std::thread(threadfun1));
}
for (auto& th : lstThread)
{
th.join();
}
}
在这里插入代码片
3.3 condition_variable
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void print_id(int id) {
std::unique_lock<std::mutex> lck(mtx);
while (!ready) cv.wait(lck);
std::cout << "thread " << id << '\n';
}
void go() {
std::unique_lock<std::mutex> lck(mtx);
ready = true;
cv.notify_all();
}
int main()
{
std::thread threads[10];
for (int i = 0; i<10; ++i)
threads[i] = std::thread(print_id, i);
std::cout << "10 threads ready to race...\n";
go();
for (auto& th : threads) th.join();
return 0;
}在这里插入代码片
========================================================================================================
4智能指针内存管理
4.1 shared_ptr
#include <iostream>
#include <memory>
using namespace std;
class Test
{
public:
Test()
{
cout << "Test()" << endl;
}
~ Test()
{
cout << "~Test()" << endl;
}
};
int main()
{
shared_ptr<Test> p1 = make_shared<Test>();
cout << "1 ref: " << p1.use_count() << endl;
{
shared_ptr<Test> p2 = p1;
cout << "2 ref:" << p1.use_count() << endl;
}
cout << "3 ref:" << p1.use_count() << endl;
return 0;
}
在这里插入代码片
4.2 weak_ptr相互引用问题实例
#include <memory>
#include <iostream>
using namespace std;
class TestB;
class TestA
{
public:
TestA()
{
std::cout << "TestA()" << std::endl;
}
void ReferTestB(shared_ptr<TestB> test_ptr)
{
m_TestB_Ptr = test_ptr;
}
~TestA()
{
std::cout << "~TestA()" << std::endl;
}
private:
shared_ptr<TestB> m_TestB_Ptr;
};
class TestB
{
public:
TestB()
{
std::cout << "TestB()" << std::endl;
}
void ReferTestB(std::shared_ptr<TestA> test_ptr)
{
m_TestA_Ptr = test_ptr;
}
~TestB()
{
std::cout << "~TestB()" << std::endl;
}
std::shared_ptr<TestA> m_TestA_Ptr;
};
int main()
{
std::shared_ptr<TestA> ptr_a = std::make_shared<TestA>();
std::shared_ptr<TestB> ptr_b = std::make_shared<TestB>();
ptr_a->ReferTestB(ptr_b);
ptr_b->ReferTestB(ptr_a);
return 0;
}
在这里插入代码片
4.3weak_ptr如何解决相互引用的问题
class TestB;
class TestA
{
public:
TestA()
{
std::cout << "TestA()" << std::endl;
}
void ReferTestB(std::shared_ptr<TestB> test_ptr)
{
m_TestB_Ptr = test_ptr;
}
void TestWork()
{
std::cout << "~TestA::TestWork()" << std::endl;
}
~TestA()
{
std::cout << "~TestA()" << std::endl;
}
private:
std::weak_ptr<TestB> m_TestB_Ptr;
};
class TestB
{
public:
TestB()
{
std::cout << "TestB()" << std::endl;
}
void ReferTestB(std::shared_ptr<TestA> test_ptr)
{
m_TestA_Ptr = test_ptr;
}
void TestWork()
{
std::cout << "~TestB::TestWork()" << std::endl;
}
~TestB()
{
std::shared_ptr<TestA> tmp = m_TestA_Ptr.lock();
tmp->TestWork();
std::cout << "2 ref a:" << tmp.use_count() << std::endl;
std::cout << "~TestB()" << std::endl;
}
std::weak_ptr<TestA> m_TestA_Ptr;
};
int main()
{
std::shared_ptr<TestA> ptr_a = std::make_shared<TestA>();
std::shared_ptr<TestB> ptr_b = std::make_shared<TestB>();
ptr_a->ReferTestB(ptr_b);
ptr_b->ReferTestB(ptr_a);
std::cout << "1 ref a:" << ptr_a.use_count() << std::endl;
std::cout << "1 ref b:" << ptr_a.use_count() << std::endl;
return 0;
}
在这里插入代码片
4.4 unique_ptr
#include <iostream>
#include <memory>
using namespace std;
int main()
{
std::unique_ptr<int> foo;
std::unique_ptr<int> bar;
int* p = nullptr;
foo = std::unique_ptr<int>(new int(100));
bar = std::move(foo);
p = bar.get();
foo.reset(bar.release());
cout << "foo : " << *foo << endl;
cout << "p : " << *p << endl;
delete p;
if (bar)
cout << "bar : " << *bar << endl;
else
cout << "bar已经被释放" << endl;
return 0;
}在这里插入代码片
========================================================================================================
5其他
5.1 function,bind封装可执行对象
#include <iostream>
#include "Test.h"
int add(int a,int b)
{
return a + b;
}
class TestAdd
{
public:
void Add(std::function<int(int, int)> fun, int a, int b)
{
int sum = fun(a, b);
std::cout << "sum:" << sum << std::endl;
}
};
int main()
{
Test test;
test.Add(add, 1, 2);
TestAdd testAdd;
test.Add(std::bind(&TestAdd::Add, testAdd, std::placeholders::_1, std::placeholders::_2), 1, 2);
return 0;
}
在这里插入代码片
5.2 lamda表达式
头文件test.h
class Test
{
public:
void Add(std::function<int(int, int)> fun, int a, int b)
{
int sum = fun(a, b);
std::cout << "sum:" << sum << std::endl;
}
};
在这里插入代码片
test1.cpp
#include <iostream>
int main()
{
auto add = [](int a, int b)->int{
return a + b;
};
int ret = add(1,2);
std::cout << "ret:" << ret << std::endl;
return 0;
}
在这里插入代码片
test2.cpp
#include<iostream>
#include "Test.h"
int add(int a,int b)
{
return a + b;
}
class TestAdd
{
public:
void Add(std::function<int(int, int)> fun, int a, int b)
{
int sum = fun(a, b);
std::cout << "sum:" << sum << std::endl;
}
};
int main()
{
Test test;
test.Add(add, 1, 2);
test.Add([](int a, int b)->int {
std::cout << "lamda add fun" << std::endl;
return a + b;
},1,2);
return 0;
}在这里插入代码片
5.3 左值右值问题
左值引用.cpp
int main()
{
int a = 10;
int& refA = a;
int& b = 1;
return 0;
}在这里插入代码片
右值引用.cpp
int&& a = 1;
int b = 1;
int && c = b;
class A {
public:
int a;
};
A getTemp()
{
return A();
}
A && a = getTemp();
在这里插入代码片
5.4 decltype 类型指示符
int a = 10;
int b = 20;
decltype(a+b) c = 50;
5.5 尾置返回类型
auto func(char x) -> int(*) [10];
在这里插入代码片
5.6 类对象成员的类内初始化
class ClassName
{
public:
int x = 10;
};
在这里插入代码片
5.7 设置构造函数为default
https:
class ClassName{
public:
ClassName(int x);
ClassName()=default;
};
在这里插入代码片
5.8 long long类型
/*
long long 类型实际上没有在C++ 98中存在,而之后被C99标准收录,其实现在市面上大多数编译器
是支持 long long 的,但是这个类型正式成为C++的标准类型是在C++11中。标准要求long long
至少是64位也就是8个字节。一个字面常量使用LL后缀表示long long类型,使用ULL后缀表示
unsigned long long 类型。
*/在这里插入代码片
5.9 列表初始化
int xx[5]={1,2,3,4,5};
int yy[]={6,7,8,9,0};
int a{10};
int b={10};
int c={10.123};
typedef struct Str{
int x;
int y;
}Str;
Str s = {10,20};
class Cls{
public:
int x;
int y;
};
Cls c = {10,20};
vector<int>v1={1,2,3,4,5,6,7,8,9};
vector<int>v2;
v2={3,4,5,6,7};
map<string ,int> m = {
{"x",1},
{"y",2},
{"z",3}
};
vector<int> getVector()
{
return {1,2,3,4,5};
}
int main()
{
vector<int> v = getVector();
cout<<v[0]<<v[1]<<v.size()<<endl;
return 0 ;
}在这里插入代码片
5.10 constexpr 变量
const int a =20;
int a = 20 ;
const int x = a;
int b[x]={0};
int a = 20 ;
constexpr int x = a;
在这里插入代码片
5.11 constexpr函数
constexpr int size(int s)
{
return s*4;
}
int a = 20;
const int b = 30;
constexpr int c = 40;
constexpr int si = size(a);
constexpr int si1 = size(20);
constexpr int si2 = size(b);
constexpr int si3 = size(c);
5.12 using类型别名
typedef int INT;
using INT2 = int;
INT a = 20;
INT2 b = 30;在这里插入代码片
|