学习笔记(09):c++入门到精通教程 c++11/14/17-迭代器精彩演绎,失效分析及弥补、实战...

立即学习:https://edu.csdn.net/course/play/9186/191693?utm_source=blogtoedu

// project8.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>

using namespace std;
struct conf 
{
    char itemname[40];
    char itemcontent[100];

};

char* getinfo(vector<conf*>& conflist,const char* pitem)
{
    for (auto pos = conflist.begin(); pos != conflist.end(); ++pos)
    {
        if (_stricmp((*pos)->itemname, pitem) == 0)
        {
            return (*pos)->itemcontent;
        }
    }
    return nullptr;
}
int main()
{
    //SeverName = 1区; // 表示服务器名称;
    // SeverID = 100000; // 表示服务器ID;
    conf* pconf1 = new conf;
    strcpy_s(pconf1->itemname, sizeof(pconf1->itemname), "SeverName");
    strcpy_s(pconf1->itemcontent, sizeof(pconf1->itemcontent), "1区");

    conf* pconf2 = new conf;
    strcpy_s(pconf2->itemname, sizeof(pconf2->itemname), "SeverID");
    strcpy_s(pconf2->itemcontent, sizeof(pconf2->itemcontent), "100000");

    vector<conf*> conflist;
    conflist.push_back(pconf1);    //[0]
    conflist.push_back(pconf2);//[1]

    char* p_tmp = getinfo(conflist, "SeverName");
    if (p_tmp != nullptr)
    {
        cout << p_tmp << endl;
    }

    //我们要释放内存
    std::vector<conf*>::iterator pos;
    for (pos = conflist.begin(); pos != conflist.end(); ++pos)
    {
        delete(*pos); //*pos才是那个值
    }

    conflist.clear();
    //vector<student> sv;
    //student mystu;
    //mystu.num = 100;
    //sv.push_back(mystu); // 把对象mystu赋值到了sv容器中;

    //vector<student>::iterator iter;
    //iter = sv.begin(); //指向第一个元素
    //cout << (*iter).num << endl;
    //cout << iter->num << endl;
    /*const vector<int> iv = { 100,200,300 };*/
    /*vector<int> iv = { 100, 200, 300 };*/
    //vector<int>::const_iterator iter;
    //for (iter = iv.begin(); iter != iv.end(); ++iter)
    //{
    //    //*iter = 4; //报错 
    //    cout << *iter << endl;
    //}

    //5.1 cbegin和cend
    //for (auto iter = iv.cbegin(); iter != iv.cend(); ++iter)
    //{
    //    cout << *iter << endl;
    //}

    //迭代器失效
    //vector<int> vecvalue{ 1,2,3,4,5 };
    /*for (auto vecitm : vecvalue)
    {
        vecvalue.push_back(888);
        cout << vecitm << endl;
    }*/
    /*for (auto beg = vecvalue.begin(), end = vecvalue.end(); beg != end; ++beg)
    {
        vecvalue.push_back(888);
        cout << *beg << endl;
    }*/
    //改进 
    //


    //迭代器失效(灾难程序崩溃)
    //vector<int>vecvalue = {1,2,3,4,5};
    //auto beg = vecvalue.begin();
    //auto end = vecvalue.end();
    //while (beg != end)
    //{
    //    cout << *beg << endl;
    //    vecvalue.insert(beg, 80);
    //    break;
    //    ++beg;
    //}

    //beg = vecvalue.begin();
    //end = vecvalue.end();
    //while (beg != end)
    //{
    //    cout << *beg << endl;
    //    ++beg;
    //}

    //auto beg = vecvalue.begin();
    //int icount = 0;
    //while (beg != vecvalue.end()) //随时更新end
    //{
    //    beg = vecvalue.insert(beg, icount +80); //insert的返回结果就要接着
    //    icount++;
    //    if (icount > 10)
    //        break;
    //    ++beg;
    //}
    //
 //   beg = vecvalue.begin();
    //auto end = vecvalue.end();
    //
    //while (beg != end) //随时更新end
    //{
    //    
    //    cout << *beg << endl;
    //    ++beg; //不要忘记放在末尾
    //}

    //正确方法演示
    //vector<int> iv = {100,200,300};
    //while (!iv.empty())
    //{
    //    auto iter = iv.begin(); //因为不为空,所以返回begin()是没有问题的
    //    iv.erase(iter); //删除该位置上的元素;
    //}

    //范例演示: 用迭代器遍历一下string类型数据
    /*string str("I Love China!");*/
    /*    for (auto iter = str.begin(); iter != str.end(); ++iter)
        {
            *iter = toupper(*iter);
        }
        cout << str << endl;*/

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值