Using Union to avoid reducing the efficiency of map replication struct

Source: Internet
Author: User

A test by yourself, which will be useful for future reference.

 

Use union to associate long with two 32-bit int values in a struct. If map is passed, use long. The struct is used normally when the struct is retrieved, reduce the efficiency of map copy struct during massive data processing

# Include <iostream> <br/> # include <iterator> <br/> # include <string> <br/> # include <map> <br/> using namespace STD; <br/> typedef Union <br/> {<br/> long key; <br/> struct {<br/> int FD; <br/> int use_count; <br/>} FD; <br/>} aio_fd; <br/> int main (void) <br/>{< br/> cout <sizeof (aio_fd) <Endl; </P> <p> Map <string, long> fd_map; <br/> aio_fd temp_fd; <br/> temp_fd.fd.fd = 2; <br/> temp_fd.fd.use_count = 18; </P> <p> fd_map.insert (make_pair ("AA", temp_fd.key); <br/> Map <string, long> :: iterator fd_iter = fd_map.find (string ("AA"); </P> <p> aio_fd get_fd; <br/> get_fd.key = fd_iter-> second; </P> <p> cout <"FD:" <get_fd.fd.fd <"/tuse count:" <get_fd.fd.use_count <Endl; <br/> return 0; <br/>} 

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.