map用法

map本质:

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

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

map用法总结

1 头文件
#include

2 定义

map my_Map;

或者是typedef map MY_MAP;
MY_MAP my_Map;

3 插入数据

(1) my_Map["a"] = 1;

(2) my_Map.insert(map::value_type("b",2));
(3) my_Map.insert(pair("c",3));
(4) my_Map.insert(make_pair("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() 清空所有元素
可以直接进行赋值和比较:=, >, >=, <, <=, != 等等

map的基本操作函数:

  C++ Maps是一种关联式容器,包含“关键字/值”对
  begin()          返回指向map头部的迭代器
  clear()         删除所有元素
 count()          返回指定元素出现的次数
  empty()          如果map为空则返回true
  end()            返回指向map末尾的迭代器
  equal_range()    返回特殊条目的迭代器对
  erase()          删除一个元素
  find()           查找一个元素
  get_allocator()  返回map的配置器
  insert()         插入元素
  key_comp()       返回比较元素key的函数
  lower_bound()    返回键值>=给定元素的第一个位置
  max_size()       返回可以容纳的最大元素个数
  rbegin()         返回一个指向map尾部的逆向迭代器
  rend()           返回一个指向map头部的逆向迭代器
  size()           返回map中元素的个数
  swap()            交换两个map
  upper_bound()     返回键值>给定元素的第一个位置
  value_comp()      返回比较元素value的函数

例题

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

/*
存读入的单词,标准化每个单词后形成一个键值对,
把满足条件的存到一个新的容器里,排序后输出

*/
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

map<string,int> cnt;//用来单词对应的标准化后的次数 
vector<string> words;//用来存每个单词 

//将单词进行标准化
string repr(const string& s){
    string ans=s;
    for(int i=0;itolower(ans[i]);//大写边小写 
    }
    sort(ans.begin(),ans.end());//将字母排序 
    return ans;
} 

int main(){
    freopen("in.txt","r",stdin);
    int n=0;
    string s;
    while(cin>>s){//读入单词 
        if(s[0]=='#') break;//输入结束标志 
        words.push_back(s);//单词插入words的vector中 
        string r=repr(s);//标准化:大写变小写,字母排序 
        if(!cnt.count(r)) cnt[r]=0;//,判断键出现过没有,统计次数 
        cnt[r]++;
    } 
    vector<string> ans;//用来储存符合题目要求的单词 
    for(int i=0;iif(cnt[repr(words[i])]==1) ans.push_back(words[i]);//判断单词对应的标准化出现的次数 
    }
    sort(ans.begin(),ans.end());//对结果单词进行排序 
    for(int i=0;icout<"\n";//输出答案 
    }
    return 0;
}

你可能感兴趣的:(acm练习(c++/c))