地图上的find_if有问题
Problem with find_if on map
我正在尝试在 std::map 上使用 std::find_if,寻找与字符串匹配的特定对象,如下所示:
class MyString
{
public:
MyString() {}
MyString(const std::string& x) : m_x(x) {}
const std::string& value() const
{
return m_x;
}
private:
std::string m_x;
};
std::map<int,MyString> squaresS;
std::map<int,MyString>::iterator iIt;
squaresS[1] = MyString("1");
squaresS[2] = MyString("4");
squaresS[3] = MyString("9");
const std::string sTarget = "4";
iIt = std::find_if(squaresS.begin(), squaresS.end(),
boost::bind(std::equal_to<std::string>(),
boost::bind(&MyString::value,
boost::bind(&std::map<int,MyString>::value_type::second, _1)),
sTarget));
if (iIt != squaresS.end())
std::cout << "Found " << iIt->second.value() << std::endl;
else
std::cout << "Not Found" << std::endl;
执行此代码的结果为"未找到";我预计会输出 Found 4。但是,如果我使用整数做大致相同的事情,那么它可以工作,即输出是 找到 4:
class MyInteger
{
public:
MyInteger() {}
MyInteger(int x) : m_x(x) {}
int value() const
{
return m_x;
}
private:
int m_x;
};
std::map<int,MyInteger> squaresI;
std::map<int,MyInteger>::iterator sIt;
squaresI[1] = MyInteger(1);
squaresI[2] = MyInteger(4);
squaresI[3] = MyInteger(9);
int iTarget = 4;
sIt = std::find_if(squaresI.begin(), squaresI.end(),
boost::bind(std::equal_to<int>(),
boost::bind(&MyInteger::value,
boost::bind(&std::map<int,MyInteger>::value_type::second, _1)),
iTarget));
if (sIt != squaresI.end())
std::cout << "Found " << sIt->second.value() << std::endl;
else
std::cout << "Not Found" << std::endl;
我怀疑这与 std::equal_to 有关,但我不确定如何解决这个问题。
以下是您可以执行的操作:
class MyString
{
public:
MyString() {}
MyString(const std::string& x) : m_x(x) {}
const std::string& value() const
{
return m_x;
}
private:
std::string m_x;
};
class mystringmatch
{
MyString _target;
public:
mystringmatch(const MyString& target):_target(target)
{
}
bool operator()(const std::pair<int, MyString>& src) const
{
return src.second.value() == _target.value();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::map<int,MyString> squaresS;
std::map<int,MyString>::iterator iIt;
squaresS[1] = MyString("1");
squaresS[2] = MyString("4");
squaresS[3] = MyString("9");
const std::string sTarget = "4";
iIt = std::find_if(squaresS.begin(), squaresS.end(), mystringmatch(sTarget));
if (iIt != squaresS.end())
std::cout << "Found " << iIt->second.value() << std::endl;
else
std::cout << "Not Found" << std::endl;
return 0;
}
在我看来,这种代码使人们从C++转向其他语言。几乎不可能阅读。
sIt = std::find_if(squaresI.begin(), squaresI.end(),
boost::bind(std::equal_to<int>(),
boost::bind(&MyInteger::value,
boost::bind(&std::map<int,MyInteger>::value_type::second, _1)),
iTarget));
正如其他人所指出的,您的代码已经对我有用(VC++ 2010 SP1)。也就是说,可以进行一个微不足道的更改来减少嵌套bind
的数量 - boost::bind
的返回类型(与std::bind
的返回类型不同)具有所有关系和逻辑运算符重载,包括operator==
,消除(或至少减轻)对std::equal_to<>
等适配器的需求。利用这一点将代码简化为:
typedef std::map<int, MyString> squares_t;
squares_t::const_iterator iIt = std::find_if(
squaresS.begin(),
squaresS.end(),
boost::bind(
&MyString::value,
boost::bind(&squares_t::value_type::second, ::_1)
) == sTarget
);
有关详细信息,请参阅 Boost.Bind 文档。
相关文章:
- 我似乎对if/else的基本语句有问题:/
- 我在范围内未声明的错误类有问题
- 我的C++语言蛮力算法有问题
- 我关于函数"Assert"的C++代码有问题
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++类中的友元函数有问题?
- 我对数组即 0x6dfe78 有问题
- 我对 std::unique(算法)C++有问题
- 使用 fstream 库并在屏幕上打印的文件有问题?
- 这个模板为什么有问题?如何正确编译
- 我对 MyGraph 属性顶点名称和边权重有问题
- delete[]有问题,如何部分删除内存
- 在C++中使用exit()退出程序有问题吗
- 跳转到if(false)块有问题吗
- 查找有问题的宏
- 使用 rand() 和 srand() 有问题
- C++ 回文程序总是给出 0(假)作为输出问题;我的代码哪里有问题?
- 我在 C++ "out_of_range at memory location"有问题
- 我在 c++ 中的 AES 代码有问题。明文正在更改,但加密邮件是相同的。我找不到我的错误
- 标头中的模板规范有问题