Use find_if and function object to search for a vector of the custom type.

Source: Internet
Author: User

To implement NAT penetration, You need to search for the client's IP address and port at the same time. In this case, you need to customize the struct and then declare a vector of this type, each time a customer initiates a connection, he/she needs to search for this table. If the client's ip address and port exist, the system returns the result. Otherwise, the structure is added to the forwarding list.
# 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;
// Query class

Class FindIP
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;
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, " ");
Tempnat. userport = 9000;
UserList. push_back (tempnat );
//, Destvalue;
Strcpy (natvalue. userip, " ");
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;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.