gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:47:40 26 4
gpt4 key购买 nike

这段代码有什么错误:

    #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::string , "ADD" , 在 std::vector<symtab> .这当然行不通。

你需要的是 std::find_if .

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

关于c++ - 使用 vector 和 struct 在 C++ 中获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49592608/

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