gpt4 book ai didi

c++ - 在 find_if 算法中将 char* 转换为 string::iterator

转载 作者:行者123 更新时间:2023-11-28 05:51:46 25 4
gpt4 key购买 nike

string temp = "****98776rth98*76"   // An arbitrary string
string::const_itertor a = find_if( temp.cbegin(), temp.cend(), [](string::const_itertor a){ return *a != '*'; }

我收到一条错误消息“无法将‘const char’转换为‘string::iterator’”。我能做什么?

最佳答案

std::find_if 的谓词采用常量引用(或类似的东西)指向迭代器取消引用的对象,而不是迭代器。将谓词更改为

[](const char& a){ return a != '*'; }

或者,如果您使用 C++14,

[](const auto& a){ return a != '*'; }

关于c++ - 在 find_if 算法中将 char* 转换为 string::iterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35109767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com