fjx16852318 发表于 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);
}
页: [1]
查看完整版本: 模板类的使用