zoukankan      html  css  js  c++  java
  • erase操作

    #include<iostream>
    #include <vector>
    int main()
    {
        std::vector<int> vec;
        vec.push_back(1);
        vec.push_back(2);
        vec.push_back(4);
        vec.push_back(4);
        vec.push_back(5);
        for (auto i = vec.begin(); i != vec.end(); ++i)
        {
            if (*i == 4)
            {
                vec.erase(i);
            }
        }
        getchar();
        return 0;
    }

    如果你这样试图删除为4的元素则达不到效果,因为erase后vec改变迭代器失效,但erase是返回下一个迭代器的

    #include<iostream>
    #include <vector>
    int main()
    {
        std::vector<int> vec;
        vec.push_back(1);
        vec.push_back(2);
        vec.push_back(4);
        vec.push_back(4);
        vec.push_back(5);
        for (auto i = vec.begin(); i != vec.end();)
        {
            if (*i == 4)
            {
                i = vec.erase(i);//返回被删的下一个迭代器,可以利用这个特性
            }
            else
            {
                i++;
            }
        }
        getchar();
        return 0;
    }
  • 相关阅读:
    3.springMVC参数绑定过程(页面向后台传参)
    2.springMVC入门程序
    1.理解springMVC的原理
    RTO
    DC Congestion Control
    docs for DC Network
    FCT和QCT
    下行TM
    上行TM
    调度与队列
  • 原文地址:https://www.cnblogs.com/zzyoucan/p/3674434.html
Copyright © 2011-2022 走看看