看流星社区

 找回密码
 注册账号
查看: 1685|回复: 0

模板类的使用

[复制链接]

该用户从未签到

发表于 2017-6-2 11:07:54 | 显示全部楼层 |阅读模式
advance的使用:
        list<int> list_num;
        for (int i=0;i< 10; i++)
        {
                list_num.push_back(i);
        }
        list<int>::iterator ops;
        ops = list_num.begin();
        cout<<*ops<<endl;

        advance(ops, 3);
        cout<<*ops<<endl;

        advance(ops, -1);
        cout<<*ops<<endl;
打印结果
0
3
2


distance的使用
        list<int>::iterator nPos = find(list_num.begin(), list_num.end(), 5);
        if (nPos != list_num.end())
        {
                cout<<"the distance:"<<distance(list_num.begin(), nPos)<<endl;
        }找到的5到开始的距离是5




iter_swap的使用
        iter_swap(nPos, list_num.begin());
        for (list<int>::iterator pos=list_num.begin(); pos != list_num.end(); ++pos)
        {
                cout<<*pos<<"  ";
        }交换了第5的位置和第一个位置的置

string忽略大小写的比较与忽略大小写的子串查找
cout<<"**********************"<<endl;

        string str_one = "THANKYOU";
        string str_two = "ThankYou";
        if (str_one.size() == str_two.size() &amp;&amp;
                equal(str_one.begin(), str_one.end(),
                str_two.begin(),
                nocase_compare))
        {
                cout<<"equal"<<endl;
        }
        else
        {
                cout<<"not equal"<<endl;
        }

        //查找子串
        str_two = "yOu";
        string::iterator pos;
        pos = search(str_one.begin(), str_one.end(),
                                str_two.begin(),str_two.end(),
                                nocase_compare);
        if (pos == str_one.end())
        {
                cout<<"not find"<<endl;
        }
        else
        {
                cout<<"find"<<" at index:"<<pos-str_one.begin()<<endl;
        }
        return ;
}

BOOL nocase_compare(char c1, char c2)
{
        return toupper(c1)==toupper(c2);
}
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-19 12:06

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表