库中是否有标准算法可以完成以下 for 循环的工作?
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main( )
{
const char oldFillCharacter { '-' };
std::vector<char> vec( 10, oldFillCharacter ); // construct with 10 chars
// modify some of the elements
vec[1] = 'e';
vec[7] = 'x';
vec[9] = '{';
const char newFillCharacter { '#' };
for ( auto& elem : vec ) // change the fill character of the container
{
if ( elem == oldFillCharacter )
{
elem = newFillCharacter;
}
} …Run Code Online (Sandbox Code Playgroud) 问题很简单:在字符串缓冲区中查找空终止符( '\0' )位置的最快方法是什么?
std::array<char, 100> str { "A sample string literal" };
Run Code Online (Sandbox Code Playgroud)
我想一种选择是使用std::strlen.
我能想到的另一个选择是std::find或者甚至是std::ranges::find:
const auto posOfNull { std::find( str.cbegin( ), str.cend( ), '\0' ) };
Run Code Online (Sandbox Code Playgroud)
ExecutionPolicy现在,如果将(eg std::execution::par) 作为第一个参数传递给它,会有什么不同吗?如果会,那么哪种政策适合这种特殊情况?
或者也许是我不知道的第三个选择?
这个相当简单:根据cppreference 文章,概念可以毫无问题地重新声明:
所以我想好吧,很酷,我不必担心重新声明我创建“可打印枚举”的概念;我可以在日志记录标头和常见类型标头中声明该概念,因此我只需包含所需的内容:
// common types header
#include <enum_traits.hpp>
template<typename E>
concept PrintableEnum = requires(E& e) { {enum_traits<E>::toString(e)} -> std::same_as<const char*>; };
template<PrintableEnum T>
std::ostream& operator<<(std::ostream& os, const T& o)
{
os << enum_traits<T>::toString(o);
return os;
}
// logging header
template<typename T>
concept PrintableEnum = requires(T& e) { {enum_traits<T>::toString(e)} -> std::same_as<const char*>; };
template<PrintableEnum T>
plog::Record& operator<<(plog::Record& rec, const T& o)
{
rec << enum_traits<T>::toString(o);
return rec;
}
Run Code Online (Sandbox Code Playgroud)
(忽略 ostream 过载可能暂时覆盖 plog 记录过载;这可能是我解决该问题的方法,但我首先对此感到好奇。)
因此,当我尝试编译此文件时,MSVC 会为包含两个标头的每个文件抛出一个合适的情况(它指向哪一个,因为错误根据包含顺序而变化):
[build] commontypes.hpp(25): error …Run Code Online (Sandbox Code Playgroud) 如何使下面的伪代码编译?
#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)
CopyVector和CopyVectorAsync是使用不同算法复制某些类型元素向量的函数T。Test函数调用具有不同元素类型的给定复制函数。main函数调用CopyVector和CopyVectorAsync所有元素类型。
我想做的正是标题所说的。我有一些带有 API 的第三方代码。
评估函数所需的信息都应该在编译时可用以评估该函数。但第三方似乎并没有给它贴上constexpr悲伤的标签。此外,我没有完整实现的源代码,只有很多的标头。看起来它可能使用共享对象来评估调用,因为在深入研究 API 的头文件后,很多东西似乎都被标记为外部。由于这些调用并不是constexpr每次我尝试调用它以用于static_assert()编译时都会失败。
如何使该函数在编译时运行并在constexpr函数中使用结果来解决static_assert()条件?所有输入在编译时都是已知的。不需要外部数据。
这可能不是 C++20 特有的,但这就是我现在正在使用的。我有一个简单的结构
\nstruct foo {\n int bar;\n}\nRun Code Online (Sandbox Code Playgroud)\n可以声明并初始化为
\nconst auto baz = foo{42};\nRun Code Online (Sandbox Code Playgroud)\n现在,当我禁用移动构造函数 ( foo(foo&&) = delete;) 时,上述初始化失败并显示
\n\n错误:没有匹配的函数可调用 \xe2\x80\x98foo::foo()\xe2\x80\x99
\n
出现此错误的原因是什么?有没有办法恢复默认行为?
\n我正在尝试使用自定义函数来删除 unique_ptr 。但编译器给了我错误。不知怎的,我怀疑这是因为我的删除器不是类类型。但我发誓它以前可以使用函数,我曾经插入 C 函数就很好了。什么是真正的交易?
#include <cstdio>
#include <memory>
#include <utility>
using cTYPE = struct {
int a;
};
void delete_wrapper(cTYPE* ptr)
{
return free(ptr);
}
cTYPE* new_wrapper()
{
return static_cast<cTYPE*>(malloc(sizeof(cTYPE)));
}
int main()
{
auto foo = []() {
using tmp_storage_type = std::unique_ptr<cTYPE, decltype(delete_wrapper)>;
static tmp_storage_type obj;
obj = tmp_storage_type{ new_wrapper(), delete_wrapper };
};
foo();
}
Run Code Online (Sandbox Code Playgroud)
错误(其中,clang 在这里给出了更好的错误):
<source>:23:33: note: in instantiation of template class 'std::unique_ptr<cTYPE, void (cTYPE *)>' requested here
static tmp_storage_type obj;
^
<source>:24:15: error: no …Run Code Online (Sandbox Code Playgroud) 我刚刚在 ubuntu 20.04 上将编译器升级到 C++20。g++ version给我以下输出:
c++ (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0\nRun Code Online (Sandbox Code Playgroud)\n我正在按照stackoverflow上的建议尝试以下代码
\nconstexpr int f() {\n std::vector<int> v = {1, 2, 3};\n return v.size();\n}\n\nint main() {\n static_assert(f() == 3);\n}\nRun Code Online (Sandbox Code Playgroud)\n但我收到以下错误:
\nerror: variable \xe2\x80\x98v\xe2\x80\x99 of non-literal type \xe2\x80\x98std::vector<int>\xe2\x80\x99 in \xe2\x80\x98constexpr\xe2\x80\x99 function\nRun Code Online (Sandbox Code Playgroud)\n我是不是哪里出错了。或者是我的安装不正确
\n我有一些代码看起来像这样:
double r1 = 0.0, r2 = 0.0;
for (size_t i = 0; i < k; ++i) {
r1 += reduction1(data1[i]);
}
for (size_t i = 0; i < k; ++i) {
r2 += reduction2(data2[i]);
}
Run Code Online (Sandbox Code Playgroud)
这两个归约虽然运行相同次数的迭代,但运行在不同的路径和不同的数据集上。我想知道是否有办法并行运行这两种缩减。
奖励:如果两个循环运行不同次数的迭代怎么办?
编辑:就我而言,k它相当小,大部分工作都是在各个reduction函数内完成的。所以我的目标是并行执行尽可能多的归约函数。
c++ ×10
c++20 ×10
constexpr ×2
c++-concepts ×1
concurrency ×1
constructor ×1
for-loop ×1
gcc11 ×1
openmp ×1
optimization ×1
search ×1
string ×1
unique-ptr ×1