我正在尝试为我的程序实现一个循环缓冲区。缓冲区用于在两个线程之间共享数据,如下所示。我使用 OpenCV 从相机(线程 1)抓取视频帧。然后我想将这些数据存储在一个循环缓冲区中,以便线程2可以从缓冲区中获取数据。

如何cv::Mat在 C++ 中为对象实现循环缓冲区?我知道如何为标准 C++ 对象(如int或char)创建循环缓冲区,但我无法使其与类型的对象一起使用cv::Mat。
有什么建议?
它曾经工作过:从std::cin动态分配的char数组读取- 或从作为参数传入的数组(参见下面的 MCVE)。
#include <iostream>
void read (char str[256]) //or omit the # between the []'s -- no difference
{
std::cin >> str; //error
}
int main ()
{
char s1 [256];
char* s2 = new char[256];
char s3 [256];
std::cin >> s1; //OK
std::cin >> s2; //error
read (s3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我相信问题是一个名为Fixingoperator>>(basic_istream&, CharT*)的 C++20 更新。它的目的是防止std::cin它正在读入的字符串溢出,并且 cppreference.com 确实列出了's 的新原型,这些原型现在具有,而不是作为所采用的参数。std::istreamoperator>>CharT (&s)[N]CharT* …
考虑以下代码片段:
#include <cstdint>
#include <limits>
#include <iostream>
int main(void)
{
uint64_t a = UINT32_MAX;
std::cout << "a: " << a << std::endl;
++a;
std::cout << "a: " << a << std::endl;
uint64_t b = (UINT32_MAX) + 1;
std::cout << "b: " << b << std::endl;
uint64_t c = std::numeric_limits<uint32_t>::max();
std::cout << "c: " << c << std::endl;
uint64_t d = std::numeric_limits<uint32_t>::max() + 1;
std::cout << "d: " << d << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
#include <cstdint>
#include <limits>
#include …Run Code Online (Sandbox Code Playgroud) 我需要在一个文件中存储一个结构并将其读回来然后返回它.我会尝试将其写入文件,如下所示:
void lld_tpWriteCalibration(struct cal cal) {
FIL fdst; /* file objects */
UINT bw; /* File write count */
/* Create destination file on the drive 0 */
wf_open(&fdst, "0:calibration.txt", FA_CREATE_ALWAYS | FA_WRITE);
wf_write(&fdst, cal, sizeof(cal), &bw);
wf_close(&fdst);
}
Run Code Online (Sandbox Code Playgroud)
那会有用吗?我该怎么读回来并从这个函数返回呢?
struct cal lld_tpReadCalibration(void) {
}
Run Code Online (Sandbox Code Playgroud)
结构是:
struct cal {
float xm;
float ym;
float xn;
float yn;
};
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
给定一个类型的行主表std::vector<std::vector<T>>(其中T是不太可比的类型,如intor std::string),我想按特定列对表进行排序,同时保留行内容(即一行只能作为一个整体移动,而不是单个细胞)。
例如,给定此表:
2 8 1 4
3 7 6 7
3 3 4 9
8 6 3 4
7 1 5 7
Run Code Online (Sandbox Code Playgroud)
按第三列(索引 2)排序,所需的结果将是:
2 8 1 4
8 6 3 4
3 3 4 9
7 1 5 7
3 7 6 7
Run Code Online (Sandbox Code Playgroud)
STL 实现这一目标的方法是什么?
我能想到的一种解决方案是将应排序的列复制到关联容器中(例如,std::unordered_map<T, std::size_t>键是单元格值,值是行索引),然后按键对映射进行排序(使用std::sort()),提取结果行索引顺序并使用它对原始表中的行重新排序。
然而,当将其编写为实际代码时,该解决方案似乎不优雅且相当冗长。
有哪些可能的、“好的”解决方案来实现这一点?
注意:表类型std::vector<std::vector<T>>是给定的,不能更改/修改。
这是一个需要 an 的构造函数std::initializer_list,我想将它分配给一个向量。我是否需要使用for-loop 将 中的每一项std::initializer_list逐一分配给向量?
Motor_Group::Motor_Group(std::initializer_list<pros::Motor> port_set)
{
this->motor_vector = port_set;
}
Run Code Online (Sandbox Code Playgroud) 我使用的是 Qt 5.15.2,我有以下代码:
QByteArray arybytData = mpsckIncoming->readAll();
bool blnHTTP = false;
if ( arybytData.startsWith("GET / HTTP/") == true ) {
//HTTP Request, probably from browser, jump to end of header
const QString cstrHeaderTerminator("\r\n\r\n");
int intHdrEnd;
if ( (intHdrEnd = arybytData.indexOf(cstrHeaderTerminator)) == -1 ) {
Run Code Online (Sandbox Code Playgroud)
使用的行QByteArray::indexOf()包含以下文本:
'indexOf' is deprecated: Use QString's toUtf8(), toLatin1() or toLocal8Bit()
Run Code Online (Sandbox Code Playgroud)
我不明白这一点,因为建议的函数都不像 的函数QByteArrray::indexOf()。
我应该忽略它吗?
我在用着:
Qt Creator 4.14.0
Based on Qt 5.15.2 (Clang 11.0 (Apple), 64 bit)
Built on Dec 17 2020 07:57:30 …Run Code Online (Sandbox Code Playgroud) 给定一个这样的类模板:
template<typename T>
struct foo
{
T data;
};
Run Code Online (Sandbox Code Playgroud)
如何确定是否T是智能指针,例如std::shared_ptr<T_underlying>使用 C++20 概念?
我想foo<T>根据这个标准添加功能。例如,我想使用新概念系统,而不是使用 SFINAE。
我想实现这样的目标:
template<typename T>
struct foo
{
T data;
void func()
requires is_shared_ptr_v<T>
{
// ...
}
};
Run Code Online (Sandbox Code Playgroud)
STL 中是否存在相关概念?如果没有,我假设我可以写一个概念 for std::shared_ptr, one forstd::unique_ptr等等,然后将它们与逻辑或一般is_smart_pointer概念联系在一起?
请帮助查找我的自定义共享指针类中的错误SharedPtr<T>。我找不到它了。但是代码可以处理泄漏。
我认为是重置方法或operator=方法中的错误。可能无需调用这些方法代码即可工作。我认为是因为 valgrind 没有显示任何泄漏和错误。但是调用这些方法会显示错误,有时会泄漏
#include <iostream>
#include <algorithm>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
template <typename T>
struct SharedPtr
{
explicit SharedPtr(T *ptr = 0) : p_(ptr), c_(0)
{
if (p_) c_ = new size_t(1);
}
~SharedPtr()
{
if (c_ && --*c_ == 0)
{
delete p_;
delete c_;
}
}
SharedPtr(const SharedPtr &p) : p_(p.get()), c_(p.getc())
{
if (c_) { ++*c_; }
}
SharedPtr& operator=(const SharedPtr &p)
{
p_ = p.p_;
c_ = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来实现以下目标:客户端类foo继承自命名的类properties或将其用作类成员。客户端类foo也可能有零个或多个 type 成员property。每个property成员都应该使用properties客户端类的实例自行注册:
struct foo :
properties
{
property<int> x;
property<int> y;
};
Run Code Online (Sandbox Code Playgroud)
这个想法是允许客户端类指定任意数量的属性,这些属性会自动“收集”以进行进一步处理,例如序列化,以便客户端类的作者不需要手动枚举/迭代每个属性。
我可以使用哪些技术来实现这一目标?我知道在静态工厂中注册自己的自注册类的模式,但是如何在同一客户端类中的特定实例上自动实现自注册?这甚至可能吗?鉴于有Q_PROPERTY类似的事情,应该有办法做到这一点?
任何高达 C++20(至少目前在 GCC 10.2 中可用的)都可以。
这个问题是对我之前的问题的一个更具体的问题。
我尝试动态分配二维数组的一个维度.该数组声明如下:
uint16_t coord[][2];
Run Code Online (Sandbox Code Playgroud)
我只需要分配行数,坐标数.通过谷歌,我发现足够的代码来分配这两个维度,从:
uint16_t **coord;
Run Code Online (Sandbox Code Playgroud)
我不确定我是否仍然可以如上所述声明数组.我需要这样做:
uint16_t *coord[2];
Run Code Online (Sandbox Code Playgroud)
或不?
我还需要从分配函数返回数组(指向它的指针),以便其他函数可以像这样访问数组:
foo = coord[4][0];
bar = coord[4][1];
Run Code Online (Sandbox Code Playgroud)
返回分配的数组的正确方法是什么?
给定以下C ++程序:
#include "counterType.h"
#include <iostream>
#include <string>
using namespace std;
counterType::counterType()
{
counter = 0;
}
counterType::counterType(int c)
{
setCounter(c);
}
void counterType::setCounter(int ck)
{
counter = ck;
}
int counterType::getCounter()
{
return counter;
}
void counterType::incrementCounter()
{
++counter;
}
void counterType::decrementCounter()
{
--counter;
}
void counterType::print()
{
cout << "Counter = "<< counter << endl;
}
Run Code Online (Sandbox Code Playgroud)
该代码似乎仅在setCounter()中包含参数时才起作用。唯一失败的测试是when是无参数的。那么,如何以某种方式检查它,如果没有参数,则计数器将为0?
c++ ×10
c++20 ×3
c ×2
arrays ×1
buffer ×1
c++-concepts ×1
file ×1
opencv ×1
qbytearray ×1
qt ×1
restore ×1
shared-ptr ×1
sorting ×1
stdvector ×1
struct ×1