小编Pat*_*ryk的帖子

使用节点将字符串解析为JSON会产生意外的令牌,验证器说好

我有以下字符串,我想解析为JSON:

{
    "STATUS": [
        {
            "STATUS": "S",
            "When": 1394044643,
            "Code": 17,
            "Msg": "GPU0",
            "Description": "cgminer 3.7.3"
        }
    ],
    "GPU": [
        {
            "GPU": 0,
            "Enabled": "Y",
            "Status": "Alive",
            "Temperature": 70,
            "Fan Speed": 3089,
            "Fan Percent": 70,
            "GPU Clock": 1180,
            "Memory Clock": 1500,
            "GPU Voltage": 1.206,
            "GPU Activity": 99,
            "Powertune": 20,
            "MHS av": 0.4999,
            "MHS 5s": 0.5009,
            "Accepted": 4335,
            "Rejected": 7,
            "Hardware Errors": 0,
            "Utility": 27.8007,
            "Intensity": "0",
            "Last Share Pool": 0,
            "Last Share Time": 1394044643,
            "Total MH": 4676.7258,
            "Diff1 Work": 69436, …
Run Code Online (Sandbox Code Playgroud)

javascript parsing json node.js

6
推荐指数
2
解决办法
2万
查看次数

读取直方图后的未处理异常(使用calcHist创建)

我想在OpenCV中从我的颜色(3通道)图像中获取直方图,但每次我都像这样做calcHist直方图:

//int histSize[3];
//float hranges[2];
//const float* ranges[3];
//int channels[3];

ColorHistogram::ColorHistogram() 
{
    // Prepare arguments for a color histogram
    histSize[0]= histSize[1]= histSize[2]= 256;
    hranges[0]= 0.0; // BRG range
    hranges[1]= 255.0;
    ranges[0]= hranges; // all channels have the same range
    ranges[1]= hranges;
    ranges[2]= hranges;
    channels[0]= 0; // the three channels
    channels[1]= 1;
    channels[2]= 2;
}

cv::MatND ColorHistogram::getHistogram(const cv::Mat &image)
{
    cv::MatND hist;
    // Compute histogram
    cv::calcHist(&image,
        1, // histogram of 1 image only
        channels, // the channel used
        cv::Mat(), // no …
Run Code Online (Sandbox Code Playgroud)

c++ opencv histogram unhandled-exception

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

如何确定多个元素的边界矩形?

我想实现一种算法,该算法将找到轮廓的边界矩形(已由另一种算法确定)。我唯一拥有的是二值化图像(如下所示)。基本想法是:

  • 采取这样的东西 - 预处理的二值化图像 在此输入图像描述

  • 并产生这样的东西 在此输入图像描述

algorithm graphics geometry bounding-box

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

获取模板,模板类型

我正在创建一个小的"通用"路径查找类,它采用一种类型Board,它将在其上找到路径,

//T - Board class type
template<class T>
class PathFinder
{...}
Run Code Online (Sandbox Code Playgroud)

而且Board还模仿保存节点类型.(这样我就可以在2D或3D矢量空间上找到路径).

我希望能够声明和定义一个成员函数PathFinder,它将采用这样的参数

//T - Board class type
PathFinder<T>::getPath( nodeType from, nodeType to);
Run Code Online (Sandbox Code Playgroud)

如何为节点类型执行类型兼容性T并将nodeType其作为参数提供给函数?

c++ templates types class

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

将 std::random_device 与 pRNG(例如 std::mt19937)一起使用和不使用之间有什么区别?

在 C++11 中,可以使用std::random_device或不使用像 mt19937 这样的伪随机数生成器来生成数字。

在此示例代码中使用它会有什么区别:

#include <random>
#include <iostream>

int main() {
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_real_distribution<double> dist(1, 10);

    for (int i=0; i<16; ++i)
        std::cout << dist(rd) << "\t" << dist(mt) << "\n";
}
Run Code Online (Sandbox Code Playgroud)

c++ random prng c++11

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

如何实现自定义std :: streambuf的seekoff()?

我有以下基于此问题和答案的实现

struct membuf : std::streambuf
{
  membuf(char* begin, char* end)
  {
    this->setg(begin, begin, end);
  }

protected:
  virtual pos_type seekoff(off_type off,
                           std::ios_base::seekdir dir,
                           std::ios_base::openmode which = std::ios_base::in)
  {
    std::istream::pos_type ret;
    if(dir == std::ios_base::cur)
    {
      this->gbump(off);
    }
    // something is missing here...
  }
};
Run Code Online (Sandbox Code Playgroud)

我想通过以下方式在我的方法中使用它:

  char buffer[] = { 0x01, 0x0a };
  membuf sbuf(buffer, buffer + sizeof(buffer));
  std::istream in(&sbuf);
Run Code Online (Sandbox Code Playgroud)

然后调用,例如tellg()in并获得正确的结果。

到目前为止,它几乎是完美的-它不会在流的尽头停止。

我应该如何升级才能使其正常工作?

我的主要动机是模仿std::ifstream行为,但char[]在测试中将二进制文件输入到行为中(而不是依赖二进制文件)。

c++ istream seek streambuf

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

成员引用基类型'double'不是结构或联合

我是C++的新手,无法弄清楚如何修复错误,非常感谢您的帮助发生错误的部分我正在尝试将半径输入到cirArea[]数组中,但它似乎不起作用.

这是我的代码的一部分:

int main(){
    Circle *area;
    double cirRadius;
    int numCircle;

    cout << "How many circles?" << endl;
    cin >> numCircle;
    double cirArea[numCircle];

    for (int i = 0; i < numCircle; i++){
        cout << "Enter the radius: ";
        cin >> cirRadius;
        cirArea[i].setRadius(cirRadius);
    }
}
Run Code Online (Sandbox Code Playgroud)

对于setRadius():

void Circle::setRadius(double r){
    if (r >= 0)
        radius = r;
    else {
        cout << "Invalid radius\n";
        exit(EXIT_FAILURE);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以这是错误:

member reference base type 'double' is not a structure or union
            cirArea[i].setRadius(cirRadius);
            ~~~~~~~~~~^~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

c++

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

如何从参数包中获取所有参数的类型?

我有以下代码片段,我struct quick用模板化static方法定义random了一些特殊化:

(我使用function_traits其他SO答案.附在底部供参考.)

struct quick
{
  template <typename T>
  static T random();

  template <typename F>
  static void check(F f)
  {

    constexpr auto arity = function_traits<F>::arity; // easy :)
    std::cout << arity << std::endl;
    typedef typename function_traits<F>::template arg<0>::type type0; // easy:)
    // how to get all types of all F's parameters?
  }
};

template <>
std::string quick::random<std::string>()
{
  return std::string("test");
}

template <>
int quick::random<int>()
{
  return 1;
}
Run Code Online (Sandbox Code Playgroud)

我想获取所有类型F的参数,check …

c++ templates type-traits c++11 c++14

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

如何教谷歌测试从 std 打印类型?

我有一个需要打印出来的测试std::pair<std::string, std::string>,但即使我为该 googletest 声明并定义了一个运算符,它也会抱怨它找不到它。

\n

谷歌测试手册提到了这一点

\n
// It\'s important that the << operator is defined in the SAME\n// namespace that defines Bar.  C++\'s look-up rules rely on that.\n
Run Code Online (Sandbox Code Playgroud)\n

我应该重新打开std命名空间并将其放置operator<<(std::ostream& os, const std::pair<std::string, std::string>& p)在那里吗?

\n

代码摘录:

\n
// included from somewhere else in legacy code:\n// typedef ::std::map< ::std::string, ::std::string > AttrList;\nbool UserDefinedTypeMatcher::MatchAndExplain(UserDefinedType& r, testing::MatchResultListener* listener) const {\n  typedef AttrList::const_iterator AttrIt;\n  std::pair<AttrIt, AttrIt> pattr = std::mismatch(r.attr.begin(), r.attr.end(), expectedUserDefinedType.attr.begin());\n  if(pattr.first != r.attr.end() && …
Run Code Online (Sandbox Code Playgroud)

c++ googletest googlemock c++03

5
推荐指数
0
解决办法
968
查看次数

是否可以将std :: condition_variable与std :: lock_guard一起使用?

我正在使用这样的std::condition_variable结合std::unique_lock

std::mutex a_mutex;
std::condition_variable a_condition_variable;
std::unique_lock<std::mutex> a_lock(a_mutex);
a_condition_variable.wait(a_lock, [this] {return something;});
//Do something
a_lock.unlock();
Run Code Online (Sandbox Code Playgroud)

工作正常。据我了解,std::condition_variable接受std::unique_lock它等待。但是,我正在尝试将其与std::lock_guard但不能结合在一起。

我的问题是: 可以std::unique_lock用a std::lock_guard代替吗?这样可以避免我每次使用锁时都手动解锁。

c++ mutex condition-variable c++11

5
推荐指数
3
解决办法
1825
查看次数