find_if和函数对象结合实现对自定义类型vector的查找
2006-07-04 14:32
531 查看
实现NAT穿透需要对client的IP和port 进行同时查找,这就需要自定义的结构体,然后声明一个此种类型的vector,每次客户发起连接的时候都要查找一下此表,如果存在此客户端的ip 和port 就返回,否则将此结构体加入到转发列表中。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <functional>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct __NAT
{
char userip[30];
int userport;
} natType;
//查找类
class FindIP
{
public:
bool operator()(natType& dValue)
{
return (strcmp(dValue.userip,m_val.userip)==0)&&dValue.userport==m_val.userport;
}
static void SetUserNat(natType dval)
{
m_val = dval;
}
private:
static natType m_val;
};
natType natvalue,destvalue;
natType FindIP::m_val = natvalue;
int main(int argc, char* argv[])
{ vector <natType> userList;
natType tempnat;
strcpy(tempnat.userip,"192.168.0.1");
tempnat.userport=9000;
userList.push_back(tempnat);
//,destvalue;
strcpy(natvalue.userip,"192.168.0.1");
natvalue.userport=9000;
FindIP::SetUserNat(natvalue);
vector <natType> :: iterator iter=find_if(userList.begin(), userList.end(), FindIP()) ;
if(iter==userList.end())
{
/* strcpy(destvalue.userip,(*iter).userip);
*/destvalue.userport=(*iter).userport;
printf("nothing/n");
userList.push_back(natvalue);
}
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
#include <functional>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct __NAT
{
char userip[30];
int userport;
} natType;
//查找类
class FindIP
{
public:
bool operator()(natType& dValue)
{
return (strcmp(dValue.userip,m_val.userip)==0)&&dValue.userport==m_val.userport;
}
static void SetUserNat(natType dval)
{
m_val = dval;
}
private:
static natType m_val;
};
natType natvalue,destvalue;
natType FindIP::m_val = natvalue;
int main(int argc, char* argv[])
{ vector <natType> userList;
natType tempnat;
strcpy(tempnat.userip,"192.168.0.1");
tempnat.userport=9000;
userList.push_back(tempnat);
//,destvalue;
strcpy(natvalue.userip,"192.168.0.1");
natvalue.userport=9000;
FindIP::SetUserNat(natvalue);
vector <natType> :: iterator iter=find_if(userList.begin(), userList.end(), FindIP()) ;
if(iter==userList.end())
{
/* strcpy(destvalue.userip,(*iter).userip);
*/destvalue.userport=(*iter).userport;
printf("nothing/n");
userList.push_back(natvalue);
}
return 0;
}
相关文章推荐
- find_if和函数对象结合实现对自定义类型vector的查找
- find_if和函数对象结合实现对自定义类型vector的查找 复杂结构体查询某个值
- find_if查找vector内对象的成员 作为菜鸟一直不会用也不敢用
- 用find_if查找vector内对象的成员
- 使用find_if的自定义条件查找vector或map
- 用find_if查找vector内对象的成员
- STL 查找vector容器中的指定对象:find()与find_if()算法
- 【转】用find_if查找vector内对象的成员
- STL 查找vector容器中的指定对象:find()与find_if()算法
- 用find_if查找vector内对象的成员 http://blog.csdn.net/zhongguoren666/article/details/7629610
- 用find_if查找vector内对象的成员
- 4-4 求自定类型元素的平均 (10分) 本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType。 函数接口定义: ElementType Aver
- JavaScript基于自定义函数判断变量类型的实现方法
- (3)JavaScript基础(基本语法:变量与数据类型、类型转换、运算符、流程控制、函数、对象、自定义对象、原型)
- [C++学习笔记14]动态创建对象(定义静态方法实现在map查找具体类名对应的创建函数,并返回函数指针,map真是一个万能类)good
- [转] WinForm自定义函数FindControl实现按名称查找控件
- 6-4 求自定类型元素的平均(10 分) 本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType。
- vector查找结构体实例find_if
- 函数的二义性与函数对象的传递问题(通过实现vector的to_string示例)
- 8.9 编写函数打开文件用于输入,将文件内容读入 string 类型的 vector 容器,每一行存储为该容器对象 的一个元素。8.10 重写上面的程序,把文件中的每个单词存储为 容器的一个元素