C++ Can';t分配给mapType::const_迭代器?(“无运算符=匹配”)

C++ Can';t分配给mapType::const_迭代器?(“无运算符=匹配”),c++,string,stl,iterator,C++,String,Stl,Iterator,我知道,一旦pred解析为true,find_if将返回一个迭代器,那么这是什么呢? 我们只能猜测错误,因为您只提供了问题的一半 假设d是mapType,并且是 问题是函子被传递一个对象到mapType::value\u type(这是映射和所有容器存储其值的方式)。对于map,value_类型实际上是一个键/值对,实际实现为std::pair。因此,您需要使用isalnum()获取要测试的对象的第二部分 在这里,我将这个翻译包装在另一个函子isAlphaNumFromMap中,find_if可

我知道,一旦pred解析为true,find_if将返回一个迭代器,那么这是什么呢?

我们只能猜测错误,因为您只提供了问题的一半

假设d是
mapType
,并且是

问题是函子被传递一个对象到mapType::value\u type(这是映射和所有容器存储其值的方式)。对于map,value_类型实际上是一个键/值对,实际实现为std::pair。因此,您需要使用isalnum()获取要测试的对象的第二部分

在这里,我将这个翻译包装在另一个函子isAlphaNumFromMap中,find_if可以使用它

Error:no operator "=" matches these operands
#包括
#包括
#包括
//使用ctype.h将C函数引入全局命名空间
//如果改用cctype,它会将它们带到std名称空间中
//注意:根据新标准,它们可能是两个名称空间中的n个。
#包括
typedef std::map mapType;
结构isAlphaNumFromMap
{
布尔运算符()(映射类型::值类型常量&v)常量
{
返回:isalnum(v.second);
}
};
int main()
{
mapType::const_迭代器i;
地图类型d;
i=std::find_if(d.begin(),d.end(),isAlphaNumFromMap());
}

如果
d
地图
,则问题在于您试图使用
isalnum

isalnum
接受一个
int
参数,但是
find\u if
调用的谓词接收一个
map::value\u类型
。这些类型不兼容,因此您需要将
find_if
调整为'isalnum'。例如:

#include <map>
#include <string>
#include <algorithm>
// Using ctype.h brings the C functions into the global namespace
// If you use cctype instead it brings them into the std namespace
// Note: They may be n both namespaces according to the new standard.
#include <ctype.h> 

typedef std::map<std::string,int> mapType;

struct isAlphaNumFromMap
{
    bool operator()(mapType::value_type const& v) const
    {
        return ::isalnum(v.second);
    }
};
int main()
{
    mapType::const_iterator i;
    mapType                 d;

    i = std::find_if( d.begin(), d.end(), isAlphaNumFromMap() );

}
#包括
#包括
#包括
#包括
使用名称空间std;
typedef映射类型;
bool是alnum(mapType::value\u type v)
{
返回0!=isalnum(v.second);
}
int main()
{
mapType::const_迭代器i;
地图类型d;
i=如果(d.begin(),d.end(),是alnum,则查找);
}

d是对地图的引用,很抱歉造成混淆。@bluetick是对const的引用还是仅仅是引用?请发布
d
的声明好吗?
#include <map>
#include <string>
#include <algorithm>
// Using ctype.h brings the C functions into the global namespace
// If you use cctype instead it brings them into the std namespace
// Note: They may be n both namespaces according to the new standard.
#include <ctype.h> 

typedef std::map<std::string,int> mapType;

struct isAlphaNumFromMap
{
    bool operator()(mapType::value_type const& v) const
    {
        return ::isalnum(v.second);
    }
};
int main()
{
    mapType::const_iterator i;
    mapType                 d;

    i = std::find_if( d.begin(), d.end(), isAlphaNumFromMap() );

}
#include <cstdlib>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

typedef map<string,int> mapType;

bool is_alnum(mapType::value_type v)
{
    return 0 != isalnum(v.second);
}

int main()
{
    mapType::const_iterator i;

    mapType d;

    i = find_if( d.begin(), d.end(), is_alnum );
}