栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++学习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++学习

类构造函数初始化列表c++
#include        // std::cout
using namespace std;
class CMyClassaa {
    public:
    CMyClassaa(int x, int y);
    int m_x;
    int m_y=5;
};

CMyClassaa::CMyClassaa(int x, int y) : m_y(y), m_x(m_y)
{
}

int main()
{
    CMyClassaa abc(1,2);
    cout << abc.m_x << ',' << abc.m_y << endl;
    return 0;
}

结果:

32764,2

abc.m_x成员的初始化为不确定值。

锁的使用c++
#include        // std::cout
#include          // std::thread
//#include 
#include 
#include 
using namespace std;
template
struct Node
{
    Node(const T &value) : data(value) { }
    T data;
    Node *next = nullptr;
};

template
class WithLockListaa
{
    mutex mtx;
    Node *head;
    public:
    void pushFront(const T &value)
    {
        auto *node = new Node(value);
        lock_guard csd(mtx); //表示一个类
        node->next = head;
        head = node;
    }

};

template
class LockFreeListbb
{
   struct atomic *> head;
    public:
    void pushFront(const T &value)
    {
        auto *node = new Node(value);
        node->next = head.load();
        while(!head.compare_exchange_weak(node->next, node));
    }
};


struct adopt_lock_t_abcs { 
    int a;
    adopt_lock_t_abcs()=default; 
};
adopt_lock_t_abcs    aaa
{ };

int main()
{
    const int SIZE = 1000000;
    auto start = chrono::steady_clock::now();
    WithLockListaa wlList;
    for(int i = 0; i < SIZE; ++i)
    {
        wlList.pushFront(i);
    }
    auto end = chrono::steady_clock::now();
    chrono::duration micro = end - start;
    cout << "--------- lock list costs micro:" << micro.count() << endl;
    start = chrono::steady_clock::now();
    LockFreeListbb lfList;
    for(int i = 0; i < SIZE; ++i)
    {
        lfList.pushFront(i);
    }
    end = chrono::steady_clock::now();
    micro = end - start;
    cout << "********* lock list costs micro:"<< micro.count() << endl;
    return 0;
}

结果:

--------- lock list costs micro:42004.7
********* lock list costs micro:56856.4
std_mutex.h:
  	template
    class lock_guard
    {
    public:
      typedef _Mutex mutex_type;

      explicit lock_guard(mutex_type& __m) : _M_device(__m)
      { _M_device.lock(); }

      lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
      { } // calling thread owns mutex

      ~lock_guard()
      { _M_device.unlock(); }

      lock_guard(const lock_guard&) = delete;
      lock_guard& operator=(const lock_guard&) = delete;

    private:
      mutex_type&  _M_device;
    };
命名空间c++
#include 
using namespace std;


namespace test2{
    void func(){
        cout << "11111" << endl;
    }
    int b=5;
}
struct node {
    int data;
    void init(int a) {
        this->data = a;
    }
    //类内定义的构造函数没有带分号';'
    node() :data(5) {} //无参数的构造函
    node(int a) :data(a) {}//有参数的构造函数
}aaNode[10];

namespace test {
    struct test_node {
    int data;
    }tes_Node[10];

    int b=10;
    void func(){
      cout << "22222" << endl;
   }
}
using namespace test;
using namespace std;
//using namespace test2;    //多次声明含有同样函数名或变量名的命名空间会出错

int main()
{
    aaNode[0] = {2};
    node aaa;
    cout<<"aaa.data="<aaa.data=5
b=10
22222
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号