cpp 汇总

220119

 

 

14 左值 右值 move

后面还有一些要汇总

1、
int&& b = 6; //这个时候只能右值引用

2、

template<typename T>
void printValue( T&& t)
{
    cout << "r-value: " << t << endl;
    t = 0;
}

 


函数里面的T&& ,即可也接收左值也可以接收右值,除非被重写
3

template<typename T>
void printValue(const T&& t) //这个时候只能接收右值
{
    cout << "r-value: " << t << endl;
    t = 0;
}

 


二完美转发
就是再传参时,还能是右值
forward的由来:保持住参数的右值属性。

模板函数中的推导类型,作为另一函数的参数时,不管实参是什么类型,作为另一个参数的实参时,都变成了左值。
因为C++里规定函数的形参就是左值,不管调用侧的实参是否是右值。
https://www.cnblogs.com/xiaoshiwang/p/9589008.html


move 转换为右值引用,forward右值转发出去

move的本质就是帮助编译器选择重载函数, 告诉编译器"请尽量把此参数当做右值来处理"

move 是强制转换??

三move重要

move的本质就是帮助编译器选择重载函数, 告诉编译器"请尽量把此参数当做右值来处理"

但是要清楚机制的时机,主要是拷贝构造函数的时候


void testfun(HasPtrMem a){};

void main(){
    HasPtrMem a;
    testfun(a);  //调用拷贝构造函数
    testfun(std::move(a));//调用移动构造函数
}
所以开源项目中 move使用非常多

 

 

13、 decltype用于数组和指针

12、 初始化列表 友元 运算符重载

#include <iostream>
#include <string>
#include <memory>
using namespace std;

class Point{
    
public:
    int x, y;

    Point(int a, int b):x(a),y(b){
    }

    friend Point operator+(const Point&a, const Point&b){
        Point s(a.x+b.x,a.y+b.y);
        return s;
    }
    friend ostream& operator<<(ostream& o, const Point& p){
        return o << p.x << "," << p.y << endl;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "11" << endl;
    Point a(1, 2), b(3, 4);
    Point c = a + b;
    cout << c<< endl;
    return 0;
}

 

11、 cpp11线程与互斥

#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
#include <vector>
using namespace std;

int number = 0;
mutex g_lock;

int ThreadProc1()
{
    
    for (int i = 0; i < 100; i++)
    {
        g_lock.lock();
        ++number;
        cout << "thread 1 :" << number << endl;
        g_lock.unlock();
        this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    
    return 0;
}

int ThreadProc2()
{
    
    for (int i = 0; i < 100; i++)
    {
        g_lock.lock();
        --number;
        cout << "thread 2 :" << number << endl;
        g_lock.unlock();
        this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    
    return 0;
}

主函数
    thread t1(ThreadProc1);
    thread t2(ThreadProc2);

    t1.detach();
    t2.detach();

    system("pause");

 

 

10、

运算符重载、友元、初始化列表

class Point{
    
public:
    int x, y;
    //初始化列表,记住按照内存里的顺序,不然随机值吧
    //注意,成员变量的初始化顺序与初始化列表中列出的变量的顺序无关,它只与成员变量在类中声明的顺序有关
    Point(int a, int b):x(a),y(b){
    }

    friend Point operator+(const Point&a, const Point&b){
        Point s(a.x+b.x,a.y+b.y);
        return s;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "11" << endl;
    Point a(1, 2), b(3, 4);
    Point c = a + b;
    cout << c.x << endl;
    return 0;
}

 

 

9. 空类自动生成四个默认函数

 

 

如果有任何一个构造函数,则这个默认的无参构造函数就失效了,

比如 Empty e;会报错。(注意 Empty e() 不一样,这个是函数声明)

 

 

8、重要

E:\cpp\bj\8

E:\cpp\bj\9月

7、杂记

a、构造函数声明,而没定义 造成错误    LNK2019    无法解析的外部符号 "public: __cdecl MyPlayer::MyPlayer(void)" (??0MyPlayer@@QEAA@XZ);
    MyPlayer(void) ;
加入大括号才是定义    
    MyPlayer(void) {}; 

b、 禁止拷贝构造和赋值,用delete关键字
  
    ConfigManager(const ConfigManager&)=delete;
    ConfigManager& operator=(const ConfigManager&)=delete;
    (例子在单例中应用)
c、 单例模式
    static ConfigManager& get_instance(){
        static ConfigManager instance;
        return instance;
    }

d、假设a为容器对象,a.cbegin()返回的迭代器类型一定是const_iterator
   关于友元,可以定义在类外 ,是独立于当前类的外部函数

 

6、 

   printf("[%s %s] %s: %s: %d\n",__DATE__, __TIME__, __FILE__, __func__, __LINE__);

   获取的是编译时的

5、字节对齐 二维数组 大小端

头文件相互包含,前面影响后面,比如把字节对齐放到最前面,程序就起不来了

4、虚函数一句话结

a、虚函数: 用父类的指针依旧能访问子类的函数
b、为什么多态的 析构函数要用虚的,因为不用的话访问不到子类的析构函数。

 

3、c cpp传递全局变量

无法解析的外部符号

这时需要 加 extern
extern "C"{
    extern int gwnd;
}

 

2、stl 里find和  find_first_of

     string str = "nextitem=0|url=rtsp";

    int d = str.find_first_of("url");
    int d1 = str.find_first_of("url=r");
    int d2 = str.find("url=r");
    cout << d <<"," << d1<<"," <<d2<< endl;、
    11,8,11

 

find_first_of 找到子串里任何一个字符

find 才是找子串

https://blog.csdn.net/cnd2449294059/article/details/74939591

1、auto 于区间迭代

for (auto address_entry: address_book)
{
    cout<<address_entry.first<<" "<address_entry.second<<endl;
}

http://towriting.com/blog/2013/08/08/improved-type-inference-in-cpp11/

posted @ 2022-01-16 22:42  cnchengv  阅读(39)  评论(0编辑  收藏  举报