小编Jam*_*nus的帖子

无符号数据类型的 MSVC ifstream 性能问题

std::ifstream在读取二进制文件时,我在 MSVC 上做了一些测试。我在charunsigned char数据类型之间有很大的性能差异。

读取 512 MB 二进制文件时的结果:

Duration read as signed: 322 ms
Duration read as unsigned: 10552 ms
Run Code Online (Sandbox Code Playgroud)

在我用来测试的代码下面:

Duration read as signed: 322 ms
Duration read as unsigned: 10552 ms
Run Code Online (Sandbox Code Playgroud)

我不明白使用 abasic_ifstream<unsigned char>basic_ifstream<char>读取二进制文件时慢 30 倍。

c++ ifstream visual-c++

5
推荐指数
2
解决办法
74
查看次数

在获得所有权之前进行std :: unique_ptr测试

我有一个类,它基本上是一个队列,用于在2个线程之间传输动态分配的对象.第一个线程创建对象,第二个线程使用它们.我std::unique_ptr用来将对象所有权从线程1传递给线程2.

实际上调用将对象放入队列的方法是这样的:

queue.put(std::move(unique_ptr_to_my_object));
Run Code Online (Sandbox Code Playgroud)

和签名:

bool Queue::put(std::unique_ptr<T> p);
Run Code Online (Sandbox Code Playgroud)

问题是该put()方法必须检查某些条件以确定是否可以将对象添加到队列中.如果条件为false,则该方法只返回false以指示它无法将对象添加到队列中,但该对象已被销毁,因为已经占用了所有权put().

所以我想知道是否可以put()像这样重写或者是否有更好的解决方案:

bool Queue::put(std::unique_ptr<T> &ref) {
    if(CANNOT_ADD)
        return false; // ownership remains in the calling function
    std::unique_ptr<T> p = std::move(ref); // we know we can add so take ownership
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr move-semantics

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

switch语句和对象隐式int转换

在C ++中,直接在具有隐式转换为int的对象上使用switch语句合法/正确吗?而不是使用返回对象标记的方法。

class Action
{
  public:
    enum EType { action1, action2, action3};
    operator int() const { return mType; }
  private:
    EType mType;
  /* ... */
}

int main()
{
    Action a = /* ... */
    switch(a)
    {
    case Action::EType::action1:
        /* ... */
        break;
    case Action::EType::action2:
    /* ... */
    }
}
Run Code Online (Sandbox Code Playgroud)

c++

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

从std :: unique_ptr数组中获取原始指针数组

我有一个库函数,需要一个四个指针的数组作为参数:f(unsigned char* data[4]),我的代码中有一个智能指针数组:std::unique_ptr<unsigned char> myArray[4].

有没有办法使用智能指针与此库函数?

我已经尝试了这个,但它给了我一个段错误: f(reinterpret_cast<unsigned char **>(myArray[0].get()));

c++ arrays smart-pointers

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

使用 std::remove_if() 时没有可行的重载 '='

我正在尝试编写一个随时间滑动的滑动窗口,并将最旧的数据作为模板类删除。我使用 std::map 作为容器和 std::chrono 来操纵时间。我在负责删除早于 X 毫秒的数据的方法中有一个编译时错误:

include/c++/5.4.0/bits/stl_pair.h:170:8: error: no viable overloaded '='
    first = std::forward<first_type>(__p.first);
Run Code Online (Sandbox Code Playgroud)

我不明白为什么std::chrono::steady_clock::time_point没有可行的“运算符 =”。我使用 clang++ 编译器。

这是模板代码:

#include <algorithm>
#include <chrono>
#include <map>
#include <utility>

template <class Data, class Clock, class Time = typename Clock::time_point>

class TimeSlideWindow {
private:
    std::map<Time, Data> mData;

public:    
    void insert(Data value)
    {
        mData.insert(std::make_pair(Clock::now(), value));
    }

    void clearOlderThan(std::chrono::milliseconds ms)
    {
        Time now = Clock::now();
        remove_if(mData.begin(),
                  mData.end(),
                  [ms, now](const std::pair<Time, Data> &elem) {
                      return elem.first < (now - ms);
                  });
    } …
Run Code Online (Sandbox Code Playgroud)

c++ templates compiler-errors

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