尝试将多个模式读入矢量,然后将它们打印出来

时间:2017-02-13 04:02:16

标签: c++ vector

基本上我试图在向量中找到模式,例如,如果向量是1 1 1,它只返回无模式。它还需要能够读取最多两种可能的模式,教授说不要担心超过2种模式。任何建议都会非常有帮助。 此外,空洞calcMode也是它的一部分,它不会让我将它添加到代码中,我不知道如何更改它。

void calcMode(vector <double> const& vec)
{
    int counter = 1;
    int max = 0;
    vector<double> mode;
    for (int pass = 0; pass < vec.size() - 1; pass++)
        {
            if (vec[pass] == vec[pass + 1])
            {
                counter++;
                // If they are the same number add it to the mode vector
                if (counter > max)
                {
                    mode.clear();
                    max = counter;
                    mode.push_back(vec[pass]);
                }
                // if it is greater clear the vector and assign it the new value
                else if (counter == max)
                {
                    mode.push_back(vec[pass]);
                }

            }
            else
                counter = 1; // reset counter.
        }
    // print out the freq and the mode(s)
    cout << mode.size() << endl;
    cout << "Mode: " << setw(25) << setprecision(3);

    cout << setw(25) << setprecision(3);

    if (vec.size() == 1)
    {
        cout << "Freq = " << counter << endl;
        cout << vec[0] << endl;
    }
    else if (vec.size() == 2)
    {
        cout << "Freq = " << counter << endl;
        cout << vec[0] << vec[1] << endl;
    }
    else
        cout << "No mode" << endl;
}

1 个答案:

答案 0 :(得分:0)

尽可能使用标准库以及有意义的地方。

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

// comparator function. When you compare two std::pair, you get both sides 
// in the compare. We only want the value side, so we define a function to 
// only test the value side.  
static bool comp(std::pair<unsigned,unsigned> lhs,
                 std::pair<unsigned,unsigned> rhs)
{
    return (lhs.second < rhs.second);
}

int main()
{
    std::vector <double> const& vec {0, 1, 1, 2, 2, 4, 3, 2, 4, 0, 1, 3};
    std::map<double, int> freq;

    // build frequency count
    for (double val:vec) 
    {
        freq[val]++;
    }
    // or 
    //std::for_each(vec.begin(), vec.end(), [&freq](double val){ freq[val]++; });
    // not sure if using for_each makes sense here. I don't think we get much

    // find highest frequency
    std::map<double, int>::iterator found = std::max_element(freq.begin(),
                                                             freq.end(),
                                                             comp);
    // cache what we found so we have it later
    std::pair<double, int> mode = *found;
    std::cout << found->first << " freq: "  << found->second << '\n';
    // remove so we don't find again
    freq.erase(found);

    // look for next highest frequency
    found = std::max_element(freq.begin(), freq.end(), comp);
    // test for same frequency
    if (mode.second == found->second)
    {
        std::cout << found->first << " freq: "  << found->second << '\n';
    }
}

std::max_element documentation

std::for_each documentation