C++ 映射查找值和关联的键
c++ map finding value and associated key
我用 c++ 开发了一个程序,其中我必须使用值在 stl map 中找到键。但是分配给键的值是 5 个元组(srcip,端口,destip,端口,srcno)
现在我想检查地图中是否有与值关联的键。我正在尝试这样的事情。
但它显示的错误像模板参数数量错误。注意(在我的程序中,成对键>值)值由 5 个变量的元组组成。
template<class T>
struct map_data_compare : public std::binary_function<typename T::value_type,typename T::mapped_type,bool>
{
public:
bool operator() (typename T::value_type &pair,typename T::mapped_type i)
{
return pair.second == i;
}
}
class Values
{
private:
std::string C_addr;
int C_port;
std::string S_addr;
int S_port;
int C_ID;
public:
Values(std::string,int,std::string,int,int);
void printValues();
};
Values :: Values(std::string Caddr,int Cport,std::string Saddr,int Sport,int Cid)
{
C_addr=Caddr;
C_port=Cport;
S_addr=Saddr;
S_port=Sport;
C_ID=Cid;
}
void Values::printValues()
{
cout << C_addr<<":" <<C_port<<":" << S_addr <<":" <<S_port << ":"<<C_ID <<endl;
}
//In main
{
typedef std::map<int, Values> itemsType;
itemsType items;
Values connection (inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port),inet_ntoa(servaddr.sin_addr),ntohs(servaddr.sin_port),clientID);
std::map<std::int,Values>::iterator it = std::find_if( items.begin(), items.end(), std::bind2nd(map_data_compare<itemsType>(),connection));
if ( it != items.end() )
{
assert( connection == it->second);
std::cout << "Found index:" << it->first << " for values:" << it->second << std::endl;
}
else
{
std::cout << "Did not find index for values:" << connection <<endl;
}
我用 c++ 开发了一个程序,其中我必须使用值在 stl map 中找到键。
这不是地图的目的。如果您需要这种访问权限,我建议您使用Boost.Bimap
如果"键"必须是唯一的,也许你可以尝试将键和值组合成一个std::pair
并将它们推送到std::set
中。
因为您似乎主要使用原始值作为我们对待的"键"。然后你可以使用内置的map::find()
函数
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++映射分割错误(核心转储)
- 内联映射初始化的动态atexit析构函数崩溃
- 使用"std::unordereded_map"映射到"std::list"对象
- 如何从多映射中删除特定的重复项
- 在未初始化映射的情况下,将值插入到映射的映射中
- QT通过C++添加映射QML项目
- 如何在 c++ 中创建分组多重映射键和关联值的表
- C++ 映射查找值和关联的键
- 使用boost::关联属性映射插入boost::BIMAP..失败
- 在多映射中,如何获取与给定值关联的键
- C++中的关联键到键映射
- 唯一关联哈希映射容器与关联哈希映射集装箱的属性是什么
- 为什么只有非常量映射提供类似于关联数组的直接元素检索?