STL——关联容器

#include<iostream>  
#include<string>  
#include<fstream>
#include<vector> 
#include<map>  
#include<cctype>/*包含tolower()函数和ispunct函数;ispunct() 函数用来检测一个字符是否为标点符号或特殊字符*/
#include<algorithm>
using namespace std;

int main(int argc, char**argv)  
{ 
    //map的定义
    map<string,size_t> word_count;
    fstream in("1.txt");//定义一个输入流
    string word;

    while (in>>word)
    {
        string::iterator it1;
        for (it1 = word.begin(); it1 != word.end(); ++it1)
        {
            *it1 = tolower(*it1); 
        }//消灭大小写,把字符转换成小写字母,非字母字符不做出处理用 法: int tolower(int c);  *解引用
        word.erase(remove_if(word.begin(), word.end(), ispunct),word.end());//消灭标点符号
        ++word_count[word];
    }

    //map同样支持迭代器操作
    map<string ,size_t>::iterator mapi;
    for (mapi = word_count.begin(); mapi != word_count.end(); ++mapi)//C++ 11支持:const auto &s : word_count
    {
        //两个成员分别代表关键字和对应值
        cout<<mapi->first<<" ";
        cout<<mapi->second<<" "<<endl;
    }

    return 0;  
}  
#include<iostream>  
#include<string>  
#include<fstream>
#include<vector> 
#include<map>  
#include<cctype>//,包含tolower()函数和ispunct函数
#include<algorithm>
using namespace std;
//map关键字是家庭的姓,值是一个vector保存家中孩子们的名
int main(int argc, char**argv)  
{ 
    //map的定义
    map<string,vector<string>> family;
    
    string first_name,child_name;

    //在while中使用lambda语句,可以传入多条语句参数,将我们想要传入的输出参数也放在其中,注意后面的一对括号
    while ( [&]()->bool {cout<<"请输入家庭的姓:"; return cin>>first_name && (first_name != "end");}() )
    {
            cout<<"请输入孩子的名字:";
            while (cin>>child_name && (child_name != "end"))
            {
                family[first_name].push_back(child_name);
            }    
    }

    //map同样支持迭代器操作
    map<string ,vector<string>>::iterator mapi;
    for (mapi = family.begin(); mapi != family.end(); ++mapi)//C++ 11支持:const auto &s : word_count
    {
        //两个成员分别代表关键字和对应值
        cout<<mapi->first<<" :";
        vector<string>::iterator it1 = mapi->second.begin();
        for (it1; it1 != mapi->second.end(); ++it1)
        {
            cout<<*it1<<" ";
        }
        
    }

    return 0;  
}  
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility> //pair
using namespace std;
map<string,size_t> word_count;
set<string> exclude; //使用set忽略常见单词={"The","But"}
string word;
void main(){
    /*while(cin>>word) if(exclude.find(word) == exclude.end()) ++word_count[word]; //使用关联数组map:提取word的计数器并+1
    //find调用返回一个迭代器,如果给定关键字在set中,迭代器指向该关键字。否则,find返回尾后迭代器*/
    while(cin>>word){
        auto ret=word_count.insert(pair<string,size_t>(word,1));
        if(!ret.second)  ++ret.first->second;
    }
    /* ++((ret.first)->second);
    ret保存insert返回的值,是一个pair
    ret.first是pair的第一个成员,是一个map迭代器,指向具有给定关键字的元素;第二个元素是bool表明是否插入成功
    ret.first->解引用此迭代器,提取map中元素,元素是一个pair
    */
    string removal_word="a";
    if(word_count.erase(removal_word))
        cout<<removal_word<<" removed\n";
    else cout <<removal_word<<" not found\n";
    //erase 对于不重复关键字的容器,erase的返回值总是0或1。对允许重复关键字的容器,删除元素的数量可能大于1
    for(const auto &w:word_count)
        cout<<w.first<<" occurs "<<w.second<<((w.second>1)?" times":" time")<<endl;
    system("pause");
    /*打印操作,当使用一个迭代器遍历关联容器,迭代器按关键字升序遍历元素
    auto map_it = word_count.cbegin();
    while(map_it!= word_count.cend()){
        cout<<map_it->first<<" occurs"<<map_it->second<<" times"<<endl;
        ++map_it;//递增迭代器,移动到下一个元素
    */
}

 

posted on 2017-06-14 16:12  明静志远  阅读(389)  评论(0编辑  收藏  举报