小编Fli*_*lip的帖子

如何使用 C++ std::ranges 替换字符串中的子字符串?

我知道如何使用 std::replace 替换向量中的字符,如下所示:ranges::replace

我会撒谎对字符串中的子字符串做类似的事情。例如:

std::string str{"The quick brown fox jumped over the lazy dog.";
std::ranges::replace(str, "the", "a");
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

错误:与调用 '(const std::ranges::__replace_fn) (std::string&, const char [4], const char [2])' 不匹配

或者如果我使用字符串

错误:与调用 '(const std::ranges::__replace_fn) (std::string&, std::string&, std::string&)' 不匹配

它适用于字符,但不适用于子字符串。

有任何想法吗?

我已成功使用循环 andstring.findstring.replace,但想使用范围。

c++ string replace c++20 std-ranges

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

constexpr构造函数初始化程序列表中的Constexpr函数

我想用结构名称的哈希值初始化结构成员。

constexpr uint32_t myHash(const char* const data)
{ //Some code for hash
    return myHash;
}

struct My_Struct{
    constexpr Test() : ID(myHash("My_Struct"))
    {
    }
    const uint32_t ID; 
}
Run Code Online (Sandbox Code Playgroud)

当我有:

constexpr My_Struct my_constexpr_struct;
Run Code Online (Sandbox Code Playgroud)

然后,在编译时成功计算了哈希。但是,当我有主要职能时

My_Struct my_normal_struct;
Run Code Online (Sandbox Code Playgroud)

然后它将调用

constexpr uint32_t myHash(const char* const data)
Run Code Online (Sandbox Code Playgroud)

而不是简单地使用编译时间常数初始化struct成员。

显然,这将导致可避免的重大性能损失。

关于如何在编译时执行编译器的任何想法或建议?我真的不想做:

constexpr uint32_t MY_STRUCT_ID = myHash("My_Struct");
struct My_Struct{
    constexpr Test() : ID(MY_STRUCT_ID)
    {
    }
    const uint32_t ID; 
Run Code Online (Sandbox Code Playgroud)

谢谢。

c++ constexpr armclang

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

标签 统计

c++ ×2

armclang ×1

c++20 ×1

constexpr ×1

replace ×1

std-ranges ×1

string ×1