首页 > 其他 > 详细

map 学习笔记

时间:2014-06-05 18:58:44      阅读:419      评论:0      收藏:0      [点我收藏+]

1.map 常用功能

bubuko.com,布布扣
/****************************************
* File Name: map.cpp
* Author: sky0917
* Created Time: 2014年06月 4日 15:49:14
****************************************/
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    /*
     * map::insert 的使用
     */
    map<string, int> word_count;

    word_count.insert(map<string, int>::value_type("zhouzhou",1));
    cout<<word_count["zhouzhou"]<<endl;

    pair<string, int> p("zz",2);
    word_count.insert(p);
    cout<<word_count["zz"]<<endl;

    word_count.insert(make_pair("zz", 1));    

    cout<<p.first<<" "<<p.second<<endl;
    word_count["ww"] = 0;
    
    pair<map<string, int>::iterator, bool> res = word_count.insert(make_pair("zz",8));
    cout<<++res.first->second<<endl;
    
    word_count["tt"] = 7;
    cout<<"start"<<endl;
    for (map<string, int>::iterator it = word_count.begin(); it != word_count.end(); ++it){
        cout<<(*it).first<<" "<<(*it).second<<endl;
    }
    cout<<"end"<<endl;
    cout<<"size = " << word_count.size()<<"----"<<endl;
    /* 
     * 查找并读取 map 中的元素 
     */    
        
    int tmp = word_count["sss"];
    tmp = word_count["2sdf"];    // 这种查找方式的缺点在于如果该元素不存在,则会插入一个具有该键值的新元素
    word_count["zz"] = 9;
    if (word_count.count("zz")){
        int cont = word_count["zz"];
    }
    map<string, int>::iterator iot = word_count.find("zz");    
    
    
    if (iot != word_count.end())
        cout<<(*iot).first<<" "<<(*iot).second<<endl;
    
    /*
     * 从 map 中删除元素
     */    
    word_count["bbz"] = 44;
    cout<<word_count["bbz"]<<" "<<endl;
    cout<<"size = "<< word_count.size()<<endl;
    word_count.erase("bbz");    
    cout<<"size = "<< word_count.size()<<endl;
    
    return 0;
}
bubuko.com,布布扣

 

2. map 示例:

bubuko.com,布布扣
/****************************************
* File Name: map_test.cpp
* Author: sky0917
* Created Time: 2014年06月 4日 17:27:34
****************************************/
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

ifstream& open_file(ifstream &in, const string &file)
{
    in.close(); // colse in case it was already open
    in.clear(); // clear any existing errors
    // if open fails, the stream will be in an invalid state
    in.open(file.c_str());  // open
    return in;
}    
int main(int argc, char **argv){
    map<string, string> trm;
    string key , value;
    if (argc != 3){
        puts("wrong number of arguments");    
        exit(0);
    }
    ifstream mfile;
    if (!open_file(mfile, argv[1])){
        puts("cann‘t open the first file");
        exit(0);
    }
    while (mfile >> key >> value){
        cout<<"key = " << key <<"value = "<<value<<endl;
        trm.insert(make_pair(key, value));
    }

    ifstream input;
    if (!open_file(input, argv[2])){
        puts("cann‘t open the last file");
        exit(0);
    }
    string line;
    while (getline(input, line)){
        istringstream stream(line); // read the line a word at a time
        string word;
        bool firstword = true;
        while (stream >> word){
            map<string, string>::const_iterator it = trm.find(word);
            if (it != trm.end())
                word = it->second;
            if (firstword)
                firstword = false;
            else 
                cout<<" ";
            cout<<word;
        }    
        cout<<endl;
    }
    return 0;
}
bubuko.com,布布扣

 

 

 

map 学习笔记,布布扣,bubuko.com

map 学习笔记

原文:http://www.cnblogs.com/sky0917/p/3768482.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!