C + + STL mapping: Map

Source: Internet
Author: User

map is an associative container for STL that provides a one-to-one (where the first can be called a keyword, where each keyword appears only once in the map, and the second may be called the value of the keyword), and because of this feature, it is possible that when we process a one-to-one data, Provides fast channel programming. here the map internal data organization, map built a red black tree (a non-strict balance of binary tree), the tree has automatic sorting of data functions, so in the map all the data is ordered.

Function: establish key-value mapping key with value is any type you need exp:map<char,int> A to build a char to int mapping A.

Common statements: Begin () return to map header iterator

End () returns the trailing iterator

Clear () Clears all elements

Erase () Delete an element

Find () finds an element

Empty () returns true if NULL

Size () returns the map sizes

COUNT (elem) returns the number of elements

Example: UVa 156-Anti-tablet language

The main idea is to give you a string of strings to find the character of their own random combination of characters will not appear in these strings in alphabetical order.

Sample input

Ladder came tape soon leader Acme RIDE Lone Dreis Peat Scale orb  eye  Rides dealer  NotE derail laces  Driedn Oel dire Disk Mace Rob dries#

Sample output

Disknotederaildriedeyeladdersoon

Analysis


The solution to this problem is many, the simplest way is to use the map container. Think of using "normalization".
Overall idea:
1. Write a standardized function (to convert uppercase letters to lowercase (tolower () functions), Word sort. Note that const is used in order not to change the initial value of s)
2. Two vector container (Words,ans), a map container (CNT)
Words Store all the words
Map stores the values that correspond to the word and number of occurrences after normalization, which is equivalent to a table.
Words after the table map, the corresponding values to the ANS
The first time I wrote a map is pretty cool ...

#include <iostream>
#include <map>
#include <vector>
#include <string>
# Include<algorithm>
using namespace std;
map<string,int>mmap;
Vector<string> str;


String Standard (const string &s)
{
string t=s;
for (int i=0;i<s.size (); i++)
{
T[i]=tolower (s[i]);//Convert to lowercase letters  
}
Sort (T.begin (), T.end ());
return t;
}


int main ()
{
string s;
while (Cin>>s)
{
if (s[0]== ' # ')
break;
Str.push_back (s);//push into vector 
String R=standard (s);//Normalize it

mmap[r]++;
}

Vector<string>ans;
For (Vector<string>::iterator It=str.begin (); It!=str.end (); it++)
{
if (Mmap[standard (*it)]==1)
Ans.push_back (*it);
}

Sort (ans.begin (), Ans.end ());
For (Vector<string>::iterator It=ans.begin (); It!=ans.end (); it++)
{
    cout<<*it <<endl;
}


return 0;
 } 

C + + STL mapping: Map

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.