Two problems encountered by map and Their Solutions

Source: Internet
Author: User
Tags map data structure

A map data structure is defined as follows: Map <tuple4, ctcpmanage> m_maptcp;

The key-value type is tuple4, which stores the TCP tuples and is defined as follows:

  

Struct tuple4

{

U_int32_t sourceip;

U_int32_t destip;

Unsigned short sport;

Unsigned short dport;

};

The associated value type is ctcpmanage, which is responsible for various operations on a TCP connection. Its constructor is as follows:

Ctcpmanage (tuple4 tuple4, bool bc2s );

Problems:

1. when the key value is used to search for elements in the map, Map <tuple4, ctcpmanage>: iterator iter = m_maptcp.find (tuple); statement compilation error, error message: /usr/lib/GCC/i386-redhat-linux/4.1.2/operator error: no match is 'operator' <'in' _ x <_ y'

Error cause:
The internal data structure of map is a binary search tree (rb tree). During the search operation, it starts from the root node of the binary tree. If the target key value is greater than the key value of the node, search for the right subtree. Otherwise, search for the left subtree. Therefore, when using map, you must define the relationship between key values, that is, you must define the operator "<".

Solution:

Define the tuple4 less than operator as follows:

Bool operator <(const tuple4 elem1, const tuple4 elem2)
{
If (elem1.sourceip! = Elem2.sourceip)
{
Return (elem1.sourceip <elem2.sourceip );
}
Else if (elem1.destip! = Elem2.destip)
{
Return (elem1.destip <elem2.destip );
}
Else if (elem1.sport! = Elem2.sport)
{
Return (elem1.sport <elem2.sport );
}
Else if (elem1.dport! = Elem2.dport)
{
Return (elem1.dport <elem2.dport );
}
Else
{
Return false;
}
}

2. m_maptcp [tuple] = objtcpmanage; error, error message:/usr/lib/GCC/i386-redhat-linux/4.1.2/errors: To 'ctcpmanage :: no matching function is called for ctcpmanage () '.

Error cause:

Map uses the keyword tuple to find the corresponding Association value. If the association value is found, the corresponding Association value data is returned. If not, add an element. The key value of the added element is tuple and the associated value is an automatically generated object. By default, this object calls a non-parameter constructor.

Solution: ctcpmanage objtcpmanage (tuple, true); m_maptcp.insert (STD: make_pair (tuple, objtcpmanage ));

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: info-contact@alibabacloud.com 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.