小编Ale*_*sky的帖子

为什么std :: vector <uint8_t> :: insert比使用MSVC 2015编译器的std :: copy工作快5倍?

我有一个简单的函数,将字节块复制到std :: vector:

std::vector<uint8_t> v;

void Write(const uint8_t * buffer, size_t count)
{
    //std::copy(buffer, buffer + count, std::back_inserter(v));

    v.insert(v.end(), buffer, buffer + count);
}

v.reserve(<buffer size>);
v.resize(0);

Write(<some buffer>, <buffer size>);
Run Code Online (Sandbox Code Playgroud)

如果我使用std::vector<uint8_t>::insert它比我使用它快5倍std::copy.

我尝试使用MSVC 2015编译此代码,启用和禁用优化并获得相同的结果.

看起来有点奇怪std::copystd::back_inserter实现.

c++

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

C++中元组元素类型的大小总和

是否可以使用以下用法constexpr计算std::tuple元素类型的大小总和的函数:

static_assert(sum_size(std::tuple<int, bool>) == 5, "not 5!");
Run Code Online (Sandbox Code Playgroud)

不直接回答我的问题,因为DoSomething不是constexpr功能.我需要在编译时调用DoSomething.或者也许有人能解释如何使用boost::fusion::for_eachstatic_assert()

c++ templates template-meta-programming variadic-templates c++14

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

为什么r值变成l值?

由于一般问题“如何std::forward工作”太复杂,我决定从一个特定示例的问题开始:

#include <utility>
#include <iostream>

namespace
{
    void foo(const int&)
    {
        std::cout << "const l-value" << std::endl;
    }

    void foo(int&)
    {
        std::cout << "l-value" << std::endl;
    }

    void foo(int&&)
    {
        std::cout << "r-value" << std::endl;
    }

    template <typename T>
    void deduce(T&& x)
    {
        //foo(std::forward<T>(x));
        foo(x);
    }
}

int main()
{
    deduce(1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

l-value
Run Code Online (Sandbox Code Playgroud)

为什么?

我们传递 r 值,并且 的参数deduce是 r 值,对吧?为什么foo(int&)叫?

当我直接打电话时

foo(1);
Run Code Online (Sandbox Code Playgroud)

它打印r-value。里面什么时候变成l值了deduce

编辑1

如果我 …

c++

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

MySQL选择查询非常慢

我有一个带有2196998条记录的表:

CREATE TABLE price (
    dt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    marketId INT,
    buy DOUBLE,
    sell DOUBLE,
    PRIMARY KEY (dt, marketId),
    FOREIGN KEY fk_price_market(marketId) REFERENCES market(id) ON UPDATE CASCADE ON DELETE CASCADE
)  ENGINE=INNODB;
Run Code Online (Sandbox Code Playgroud)

查询

select max(buy) from price;
Run Code Online (Sandbox Code Playgroud)

需要1.92秒,这是一个合理的时间,如果我在“购买”列上创建索引,则需要0.00秒:

CREATE INDEX idx_price_buy ON price (buy);
Run Code Online (Sandbox Code Playgroud)

和查询

select count(*) from price where marketId=309;
Run Code Online (Sandbox Code Playgroud)

花费0.05秒并返回160570。

但是查询

select max(buy) from price where marketId=309;
Run Code Online (Sandbox Code Playgroud)

即使我创建了两个实例,也要花费15.49秒(这是非常大的):

CREATE INDEX idx_price_market ON price (marketId);
CREATE INDEX idx_price_buy ON price (buy);
Run Code Online (Sandbox Code Playgroud)

(我不确定,但是可能索引idx_price_market已经存在,因为marketId外键约束中需要列)

1)有没有优化的方法? …

mysql sql database

0
推荐指数
1
解决办法
53
查看次数

作为模板参数的模板函数

如何使下面的伪代码编译?

#include <vector>

template <class T>
void CopyVector() { std::vector<T> v; /*...*/}

template <class T>
void CopyVectorAsync() { std::vector<T> v; /*...*/}

template <template <class> void copy()>
void Test()
{
    copy<char>();
    copy<short>();
    copy<int>();
}

int main()
{
    Test<CopyVector>();
    Test<CopyVectorAsync>();
}
Run Code Online (Sandbox Code Playgroud)

CopyVectorCopyVectorAsync是使用不同算法复制某些类型元素向量的函数TTest函数调用具有不同元素类型的给定复制函数。main函数调用CopyVectorCopyVectorAsync所有元素类型。

c++ c++20

0
推荐指数
1
解决办法
135
查看次数

为什么班级不可分配?

为什么func_getter下面提供的类不可分配?

#include <type_traits>
#include <string>

template <class T, class ReturnType>
using FuncPtr = ReturnType(T::*)() const;

template <class T, class ReturnType>
class func_getter
{
public:

    using MyFuncPtr = FuncPtr<T, ReturnType>;

    constexpr func_getter(MyFuncPtr p) : m_p(p) {}

    func_getter(const func_getter&) = default;
    func_getter(func_getter&&) = default;
    func_getter& operator = (const func_getter&) = default;
    func_getter& operator = (func_getter&&) = default;

    constexpr ReturnType operator() (const T& val) const
    {
        return (val.*m_p)();
    }

private:

    MyFuncPtr m_p;
};

struct X
{
    int a;
    std::string b;

    int …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
84
查看次数

如何判断重载函数的返回类型?

在下面的代码中,如何f在不实例化Aand的情况下确定两个重载的返回类型B

class A
{
public:

    A(std::string k) : key(k)
    {
    }

private:

    std::string key;
};

class B
{
public:

    B(int k) : key(k)
    {
    }

private:

    int key;
};

int f(const A&);
std::string f(const B&);

// It is possible to determine the return types as follows:
using a_return_type = decltype(f(A{ "" }));
using b_return_type = decltype(f(B{0}));
Run Code Online (Sandbox Code Playgroud)

伪代码解决方案:

using a_return_type = std::invoke_result_t<f, A>;
using b_return_type = std::invoke_result_t<f, B>;
Run Code Online (Sandbox Code Playgroud)

编辑1

我需要一个没有A{ "" }和的解决方案B{0} …

c++

0
推荐指数
1
解决办法
103
查看次数

如何在C++中的32位平台上计算64位哈希?

据我所知,std :: hash计算类型为size_t的散列,但我需要计算uint64_t类型的散列,无论应用程序是32位还是64位,将其写入文件并在另一个应用程序中读取,例如.

是否可以使用标准C++库?

为了使问题更清楚:

std::hash<std::string> h1;
std::hash<std::vector<bool>> h2;
std::hash<int> h3;
Run Code Online (Sandbox Code Playgroud)

所有h1(),h2(),h3()的类型都是size_t,但我需要uint64_t.

c++

-2
推荐指数
1
解决办法
105
查看次数