加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用vector和struct在C中获取错误

发布时间:2020-12-15 04:49:37 所属栏目:Java 来源:网络整理
导读:这段代码中的错误是什么: #include iostream#include vector#include algorithmusing namespace std;struct symtab{ string name; string location;};vectorsymtab symtab_details;bool search_symtab(string s){ if (find (symtab_details.begin(),symtab_
这段代码中的错误是什么:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct symtab{
  string name;
  string location;
};
vector<symtab> symtab_details;

bool search_symtab(string s){

  if (find (symtab_details.begin(),symtab_details.end(),s)!=symtab_details.end()) return true;
  return false;
}


int main() {

    bool get = search_symtab("ADD");
    return 0;
}

我收到以下错误:

usr/include/c++/4.8.2/bits/stl_algo.h:166:17: error: no match for ‘operator==’ (operand types are ‘symtab’ and ‘const std::basic_string’)
if (*__first == __val)

解决方法

您正在尝试在std :: vector< symtab>中找到std :: string,“ADD”.当然那不行.

你需要的是std::find_if.

auto it = std::find_if(symtab_details.begin(),[&s](symtab const& item) { return item.name == s; });
return  (it != symtab_details.end());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读