自定义C++谓词并查找if
Custom C++ Predicate and find if
所以我有一个赋值,它有这个数据结构
struct ZipCode
{
char s[4];
int zip;
bool operator() (const ZipCode &lhs, const ZipCode &rhs) const
{return lhs.s[1] < rhs.s[1];}
bool operator() (const char* st, const int &z) const
{return st == s && z == zip;}
bool operator() (const int &z) const
{return zip == z;}
};
然后,我将这些数组插入到STL:set中,并试图稍后找到一个具有特定zip的数组。我通过使用像这样的find_if来实现这一点
std::set<ZipCode, ZipCode>::iterator itz = find_if(mySet.begin(), mySet.end(),ZipCode()(0xbb77));
当我编译它时,我得到以下错误:
c: \program files(x86)\microsoft visual studio 10.0\vc\include\algithm(83):错误C2064:术语未计算为采用1个参数的函数
我不确定问题是什么,也不知道如何解决这个问题
您会得到错误,因为operator()是一个成员函数,所以您使用的函数有两个参数,this
和0xbb77。
您可以改用set::find
。
如果要使用不同的类成员值进行搜索,则必须为ZipCode
提供三个构造函数,其中每个构造函数都使用与运算符()重载之一相同的参数。然后添加一个运算符==(const-ZipCode&other)函数并去掉运算符()s。
我会把它打出来,但在iPad上很难做到。
首先,将一个结构同时作为信息的存储和的函子使用三种不同的用途是糟糕的设计。这太复杂了。我差点以为std::set<ZipCode, ZipCode>
是非法的。
其次,你的问题的干净解决方案应该是一张地图。std::map<int, std::array<char, 4>>
。STL通用算法对std::set
(通常是红黑树)中任何数据结构的良好特性一无所知,因此使用map::find
。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- if else VS映射查找
- 自定义C++谓词并查找if
- 如何在不使用if语句的情况下查找数组中的最大值和最小值
- 表查找替代 if - then - else if 块
- 使用 #ifdef 或 #if defined() 查找变量定义(未)的位置
- 仅使用else-if语句查找4个变量中的最小值,c++
- 优化头痛 - 从查找表中删除 if
- 你能用一个函数局部类作为谓词来查找if吗