zoukankan      html  css  js  c++  java
  • C++STL2--map

    C++STL2--map

    一、心得

    本质上就是数组,关联数组,map就是键到值得一个映射,并且重载了[]符号,所以可以像数组一样用

    map<string,int> cnt;//前键后值,键就可以理解为索引

    if(!cnt.count(r)) cnt[r]=0;//统计键值出现过没有

    二、用法

      1   头文件 
      #include   <map> 
      
      2   定义 
      map<string,   int>   my_Map; 
      或者是typedef     map<string,   int>   MY_MAP; 
      MY_MAP   my_Map; 
      
      3   插入数据 
      (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   查找数据和修改数据 
      (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; 
      不过注意,键本身是不能被修改的,除非删除。 
      
      5   删除数据 
      (1)   my_Map.erase(my_Itr); 
      (2)   my_Map.erase("c"); 
      还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。 
      
      6   迭代数据 
      for   (my_Itr=my_Map.begin();   my_Itr!=my_Map.end();   ++my_Itr)   {} 
      
      7   其它方法 
      my_Map.size()               返回元素数目 
      my_Map.empty()       判断是否为空 
      my_Map.clear()           清空所有元素 
      可以直接进行赋值和比较:=,   >,   >=,   <,   <=,   !=   等等 
      
      更高级的应用查帮助去吧,^_^;

    三、实例

    题目:

    Most crossword puzzle fans are 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 not have this
    attribute, no matter how you rearrange their letters, you cannot form another word. Such words are
    called ananagrams, an example is QUIZ.
    Obviously such definitions depend on the domain within which we are working; you might think
    that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible
    domain would be the entire English language, but this could lead to some problems. One could restrict
    the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the
    same domain) but NOTE is not since it can produce TONE.
    Write a program that will read in the dictionary of a restricted domain and determine the relative
    ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be
    “rearranged” at all. The dictionary will contain no more than 1000 words.
    Input
    Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any
    number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken
    across lines. Spaces may appear freely around words, and at least one space separates multiple words
    on the same line. Note that words that contain the same letters but of differing case are considered to
    be anagrams of each other, thus ‘tIeD’ and ‘EdiT’ are anagrams. The file will be terminated by a line
    consisting of a single ‘#’.
    Output
    Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram
    in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always
    be 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

    分析:

    把每个单词"标准化",即全部转化为小写字母后再进行排序,然后再放到map中进行统计

     1 /*
     2 map就是键到值得一个映射
     3 
     4 分析:把每个单词"标准化",即全部转化为小写字母后再进行排序,然后再放到map中进行统计
     5 
     6 其实真的挺简单的,就是一个map的用法。 
     7  
     8 */
     9 #include <iostream>
    10 #include <string>
    11 #include <cctype>
    12 #include <vector>
    13 #include <map>
    14 #include <algorithm>
    15 using namespace std;
    16 
    17 map<string,int> cnt;//用来单词对应的标准化后的次数 
    18 vector<string> words;//用来存每个单词 
    19 
    20 //将单词进行标准化
    21 string repr(const string& s){
    22     string ans=s;
    23     for(int i=0;i<ans.length();i++){
    24         ans[i]=tolower(ans[i]);//大写边小写 
    25     }
    26     sort(ans.begin(),ans.end());//将字母排序 
    27     return ans;
    28 } 
    29 
    30 int main(){
    31     freopen("in.txt","r",stdin);
    32     int n=0;
    33     string s;
    34     while(cin>>s){//读入单词 
    35         if(s[0]=='#') break;//输入结束标志 
    36         words.push_back(s);//单词插入words的vector中 
    37         string r=repr(s);//标准化:大写变小写,字母排序 
    38         if(!cnt.count(r)) cnt[r]=0;//,判断键出现过没有,统计次数 
    39         cnt[r]++;
    40     } 
    41     vector<string> ans;//用来储存符合题目要求的单词 
    42     for(int i=0;i<words.size();i++){
    43         if(cnt[repr(words[i])]==1) ans.push_back(words[i]);//判断单词对应的标准化出现的次数 
    44     }
    45     sort(ans.begin(),ans.end());//对结果单词进行排序 
    46     for(int i=0;i<ans.size();i++){
    47         cout<<ans[i]<<"
    ";//输出答案 
    48     }
    49     return 0;
    50 } 

  • 相关阅读:
    1025. 除数博弈
    剑指 Offer 12. 矩阵中的路径
    64. 最小路径和
    剑指 Offer 07. 重建二叉树-7月22日
    为人工智能、机器学习和深度学习做好准备的数据中心实践
    在云应用程序中加强隐私保护的9种方法
    迎接物联网时代 区块链大有可为
    Science 好文:强化学习之后,机器人学习瓶颈如何突破?
    学会这5招,让Linux排障更简单
    云游戏:5G时代的王牌应用
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/6971618.html
Copyright © 2011-2022 走看看