vector iterators incompatible

在刷leetcode无重复字符的最长子串题目过程中遇到的问题
报错如下:
vector iterators incompatible_第1张图片
vector iterators incompatible_第2张图片
报错代码段如上,检查代码后发现有逻辑错误,迭代器it在第一个if语句中,如果没有找到num[i]的话,就指向了ZiChuan最后一个字符的下一个位置;但是我接下来做了一个容器插入的操作(push.back()),再接着又用迭代器it去做比较,但是当对容器进行插入操作后,先前获得的迭代器已经无效。
另外,我的代码逻辑上也有问题,我的本意是没找到num[i]字符的话就把num[i]推进ZiChuan数组中,直到在ZiChuan数组中找到相同的num[i],然后就把截至到这里的ZiChuan数组整个推进map中存起来。但这段代码中,会在没找到num[i]之后插入num[i],导致下一次做比较的时候ZiChuan数组中就必然存在了num[i],也就必然会被找到,必然会执行此if语句的内容,导致结果出错。
完整的错误代码如下:

#include 
#include 
#include 
#include 
#include 


void shuzuprint(std::vector<std::string>& sz)
{
    std::vector<std::string>::iterator it = sz.begin();
    std::cout << "[";
    for (; it != sz.end(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << "]";

}


class Solution 
{
public:
    //void shuzuprint(std::vector sz);
    int lxu(std::vector<std::string>& num) 
    {
        //输出原始字符串
        for (int i = 0; i < num.size(); i++)
        {
            std::cout << num[i];
        }
        std::cout << std::endl;
        //判断原始字符串是否为空
        if (num.empty())
        {
            std::cout << 0;
            return 0;
        }

        std::unordered_map<int, std::vector<std::string>> str;
        std::vector<std::string> ZiChuan;
        int n = num.size();
        int length = 1;
        int MaxLength = 0;
        
        ZiChuan.push_back(num[0]);
        std::vector<std::string>::iterator it = std::find(ZiChuan.begin(), ZiChuan.end(), num[0]);
        for (int i = 1; i < n; i++)
        {
            //it++;
            
            if (std::find(ZiChuan.begin(), ZiChuan.end(), num[i]) == ZiChuan.end())
            {

                ZiChuan.push_back(num[i]);
                length++;
                //std::cout << length;
                
            }
            if(it !=ZiChuan.end())
            {
                if (length > MaxLength)
                {
                    MaxLength = length;
                    
                }
                
                str.emplace(make_pair(length, ZiChuan));
                std::vector<std::string>().swap(ZiChuan);
                if(i!=n-1)
                {
                    ZiChuan.push_back(num[i]);
                
                }
                
                length = 1;
            }
            if (i == n - 1)
            {
                if (length > MaxLength)
                {
                    MaxLength = length;
                    std::cout << MaxLength;
                }
                //shuzuprint(ZiChuan);
                str.emplace(make_pair(length, ZiChuan));
                std::vector<std::string>().swap(ZiChuan);
            }
        

        }


        /*
        ZiChuan.push_back(str[MaxLength]);
        std::string s = str[MaxLength];

        std::cout << "最长字符串:" << s;
        */
        std::cout << "最长字符长度:" << MaxLength;
 
        return 0;
    }
    
};
// Solution::


int main()
{
    Solution slu;
    std::string s;
    std::vector<std::string> nums;
    
    while (true)
    {
        std::cin >> s;
        nums.push_back(s);
        if (std::cin.get() == '\n') break;
    }

    slu.lxu(nums);
    

}

应修改成如下代码:
vector iterators incompatible_第3张图片

#include 
#include 
#include 
#include 
#include 


void shuzuprint(std::vector<std::string>& sz)
{
    std::vector<std::string>::iterator it = sz.begin();
    std::cout << "[";
    for (; it != sz.end(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << "]";

}


class Solution 
{
public:
    //void shuzuprint(std::vector sz);
    int lxu(std::vector<std::string>& num) 
    {
        //输出原始字符串
        for (int i = 0; i < num.size(); i++)
        {
            std::cout << num[i];
        }
        std::cout << std::endl;
        //判断原始字符串是否为空
        if (num.empty())
        {
            std::cout << 0;
            return 0;
        }

        std::unordered_map<int, std::vector<std::string>> str;
        std::vector<std::string> ZiChuan;
        int n = num.size();
        int length = 1;
        int MaxLength = 0;
        
        ZiChuan.push_back(num[0]);
        //std::vector::iterator it = std::find(ZiChuan.begin(), ZiChuan.end(), num[0]);
        for (int i = 1; i < n; i++)
        {
            //it++;
            //it = std::find(ZiChuan.begin(), ZiChuan.end(), num[i]);
            if (std::find(ZiChuan.begin(), ZiChuan.end(), num[i]) == ZiChuan.end())
            {

                ZiChuan.push_back(num[i]);
                length++;
                //std::cout << length;
                
            }
            else
            {
                if (length > MaxLength)
                {
                    MaxLength = length;
                    
                }
                
                str.emplace(make_pair(length, ZiChuan));
                std::vector<std::string>().swap(ZiChuan);
                if(i!=n-1)
                {
                    ZiChuan.push_back(num[i]);
                
                }
                
                length = 1;
            }
            if (i == n - 1)
            {
                if (length > MaxLength)
                {
                    MaxLength = length;
                    std::cout << MaxLength;
                }
                //shuzuprint(ZiChuan);
                str.emplace(make_pair(length, ZiChuan));
                std::vector<std::string>().swap(ZiChuan);
            }
        

        }


        /*
        ZiChuan.push_back(str[MaxLength]);
        std::string s = str[MaxLength];

        std::cout << "最长字符串:" << s;
        */
        std::cout << "最长字符长度:" << MaxLength;
 
        return 0;
    }
    
};
// Solution::


int main()
{
    Solution slu;
    std::string s;
    std::vector<std::string> nums;
    
    while (true)
    {
        std::cin >> s;
        nums.push_back(s);
        if (std::cin.get() == '\n') break;
    }

    slu.lxu(nums);
    

}

你可能感兴趣的:(c++)