部分STL_hanhan~

1 篇文章 0 订阅
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <map>
using namespace std;
 
int main()
{
    map<string,int> word_count;//定义一个map对象empty map
    word_count["Anna"]=1;   //对于map容器,如果 下标所表示的键在容器中不存在,则添加新元素
    cout<<word_count["Anna"]<<endl;
    ++word_count["Anna"];//对键所对应的值进行操作
    cout<<word_count["Anna"]<<endl;
 
    string s;
/*  while (cin>>s)
        ++word_count[s];
*/
    //下面就是STL特有的操作啦~
    //insert插入新元素
    word_count.insert(map<string, int>::value_type("myc",1));
    //或者
    word_count.insert(make_pair("wh",1));//用make_pair简洁一点~
    //这个和上面没什么区别
    //但是insert函数只能插入新元素,就是如果试图插入的元素所对应的键已经在容器中,则insert将不作任何操作,不能对其进行赋值
 
    //查找操作:
    //下标操作符给出了读取一个值的最简单的方法:
    s="myc";
    int x=word_count[s];//但是这样操作有危险,就是如果原先该键不在容器中,那么这种下表操作将会插入一个新的具有该键的元素
    //map提供了两种查找操作:count操作:
    if (word_count.count("Anna"))//count返回0或1,表示是否存在
        word_count["Anna"]++;
    cout<<word_count["Anna"]<<endl;
    //find操作:STL里所有find操作都是返回指向元素的迭代器,如果该元素不存在,则返回end迭代器
    int q;
    map<string,int>::iterator it=word_count.find("myc");//这是定义一个迭代器it,it被赋值为find的返回值
    if (it != word_count.end())
        q=it->second;
    cout<<q<<endl;
 
    //删除操作:erase
    s="Anna";
    if (word_count.erase(s))
        cout<< "ok: " << s << " removed!"<<endl;
    else cout<< "oops: " <<s<< " not found!"<<endl;
    cout<<endl;
    //erase函数返回被删除的元素的个数,由于map容器每个键值只能出现一次,改值必然是0或1,返回0表示欲删除的元素在map中不存在
 
    //遍历整个map容器中的所有对象:
    //先添加一点对象哈~
    word_count["Angel"]=3;
    word_count["ff"]=45;
    word_count["em"]=31;
    word_count["ububu"]=64;
    //首先,和其他容器一样,map提供begin和end两个运算,以生成用于遍历整个容器的迭代器
    //建一个迭代器map_it指向第一个元素:
    map<string,int>::const_iterator map_it=word_count.begin();
    while (map_it != word_count.end())
    {
        cout << map_it->first << "~~~~~" << map_it->second<<endl;
        ++map_it;
    }
    return 0;
}
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
int main()
{
    int n;
    while(cin>>n&&n)
    {
        map<string,int>ma;
        map<string,int>mb;
        string a,b;
        while(n--)
        {
            cin>>a>>b;
            if(mb[a]==0){ma[a]=1;}
            ma[b]=0;mb[b]=1;
        }
        int sum=0;
        map<string ,int>::iterator it;
        for(it=ma.begin(); it != ma.end(); it++)
        {
            if(it->second==1)sum++;
        }
        if(sum!=1)cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值