You need to enable JavaScript to run this app. C++使用mismatch函数报no operator "=" matches these operands错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

C++使用mismatch函数报no operator "=" matches these operands错误如何解决

问题根因

编译报错的核心是你定义的position1类型和std::mismatch的返回值类型完全不匹配,具体原因如下:

  • s1、s2是std::string类型的原生数组,传入std::mismatch时数组名退化为指向数组元素的指针,因此这个场景下std::mismatch的返回值类型是std::pair<std::string*, std::string*>,两个元素都是指向完整std::string对象的指针。
  • 你定义的position1类型是std::pair<std::string::iterator, std::string::iterator>,而std::string::iterator是遍历单个std::string内部字符的迭代器,本质是指向char的指针,和指向完整std::string对象的指针是完全无关的两种类型,因此赋值时触发类型不匹配的编译错误。
修复方案

有两种可选修复方式:

  1. 优先使用auto自动推导返回值类型,避免手动写错类型:
auto position1 = mismatch(s1, s1+4, s2, cmp_string);
  1. 手动指定正确的类型:
pair<string*, string*> position1;
position1 = mismatch(s1, s1+4, s2, cmp_string);

其他可选优化点

  • 你代码中inputs1数组只有8个元素,初始化v1时用inputs1+9会触发数组越界访问,属于未定义行为,建议修改为inputs1+8。
  • 代码注释中的position2是笔误,应该修改为position1。
  • 自定义比较函数cmp_string的参数建议改为const std::string&,避免不必要的字符串拷贝,提升性能:
bool cmp_string(const std::string& i , const std::string& j)
{
    return i.size() == j.size();
}

内容的提问来源于stack exchange,提问作者Anuj Sunder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 14:15:02