C++中的常用高级用法

  1. vector的指针用法,代码如下:
#include <thread>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    std::vector<int> v1 = {1,2,3,4,5};
    std::vector<int>* vptr = &v1;
    vptr->push_back(4);
    for(auto v : v1)
    {
        cout<<v<<" "<<endl;
    }
    return 0;
}

测试输出结果:

1 2 3 4 5 4 
  1. std::ref的用法,代码如下:
    (1)主要用在两个地方:其一是函数式编程中,用std::bind绑定参数; 其二是在多线程编程时,传入参数必须用std::ref引用传入;
#include <thread>
#include <iostream>
#include <vector>

void fun(int& num)  //参数为int&
{
    while (num < 10)
        std::cout << num++<<" ";        
}

int main()
{
    int num = 0;
    std::thread t1(fun, std::ref(num));
    std::thread t2(fun, std::ref(num));
    t1.join();
    t2.join();

    return 0;
}

输出结果如下(其一结果):

0 2 3 4 5 6 7 8 9 1

输出结果如下(其二结果):

1 2 3 4 5 6 7 8 9 0
  1. string::npos的用法:
    用法:npos 是一个常数,用来表示不存在的位置;
    案例:有两个字符串a、b,判断a字符串是否包含b字符串,测试代码如下:
#include <thread>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string a = "12345678";
    string b = "12345";
    //如果字符串不存在包含关系,那么返回值就一定是npos
    if(a.find(b)!=string::npos){
        cout<<"yes"<<endl;
    }else{
        cout<<"no"<<endl;
    }
    return 0;
}

测试结果如下:

yes //a是包含b的

4)map容器中,使用宏定义的应用案例,代码如下:

#include <thread>
#include <iostream>
#include <string>
#include <map>

using namespace std;
#define KEYMAP(T1,T2) map<T1,T2> //用宏定义对map容器操作

int main()
{
    KEYMAP(int,int) m1;
    m1.insert(make_pair<int,int>(2,4));

    return 0;
}

5)using定义别名的案例,代码如下:

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

using namespace std;

#define KEYMAP(T1,T2) map<T1,T2> //用宏定义对map容器操作
using mypair = std::pair<int, double>; //using的用法:使用别名 类型别名=原类型

int main(void)
{
    KEYMAP(int,double) key;
    key.insert(mypair(3,7.8));
    cout<<key.at(3)<<endl;

    return 0;
}

输出结果如下:

7.8

备注:C++可以用两个关键字定义别名 typedef和using,但一个使用地方是不可以用typedef而只能用using,那就算定义模板类,如下所示:

template <typename T>
typedef std::vector<T> v;//使用typedef 编译器会报错error: template declaration of ‘typedef’

template <typename T>
using v = std::vector<T>;//使用using //编译器不会报错,限于C++11以后的编译器

6)结构体的字节对齐案例,利用(pragma的用途:pragma用于指示编译器完成一些特定的动作),代码如下:
#pragma 是一个预处理命令,相当于#define; //pragma的英文: 编译指示;

#include <iostream>

using namespace std;
//按1个字节对齐方式 
#pragma pack(push, 1) //这句话相当执行了两句: #pragma pack(push) //保存之前的对齐方式;(2)pack(1) 设置当前对齐方式;
typedef struct
{
  char A;
  int roll;
} DaoyuanImu;
#pragma pack(pop)  //恢复以前的对齐方式

int main(void)
{
    cout<<sizeof(DaoyuanImu)<<endl; //输出的是5;如果屏蔽掉 #pragma pack(push)和#pragma pack(pop);即按默认对齐:输出为8;
    return 0;
}

  1. auto自动类型在多线程std::thread的应用,代码如下:
    线程封装到类里面,并用auto自动类型推到进行接收;
#include<iostream>
#include<thread>

using namespace std;

class Demo
{
public: 
    void fun1(void)
    {
        cout<<"hello world"<<endl;
    }
    void fun2(int x, int y)
    {
        cout<<"x is:"<<x<<" y is:"<<y<<endl;
    }
    std::thread th1(void)
    {
        return std::thread(&Demo::fun1,this);
    }

    std::thread th2(int a,int b)
    {
        return std::thread(&Demo::fun2,this,a,b);
    }
};
int main(void)
{
    Demo* p = new Demo();
    auto tmp = p->th1();
    tmp.join();

    p->th2(2,3).detach();
    return 0;
}

输出结果如下:

hello world
x is:2 y is:3
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值