Basic methods for C + + containers

Source: Internet
Author: User

Map

Define map:map<string,int> Word_count;

Add new element: word_count["Hengshan"]=10; This method will find if the new element is inserted directly if it is not found. You can also use Insert, such as Word_count.insert (Make_pair ("Hengshan", 10)), which is more orthodox.

Find an element: Using subscript will have a side effect, that is, if the element does not exist, an element will be created automatically, so it is better to use find. such as int aa=word_count.find ("Hengshan"), you can also use count to find out how many times an element has occurred. Use pointers to find an element without automatically adding, such as Map<string,int>::iterator it=word_count.find ("Hengshan");

if (It!=word_count.end ()) Cout<<it->second ();

Delete element: Word_count.erase ("Hengshan")
Traversal: Using iterators iterator
Sizes size  

Set?

Sizes: Size

Finds find, returns the pointer type, iterator

Count, look for a few such elements

Add: Insert

Delete: Erase

Set is stored in a certain order, for example, the memory string is in the dictionary order, and the first addition to the set is irrelevant.

Basic methods for C + + containers

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.