C++stl1--map

Source: Internet
Author: User

C++stl1--map First, experience

It is essentially an array, an associative array, a map is a key to a map, and overloads the [] symbol, so it can be used like an array .

Map<string,int> cnt;// Pre-key value , the key can be understood as the index

if (!cnt. Count (R)) cnt[r]=0;//Statistic key value has occurred

Second, usage

1 Header Files
#include <map>

2 Definitions
Map<string, int> My_map;
or a typedef map<string, int> My_map;
My_map My_map;

3 Inserting data
(1)my_map["a"] = 1;
(2) My_map.Insert(Map<string, Int>::value_type ("B", 2));
(3) My_map.insert (pair<string,int> ("C", 3));
(4) My_map.insert (make_pair<string,int> ("D", 4));

4 finding data and modifying data
(1) int i = my_map["a"];
my_map["a"] = i;
(2) My_map::iterator My_itr;
My_itr.find ("B");
Int J = my_itr->second;
My_itr->second = j;
Note, however, that the key itself cannot be modified unless it is deleted.

5 Deleting data
(1) my_map.erase (MY_ITR);
(2)my_map.erase ("C");
It is also important to note that the first case cannot be deleted during the iteration, just as the element cannot be deleted while foreach.

6 Iterative data
For (My_itr=my_map.begin ();   My_itr!=my_map.end (); ++MY_ITR) {}

7 Other methods
My_map.size () returns the number of elements
My_map.empty () to determine if it is empty
My_map.clear () clears all elements
can be assigned and compared directly: =, >=, &lt, <=,! =, etc.

More advanced application Check help go, ^_^;

Third, examples

Topic:

Most crossword puzzle fans is used to anagrams-groups of words with the same letters in different
orders-for example OPTS, SPOT, STOP, POTS and POST. Some words however do
attribute, no matter how do you rearrange their letters, you cannot form another word. Such words is
Called Ananagrams, an example is QUIZ.
Obviously such definitions depend on the domain within which we is working; You might think
That's Athene is a ananagram, whereas any chemist would quickly produce ethane. One possible
Domain would be the entire 中文版 language, but this could leads to some problems. One could restrict
The domain to, say, Music, in which case scale becomes a relative ananagram (laces are not in the
Same domain) but NOTE was not since it can produce TONE.
Write a program that would read in the dictionary of a restricted domain and determine the relative
Ananagrams. Note that single letter words is, ipso facto, relative ananagrams since they cannot be
"Rearranged" at all. The dictionary would contain no more than words.
Input
Input would consist of a series of lines. No line would be more than-characters long, but may contain any
Number of words. Words consist of up to upper and/or lower case letters, and is not being broken
Across lines. Spaces appear freely around words, and at least one space separates multiple words
On the same line. Note that words this contain the same letters but of differing case is considered to
Be anagrams of all other, thus ' TIeD ' and ' EdiT ' is anagrams. The file is being terminated by a line
Consisting of a single ' # '.
Output
Output would consist of a series of lines. Each line would consist of a single word this is a relative ananagram
In the input dictionary. Words must is output in lexicographic (case-sensitive) order. There'll always
BES at least one relative ananagram.
Sample Input
Ladder came tape soon leader Acme RIDE Lone Dreis Peat
Scale Orb Eye Rides dealer NotE derail laces dried
Noel Dire Disk Mace Rob Dries
#
Sample Output
Disk
NotE
Derail
Dried
Eye
Ladder
Soon

Analysis:

"Standardize" each word, that is, convert all to lowercase and then sort, and then put it in the map for statistics

1 /*2 map is the key to the worth of a map3 4 Analysis: Each word "standardized", that is, all converted to lowercase letters before sorting, and then put in the map for statistics5 6 In fact, it's quite simple, it's a map usage. 7  8 */9#include <iostream>Ten#include <string> One#include <cctype> A#include <vector> -#include <map> -#include <algorithm> the using namespacestd; -  -map<string,int> cnt;//The normalized number of words used to correspond to a word -vector<string> words;//to save every word . +  - //standardize the words + stringRepr (Const string&s) { A     stringans=s; at      for(intI=0; I<ans.length (); i++){ -Ans[i]=tolower (Ans[i]);//Uppercase Edge Lowercase -     } -Sort (Ans.begin (), Ans.end ());//Sort the letters -     returnans; - }  in  - intMain () { toFreopen ("In.txt","R", stdin); +     intn=0; -     strings; the      while(Cin>>s) {//read in words *         if(s[0]=='#') Break;//Enter End Flag $Words.push_back (s);//words inserted into the vector of wordsPanax Notoginseng         stringR=REPR (s);//Normalization: Uppercase to lowercase, alphabetical sort -         if(!cnt.count (R)) cnt[r]=0;//, the key has not been seen, the number of statistics thecnt[r]++; +     }  Avector<string> ans;//used to store words that meet the requirements of the topic the      for(intI=0; I<words.size (); i++){ +         if(Cnt[repr (words[i])]==1) Ans.push_back (Words[i]);//determine the number of times a word corresponds to a normalized occurrence -     } $Sort (Ans.begin (), Ans.end ());//sort the resulting words $      for(intI=0; I<ans.size (); i++){ -cout<<ans[i]<<"\ n";//Output Answer -     } the     return 0; -}

C++stl1--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.