QTextEdit 无法在 insertHtml 之后插入PlainText。QTextEdit 插入 html,然后插入纯文本。纯文本仍以 html 格式显示。这是一个错误吗?或者我使用了错误的方法。
我想先添加html,然后添加纯文本,它们就能正常显示,我该怎么办。
QTextEdit 无法在 insertHtml 之后插入PlainText。QTextEdit 插入 html,然后插入纯文本。纯文本仍以 html 格式显示。这是一个错误吗?或者我使用了错误的方法。
我想先添加html,然后添加纯文本,它们就能正常显示,我该怎么办。
使用库的 C++ 代码fmt
按预期运行:
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <vector>
int main() {
std::vector<int> v{4, 2, 7};
fmt::println("{}", v);
}
我尝试将标准print
头与 g++-14 一起使用:
#include <print>
#include <vector>
void test() {
std::vector<int> v{4, 2, 7};
std::println("{}", v);
}
无法使用巨大的模板进行编译。有办法解决吗?
我正在尝试为井字游戏制作一个棋盘对象,但无法解决此错误。
class Board {
public:
void reset() {
board[3][3] = {
{" ", " ", " "},
{" ", " ", " "},
{" ", " ", " "}
};
};
private:
std::string board[3][3];
Board() {
reset();
};
};
我尝试将变量类型从 std::string 切换为 char 但它引发了一个不同的错误,这也没有意义
我有一个宏,它创建一个类的实例Info
,该类接受具有模板化大小的数组作为构造函数参数。尺寸信息用于检查。
我想在返回实例之前检查这个宏。支票可能类似于static_assert
,但我们将其保留在此处。执行检查的一种优雅方法是让宏调用执行检查的 lambda,然后返回实例。
但这会失败,因为一个非常烦人的原因:如果我将 char 数组传递给auto
lambda 的参数,它们将被推导为char
指针,这会阻止调用Info
构造函数。所以我想将具有模板化大小的 char 数组传递给 lambda。但这只有从 C++20 开始才有可能。
有什么解决方法吗?我可以使用 C++ 17 让 lambda 接受模板化大小的数组吗?
代码:
#include <cstdlib>
#include <iostream>
class Info
{
public:
template <size_t function_length, size_t file_length>
Info(const char (&function)[function_length], const char (&file)[file_length])
: function(function)
, file(file)
{
if ('\0' != function[function_length - 1])
{
this->function = "invalid";
}
if ('\0' != file[file_length - 1])
{
this->file = "invalid";
}
}
const char* function;
const char* file;
};
#define CREATE_INFO() \
( \
[]<std::size_t function_length, std::size_t file_length>( \
const auto(&function)[function_length], const auto(&file)[file_length]) \
{ \
static constexpr char error_string[] = "invalid"; \
\
if ('m' != function[0]) \
{ \
return Info(error_string, error_string); \
} \
return Info(function, file); \
}(__FUNCTION__, __FILE__))
int main()
{
Info info = CREATE_INFO();
std::cout << "Function: " << info.function << ", file: " << info.file << std::endl;
return 0;
}
编译器资源管理器中的示例:https://godbolt.org/z/8q5zYKj4a
我正在尝试使用 aboost::pool_allocator
来(有效地)分配std::shared_ptr<T>
。从概念上讲,我想要这样的东西:
struct Foo {};
std::shared_ptr<Foo> create() {
static boost::pool_alloator<Foo> alloc;
return std::allocate_shared<Foo>(alloc);
}
这个例子有一个问题:boost::pool_allocator<Foo>
针对大小分配进行了优化sizeof(Foo)
(事实上,我很惊讶它完全支持不同大小的分配......)。但std::allocate_shared<Foo>(…)
不分配字节,因为它一次性sizeof(Foo)
分配控制块和有效负载。
为了有效地使用boost::pool_allocator<…>
with std::allocate_shared
,我需要告知pool_allocator
预期分配的大小。为此,我可能需要allocate_shared
内部分配的类型。我非常确定,对于 GCC 的 STL 实现,该类型将是_Sp_counted_ptr_inplace,而对于 Clang 的 libc++ 来说,它看起来是__shared_ptr_emplace::_Storage - 但使用这些类型显然是不可移植的。
是否有任何符合标准的方法来计算将执行的分配大小std::allocate_shared
?或者任何其他方式(有效地)使用池分配器allocate_shared
?
当我输入“12345”和“0”的用户输入时,我看到在第一个循环中 arrOne 正在按照我期望的方式填充 1, 2, 3, 4, 5。但是在第二个循环之后(填充 arrTwo )当我去打印 arrOne 的值(使用第三个循环)时,我看到这些值现在是 0, 2, 3, 4, 5。
我对在初始循环后如何修改 arrOne 的初始值感到摸不着头脑。感谢任何指导!
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// Write your main here
string inputOne = "";
string inputTwo = "";
cout << "Please enter two positive integers up to 20 digits long" << endl;
cin >> inputOne;
cin >> inputTwo;
int arrOne[inputOne.length() - 1];
int arrTwo[inputTwo.length() - 1];
for(int i = 0; i< inputOne.length(); i++){
arrOne[i] = inputOne[i] - '0';
cout<< "arrOne[i] = " << arrOne[i] << endl;
}
for(int i = 0; i< inputTwo.length(); i++){
arrTwo[i] = inputTwo[i] - '0';
cout<< "arrTwo[i] = " << arrTwo[i] << endl;
}
for(int i = 0; i< inputOne.length(); i++){
cout<< "arrOne[i] = " << arrOne[i] << endl;
}
return 0;
}
我期望在填充 arrTwo 数组后,arrOne 数组将继续存储 1、2、3、4、5。
我正在尝试将Args
参数从模板传递到容器deque.emplace_back()
调用
例如
class Collection
{
public:
template <typename ...Args>
Collection(const std::string& name, Args... entries)
: m_name(name)
{
m_entries.emplace_back(entries...); // Here!!!! Entry<int>, Entry<double>, Entry<int, int> added to deque
}
private:
std::string m_name;
std::deque<std::any> m_entries;
};
Collection collection1
{
"my collection",
Entry<int>{4},
Entry<double>{5.5},
Entry2<int, int>{1, 2}
};
emplace_back
是带有Args
参数重载的模板
template< class... Args >
reference emplace_back( Args&&... args );
所以我认为它可能需要一个对象列表。但这还没有编译。有错误
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::any; _Args = {Entry<int>&, Entry<double>&, Entry2<int, int>&}; _Tp = std::any]':
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/alloc_traits.h:537:17: required from 'static void std::allocator_traits<std::allocator<_Tp1> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::any; _Args = {Entry<int>&, Entry<double>&, Entry2<int, int>&}; _Tp = std::any; allocator_type = std::allocator<std::any>]'
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/deque.tcc:170:30: required from 'std::deque<_Tp, _Alloc>::reference std::deque<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {Entry<int>&, Entry<double>&, Entry2<int, int>&}; _Tp = std::any; _Alloc = std::allocator<std::any>; reference = std::any&]'
<source>:44:31: required from 'Collection::Collection(const std::string&, Args ...) [with Args = {Entry<int>, Entry<double>, Entry2<int, int>}; std::string = std::__cxx11::basic_string<char>]'
<source>:58:1: required from here
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/new_allocator.h:187:11: error: no matching function for call to 'std::any::any(Entry<int>&, Entry<double>&, Entry2<int, int>&)'
187 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在以下情况下将POINT
参数转换为需要或不需要:LPPOINT
POINT point;
ClientToScreen(somehwnd, (LPPOINT) &point);
或者以下内容就足够了:
POINT point;
ClientToScreen(somehwnd, &point);
此 Win32 文档使用强制转换,而此 Win32 文档不使用强制转换。
我正在尝试使用 opencl 将图像转换为灰度。但是当我尝试创建读缓冲区时。即使缓冲区大小完全在设备的内存限制之内,我也会遇到分段错误。我尝试了宽度 = 4000 和高度 = 6000 这是我的 opencl 函数。段错误出现在这一行
cl::Buffer input_buffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(uchar) * width * height * 3, input, &err);
void executeOpenCL(uchar*** input, uchar** output, const int height, const int width)
{
cl_int err;
std::vector<cl::Platform> platforms;
err = cl::Platform::get(&platforms);
cl::Platform platform = platforms[0]; // Choose the first platform
std::vector<cl::Device> devices;
err = platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
cl::Device device = devices[0]; // Choose the first GPU device
cl::Context context(device);
cl::CommandQueue queue(context, device);
// Create OpenCL buffers for input and output image data
cl::Buffer input_buffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(uchar) * width * height * 3, input, &err);
return;
}
我尝试检查缓冲区大小是否大于主机设备的内存,但即使在限制范围内。
输入定义为无符号 char3d 数组
uchar*** array3D = new uchar** [height];
for (int i = 0; i < height; ++i) {
array3D[i] = new uchar* [width];
for (int j = 0; j < width; ++j) {
array3D[i][j] = new uchar[3]; // Assuming 3 channels (RGB)
}
}
我们有一些共享资源:内存池、线程不安全的 API,任您选择。我们希望通过 ASIO 链控制对所述资源的访问。所有访问资源的例程都应该在该链上运行。
我们还使用 C++20 协程并享受它们提供的顺序执行的错觉。
当访问共享资源时,我们希望使用 挂起协程co_await
,切换到受祝福的链,对资源执行任何操作,然后返回到其本机执行器上的协程。
注意事项:
dispatch
技巧”,因为人体工程学很糟糕,而且这是一个等待发生的竞争条件。auto s1 = bind_executor(strand, asio::deferred);
co_await asio::dispatch(s1);
// Access shared resource
co_await asio::dispatch(asio::deferred);
asio::awaitable
,并可能为不需要的操作分配一个协程框架(通过use_awaitable
)这就是我今天早上破解的内容,显然它不是很好(不使用概念,不转发参数,不允许返回值等),但它说明了我的目的:
(神箭)
static std::atomic_int tid_gen;
thread_local int const tid = tid_gen++;
inline void out(auto const& msg) { std::print("T{:x} {}\n", tid, msg); }
template <typename F, typename Ex, typename CT>
auto async_run_on(F f, Ex ex, CT&& token) {
return asio::async_initiate<CT, void()>(
[](auto handler, F f, Ex ex) {
ex.dispatch(
[=, handler = std::move(handler)]() mutable {
std::invoke(f);
handler.get_executor().execute(std::move(handler));
},
asio::get_associated_allocator(ex));
},
token, std::move(f), ex);
}
asio::awaitable<void> f(auto strand) {
out("Main");
co_await async_run_on([](){ out("Strand"); }, strand, asio::deferred);
out("Main again");
}
int main() {
asio::io_context io;
asio::thread_pool tp(1);
co_spawn(io, f(make_strand(tp)), asio::detached);
io.run();
}
希望不言而喻的是,我们可以扩展它来构建始终在给定执行器上运行并返回到任何地方的可调用等待对象。
这个用例应该如何工作?
我在ASIO方面很差,最差的。我是第一个遇到这个问题的可能性为零。这让我对我提出的任何解决方案都非常怀疑。协程和链是 ASIO 构建块,我不是 sehe 或 ChrisK,我不是弄清楚这些拼图如何组合在一起的人。
但谷歌、SO 和 ASIO 文档对此都保持奇怪的沉默。对于链给出的示例和对于协程给出的示例之间几乎没有交叉授粉。
我们是否应该co_await
每次都进行链绑定延迟操作,或者是否有一个我完全错过的自由函数?