小编Zha*_*ang的帖子

在调用std :: numeric_limits <unsigned char>成员之前,一元"+"的目的是什么?

在cppreference的文档中看到了这个例子std::numeric_limits

#include <limits>
#include <iostream>

int main() 
{
    std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";

    std::cout << "uchar\t"
              << +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
              << +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
              << +std::numeric_limits<unsigned char>::max() << '\n';
    std::cout << "int\t"
              << std::numeric_limits<int>::lowest() << '\t'
              << std::numeric_limits<int>::min() << '\t'
              << std::numeric_limits<int>::max() << '\n';
    std::cout << "float\t"
              << std::numeric_limits<float>::lowest() << '\t'
              << std::numeric_limits<float>::min() << '\t'
              << std::numeric_limits<float>::max() << '\n';
    std::cout << "double\t"
              << std::numeric_limits<double>::lowest() << '\t'
              << std::numeric_limits<double>::min() << '\t'
              << std::numeric_limits<double>::max() …
Run Code Online (Sandbox Code Playgroud)

c++ char unary-operator

129
推荐指数
4
解决办法
4070
查看次数

传递一个数组(变量)作为模板参数

typedef int INT5[5] ;
    template<INT5 i5>
    void fun()
    {
        for (auto i : i5)
        {
            cout << i << endl;
        }
    }

    void test()
    {
        int i5[5] = { 2,3,1,2,4 };
        fun<i5>();//error
    }
Run Code Online (Sandbox Code Playgroud)

我想传递一个数组作为模板参数,但失败了.

错误C2672:'__ ExplicitVarTypeTest :: fun':找不到匹配的重载函数

错误C2971:'__ ExplicitVarTypeTest :: fun':模板参数'i5':'i5':具有非静态存储持续时间的变量不能用作非类型参数

有人给我一个解决方案,将varible作为一个有趣的参数传递而不是模板的参数.但在我的情况下,我无法改变函数的论点.因为它是一个回调函数.

实际上,我需要这样做

typedef char MyCharMap[UINT8_MAX];
    template<MyCharMap keymap>
    LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        //MSDN???nCode<0?HC_ACTION????CallNextHookEx??
        if (nCode >= HC_ACTION)
        {
            KBDLLHOOKSTRUCT* pStruct = (KBDLLHOOKSTRUCT*)lParam;
            if (keyMap[pStruct->vkCode])
            {
                switch (wParam)
                {
                case WM_KEYDOWN:
                    keybd_event(keyMap[pStruct->vkCode], 0, 0, 0); …
Run Code Online (Sandbox Code Playgroud)

c++

10
推荐指数
1
解决办法
1197
查看次数

如何在std :: vector中生成对象且不复制?

有一个学生班

class Student
{
public:
    inline static int current_id_max = 0;
    int id = 0;
    string name;
public:
    Student()
    {
        id = (++current_id_max);
        cout << "Student constructor\n";
    }
    Student(const string& _name)
    {
        name = _name;
        id = (++current_id_max);
        cout << "Student constructor: " << _name << endl;
    }
    Student(const Student& o)
    {
        name = o.name;
        id = (++current_id_max);
        cout << "Student constructor copy: " << name << endl;
    }
    ~Student() { cout << "Student destructor: " << name << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ performance stl c++11

8
推荐指数
1
解决办法
180
查看次数

在哪里可以找到所有C ++十进制类型指示器?

我在哪里可以找到所有C ++十进制类型指示符,例如

long l = 0L;
Run Code Online (Sandbox Code Playgroud)

我也知道LU f d。还有其他吗?我在哪里可以找到它们?如何明确指示未签名的短裤?

c++ keyword

6
推荐指数
1
解决办法
136
查看次数

std :: condition_variable :: wait_until与std :: this_thread :: sleep_for相比有什么优势吗?

在等待时间的情况下:

我们的软件在后台运行,并且每20-30分钟与服务器同步数据一次。

我想用

std::this_thread::sleep_for
Run Code Online (Sandbox Code Playgroud)

但是我的上司强烈反对任何形式的睡眠功能。他建议

std::condition_variable::wait_until(lock, timeout-time, pred)
Run Code Online (Sandbox Code Playgroud)

我想知道在这种情况下sleep_for是否有任何不利之处?

c++ multithreading c++11

6
推荐指数
1
解决办法
107
查看次数

regex_replace,为什么会失去$ 1?

string s = " 'I'd go.' ";
s = std::regex_replace(s, std::regex("((^| )')|('($| ))"), "$1(Quotation, )");
cout << s; // '(Quotation, )I'd go.(Quotation, )
Run Code Online (Sandbox Code Playgroud)

我想更换'(Quotation, )的,我不想失去原来的'.所以,我用$1的意思是原作'.我不想更换'I'd.

^意味着如果'它位于字符串的开头,它将被替换. $表示字符串的结尾.

结果应该是:

'(报价,)我会去的.(报价)

但实际上结果是

'(报价,)我去.(报价单)

左引号替换工作正常,但右边输了'.为什么?

c++ regex c++11 visual-studio-2017

5
推荐指数
1
解决办法
272
查看次数

什么是std :: this_thread :: sleep_for(2s)中的s?

我发现std :: this_thread :: sleep_for可以处理时间单位s.

std::this_thread::sleep_for(2s);
Run Code Online (Sandbox Code Playgroud)

但我不知道是什么s2s是.

c++ c++14

5
推荐指数
1
解决办法
480
查看次数

为什么 printf 输出浮动 1.45 保留一位小数,而 1.445 保留两位小数在不同的行为中?

printf("%.1f, %.2f", 1.45, 1.445);
Run Code Online (Sandbox Code Playgroud)

输出

1.4、1.45

1.45 被切片但 1.445 被四舍五入。

我发现了这个, 为什么 printf 舍入浮点数? 这解释了规范建议对浮动进行四舍五入。

编辑:我使用 VS2017(在 C++ 项目中)和 Dev-C 对其进行了测试。

c c++ floating-point specifications

4
推荐指数
1
解决办法
173
查看次数

std::atomic 的读取线程安全吗?

我的目的——只有一个线程可以同时处理数据。我使用过 std::mutex、std::unique_lock,try_lock效果很好。

但有人争论,为什么不直接使用原子变量呢?像这样的事情,

#include <atomic>
#include <thread>

std::atomic_bool isRunning = false;

void process() {    
    if (isRunning)
    {
        return;
    }
    isRunning = true;
    cout << "processing ..." << endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));
    cout << "finished" << endl;
    isRunning = false;
}

int main(int argc, wchar_t* argv[])
{
    std::thread th1(process);
    std::thread th2(process);
    std::thread th3(process);
    std::thread th4(process);
    std::thread th5(process);

    th1.join();
    th2.join();
    th3.join();
    th4.join();
    th5.join();
}
Run Code Online (Sandbox Code Playgroud)

根据我的经验,atomic 对于写入来说是安全的,但对于读取来说并不安全。例如,if (isRunning)可能有 2 个线程false同时获得相同的值,并且都运行到处理代码中。

但我测试了好几次,都没有这种情况,都只显示一条信息

加工 ...

我没有找到任何相关文档,您对此有何看法?

c++ multithreading c++20

4
推荐指数
1
解决办法
517
查看次数

增加月份行为的原理是什么?

增加月份的示例

28/02/2009 + 1个月= 2009年3月31日

#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/gregorian/formatters.hpp>
#include <iostream>
using namespace boost::gregorian;
using namespace std;

int main()
{
typedef boost::date_time::month_functor<date> add_month;
        date d(2019, Feb, 28);
        add_month mf(1);
        date d2 = d + mf.get_offset(d);
        cout << to_simple_string(d2) << endl;//output:2019-Mar-31
}
Run Code Online (Sandbox Code Playgroud)

输出为2019年3月31日而不是2019年3月28日。这种行为的理论基础是什么?谈论它以类似的添加月份代码在C#和delphi中输出2019年3月28日。

c++ boost date

3
推荐指数
1
解决办法
61
查看次数