最受欢迎的元素(CPP)

时间:2016-05-25 14:34:48

标签: c++

嘿伙计们,我正试图找到一种方法来获取矢量中最常用的元素但由于某种原因它似乎没有工作并返回1,任何帮助都将不胜感激。

从我能看到的情况来看,我认为我的想法是正确的,但我猜不是,谢谢!

    std::sort(Track5Guess.begin(), Track5Guess.end());


    temp1 = Track5Guess.at(0);


    for (i = 1; i < Track5Guess.size() - 1; i++)
    {
        if (track == false)
        {
            if (Track5Guess.at(i) == temp1)
            {
                counter1++;
            }
            else
            {
                temp2 = Track5Guess.at(i);
                track = true;
                i++;
            }
            if (track == true)
            {
                if (Track5Guess.at(i) == temp2)
                {
                    counter2++;
                }
                else
                {
                    if (counter2 >= counter1)
                    {
                        temp1 = temp2;
                        track = false;
                        counter1 = counter2;
                        counter2 = 0;
                        temp2 = 0;
                    }

                    if (counter1 > counter2)
                    {
                        track = false;
                        counter2 = 0;
                        temp2 = 0;
                    }

                }
            }


        }
    }
    if (i == Track5Guess.size() - 1)
    {
        Num2Guess5 = temp1;
        temp1 = 0;
        counter1 = 0;
    }

0 个答案:

没有答案