簡體   English   中英

C ++列表中最頻繁的元素,STL?

[英]Most frequent element in a C++ List, STL?

我有一個程序,可以在C ++ STL列表中輸入一些數字。 我想找到列表中最常出現的元素。 我的問題是,由於程序無法按預期運行,我在做什么錯誤的方式?

// List.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
char number;
int counter;
list <char> l;
vector <int> m;

list <char>::iterator START;
void countRepetition();
int main()
{
    do {
        number = getchar();
        if (number != '0') {
            l.push_back(number);
        }
    } while (number != '0');

    /*for (START = l.begin(); START != l.end(); START++) {
        m.push_back(countRepetition(*START));
    }

    for (int i = 0; i < m.size(); i++) {
        cout << m[i] << endl;
    }
    */
     countRepetition();
    auto x = max_element(m.begin(), m.end());
    cout << *x << endl;
    return 0;
}
void countRepetition() {
    for (auto i = l.begin(); i != l.end(); i++) {
        for (auto j = l.begin(); j != l.end(); j++) {
            if (*i == *j) {
                counter++;
            }
            m.push_back(counter);
        }
    }
}

我認為您想要的是這樣的:

void countRepetition() {
    for (auto i = l.begin(); i != l.end(); i++) {
        int counter = 0;
        for (auto j = l.begin(); j != l.end(); j++) {
            if (*i == *j) {
                counter++;
            }
        }
        m.push_back(counter);
    }
}

我進一步建議對函數使用m和l參數: void countRepetition(const list<char>& l, vector<int>& m)

void countRepetition(const list<char>& l, vector<int>& m) {
    for (auto i = l.begin(); i != l.end(); i++) {
        int counter = 0;
        for (auto j = l.begin(); j != l.end(); j++) {
            if (*i == *j) {
                counter++;
            }
        }
        m.push_back(counter);
    }
}

編輯:(感謝papagaga)

使用地圖可以更好地解決這個問題:

void countRepetition(const list<char>& l, map<char, int>& m) {
    for(const auto& element : l){
        ++m[element];
    }
}

您還可以使用簡單的for_each +地圖容器算法來計算唯一外觀。

#include<iostream>
#include<list>
#include<map>
#include<algorithm> 
using namespace std;

int main() 
{
    list <char> Lst;
    map<char,int16_t> MapCounter;

    //Fill list by data
    char Arr[] {1,1,2,3,4,5,6,7,4,2,1};
    back_insert_iterator<decltype(Lst)> InsListIt(Lst);
    copy(&Arr[0], &Arr[12], InsListIt);

    //Calc unique appearance of elements. Store results in MapCounter of all unique elements apperances
    for_each(Lst.begin(), Lst.end(), [&MapCounter](int val){ MapCounter[val]++; });

    //Calc element that appears max frequeantly times in list
    char MaxElement = 0;
    int16_t MaxRepeat = 0;
    for_each(MapCounter.begin(), MapCounter.end(), [&MaxElement, &MaxRepeat](pair<char, int16_t> el)
    {
        if ( MaxRepeat < el.second )
        {
            MaxElement = el.first;
            MaxRepeat = el.second;
        }
    });

    return 0; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM