map和string的基本用法

个人网站http://www.ravedonut.com/

因为STL基本的容器用法差不多,只有部分不同,所以在写的时候也精简了很多。

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能

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的函数

而关于string的详细部分可以参考

http://www.cnblogs.com/this-543273659/archive/2011/07/21/2113172.html

 

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class MyPrint
{
    //仿函数
    //仿函数是一个模板类,代替了函数指针
    //函数指针始终函数的调用,而仿函数是在编译时内联(inline)。
    //仿函数之所以采用类的形式,是因为类有继承机制。

    public:
    void operator()(int elem) const
    {
        cout<<elem<<"  ";
    }
};

bool isdayu6(int elem)
{
    return elem>6;
}

int main()
{


    
    vector<int>v1;
    for (int i=0;i<10;i++)
    {
        v1.push_back(i);
    }
    for_each(v1.begin(),v1.end(),MyPrint());
    cout<<endl;

    //STL find 和find_if 用法
    vector<int>::iterator iter1;
    iter1=find(v1.begin(),v1.end(),5);
    cout<<"找到 5 ?"<<*iter1<<endl;
    
    while(1)
    {
        iter1=find_if(iter1,v1.end(),isdayu6);
        if (iter1!= v1.end())
        {
            cout<<*iter1<<"  ";
            iter1++;
        }
        else
        {
            break;
        }
    }
    cout<<endl;


    //map是将key/value当作元素进行管理,可以根据key自动排序,但搜寻元素性能比较差
    map<float,string>m1; //map<float,string>m1; 第一个参数是元素的key值,第二个则是value。
    //注意: key/value必须具有可赋值和可复制
    //key必须可以比较
    m1[90]="pan";
    m1[80]="xu";    //用数组方式插入数据
    m1.insert(pair<float,string>(77,"lee"));    //用insert函数插入pair数据
    m1.insert(map<float,string>::value_type(75,"jia"));//用insert函数插入value_type数据
    /*
    用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,
    insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值
    m1.insert(map<float, string>::value_type (1, “lee”));
    m1.insert(map<float, string>::value_type (1, “jia”));
    上面这两条语句执行后,map中1这个关键字对应的值是“lee”,第二条语句并没有生效
    
    我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下

    Pair<map<float, string>::iterator, bool> Insert_Pair;

    Insert_Pair = m1.insert(map<float, string>::value_type (1, “lee”));

    我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,
    如果插入成功的话Insert_Pair.second应该是true的,否则为false

    */
    map<float,string>::iterator ita;
    int i;
    for (i=0,ita=m1.begin();ita != m1.end();i++,ita++)
    {
        cout<<"m1:"<<ita->first<<" "<<ita->second<<endl;
    }

    cout<<"m1的大小:"<<m1.size()<<endl; //大小
    cout<<"m1中80的有?个:"<<m1.count(80)<<endl;//找到 key==80的元素个数
    map<float,string>::iterator pos;
    pos =m1.find(90);//返回 key==90的第一个元素,找不到就返回end()
    cout<<"m1中查找90:"<<pos->first<<" "<<pos->second<<endl;
    cout<<endl;
    cout<<"反向迭代器"<<endl;
    map<float, string>::reverse_iterator  iter;//也要使用反向迭代器
    for (iter=m1.rbegin();iter != m1.rend();iter++)
    {
        cout<<"m1:"<<iter->first<<" "<<iter->second<<endl;
    }
    cout<<endl;

    //字符串连接
    string s1="hello";
    string s2="world";
    string s3=s1+",";
    //string s4="hello"+",";//错误
    string s5=s1+","+"world";
    //string s6="hello"+","+s2;//错误

    //+操作符的左右操作数必须有一个是string类型的



    string str1("hello");
    string str2(str1);
    cout<<str1<<endl;
    //int StrSize=str1.size();
    cout<<str1.size()<<endl;
    cout<<str1.empty()<<endl;
    string str3=str1+str2;
    cout<<str3<<endl;

    cout<<endl;

#pragma warning (disable:4800)
    cout<<(bool)isalnum('c')<<endl;//如果c是字母或者数字,则为true
    cout<<(bool)isalpha('c')<<endl;//如果c是字母,则为true
    cout<<(bool)iscntrl('c')<<endl;//如果c是控制字符,则为true
    cout<<(bool)isdigit('c')<<endl;//如果c是数字,则为true
    cout<<(bool)isgraph('c')<<endl;//如果c不是空格,但可以打印,则为true
    cout<<(bool)islower('c')<<endl;//如果c是小写字母,则为true
    cout<<(bool)isprint('c')<<endl;//如果c是可打印字符,则为true
    cout<<(bool)ispunct('c')<<endl;//如果c是标点符号,则为true
    cout<<(bool)isspace('c')<<endl;//如果c是空白字符,则为true
    cout<<(bool)isupper('c')<<endl;//如果c是大写字母,则为true
    cout<<(bool)isxdigit('c')<<endl;//如果c是十六进制数,则为true
    cout<<(char)tolower('c')<<endl;//如果c为大写字母,返回其小写字母,否则直接返回
    cout<<(char)toupper('c')<<endl;//如果c为小写字母,返回其大写字母,否则直接返回


    
    



    system("pause");
    return 0;
}

 

posted @ 2012-06-12 10:28  狂吼的面包圈  阅读(5505)  评论(0编辑  收藏  举报