迭代器和迭代器范围

例1:

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>

//所有的迭代器接口都是一样的
//c++经常使用迭代器
//vector和deque有额外的功能
//是迭代器操作失效
//顺序容器和关联容器

using namespace std;
//容器都在std名称空间里


bool findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel);



int main()
{
    vector<int> a;
    deque<int> b;
    list<int> c;

    a.push_back(0);
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a.push_back(4);

    auto iter1 = a.begin();//或者是vector<int>::iterator iter1=a.begin();
    cout << *iter1 << endl;
    auto iter2 = a.end();//end指向最后一个的下一个,特殊的位置,标记,范围是不包括end的
    //auto m = iter2+ a.size / 2;//指向中间的那个数


    if (findInt(iter1, iter2, 3))
        cout << "find ok" << endl;
    else
        cout << "find false" << endl;
    system("pause");
    return 0; 
}

bool findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//不检查end
{
    while (beg != end)
    {
        if(*beg == ivel)
            break;
        else
            ++beg;
    }
    if (beg != end)
        return true;
    else
        return false;//做查找的时候是前包后不包的
}

例2:

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>

//所有的迭代器接口都是一样的
//c++经常使用迭代器
//vector和deque有额外的功能
//是迭代器操作失效
//顺序容器和关联容器

using namespace std;
//容器都在std名称空间里

vector<int>::iterator findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//返回的结果是一个迭代器
{
    while (beg != end)
    {
        if (*beg == ivel)
            break;
        else
            ++beg;
    }
    return beg;//beg不是end就是找到了
}


int main()
{
    //用数组创建向量
    int a1[] = {0, 1, 2, 3, 4, 5, 6};
    vector<int> ivec(a1, a1 + 7);//把数组的名称传过去就成了迭代器,写多一个

    vector<int>::iterator ans = findInt(ivec.begin(), ivec.end(), 55);
    if (ans == ivec.end())
        cout << "error" << endl;
    else
        cout << "OK" << endl;

    vector<int> a;
    deque<int> b;
    list<int> c;

    a.push_back(0);
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a.push_back(4);

    auto iter1 = a.begin();//或者是vector<int>::iterator iter1=a.begin();
    cout << *iter1 << endl;
    auto iter2 = a.end();//end指向最后一个的下一个,特殊的位置,标记,范围是不包括end的
    //auto m = iter2+ a.size / 2;//指向中间的那个数


    system("pause");
    return 0; 
}

例3:

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>

//所有的迭代器接口都是一样的
//c++经常使用迭代器
//vector和deque有额外的功能
//是迭代器操作失效
//顺序容器和关联容器

using namespace std;
//容器都在std名称空间里

vector<int>::iterator findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//返回的结果是一个迭代器
{
    while (beg != end)
    {
        if (*beg == ivel)
            break;
        else
            ++beg;
    }
    return beg;//beg不是end就是找到了
}


int main()
{
    //用数组创建向量
    int a1[] = {0, 1, 2, 3, 4, 5, 6};
    vector<int> ivec(a1, a1 + 7);//把数组的名称传过去就成了迭代器,写多一个

    vector<int>::iterator ans = findInt(ivec.begin(), ivec.end(), 55);
    if (ans == ivec.end())
        cout << "error" << endl;
    else
        cout << "OK" << endl;

    vector<int> a;
    deque<int> b;
    list<int> c;

    vector<string> svec;
    string str;


    while (cin >> str)
        svec.push_back(str);

    for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
    {
        cout << *iter << endl;
    }

    //auto iter1 = a.begin();//或者是vector<int>::iterator iter1=a.begin();
    //cout << *iter1 << endl;
    //auto iter2 = a.end();//end指向最后一个的下一个,特殊的位置,标记,范围是不包括end的
    auto m = iter2+ a.size / 2;//指向中间的那个数


    system("pause");
    return 0; 
}

4,

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>

using namespace std;

vector<int>::iterator findInt(vector<int>::iterator beg, vector<int>::iterator end, int ivel)//返回的结果是一个迭代器
{
    while (beg != end)
    {
        if (*beg == ivel)
            break;
        else
            ++beg;
    }
    return beg;//beg不是end就是找到了
}


int main()
{
    list<string> slst;
    string str;


    while (cin >> str)
        slst.push_back(str);

    cout << "now list:" << endl;
    for (list<string>::iterator iter = slst.begin(); iter != slst.end(); ++iter)
    {
        cout << *iter << endl;
    }

    system("pause");
    return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值