1

ファイルからデータを取得し、それをベクターに入れ、ベクター内で最も頻繁に使用される要素をチェックするプログラムを作成しました。(マップを使用)問題は、データに同じ数の要素がある場合です(2つのElement1、2つのElement2、1つのElement3)。Element1 が返され、「最も頻繁に使用される要素がない」という情報を渡す必要があります。私のコードは次のようになります:

using namespace std;

bool comp(const pair<string, unsigned long> &pair1,
        const pair<string, unsigned long> &pair2) {
    return pair1.second < pair2.second;
}

string Odczyt::tokenizer() {

    inFile.open("baza.txt");

    while (!inFile.eof()) {
        for (int i = 0; i < 4; i++) {
            inFile >> row1[i] >> row2[i] >> row3[i] >> row4[i];
        }
    }

    sVector1.assign(row1, row1 + 3);

    string w1 = most_occurred(sVector1);

    return w1;

}

string Odczyt::most_occurred(vector<string> &vec) {

    map<string, unsigned long> str_map1;

    for (vector<string>::const_iterator it = vec.begin(); it != vec.end();
            ++it) {
        ++str_map1[*it];
    }

    return max_element(str_map1.begin(), str_map1.end(), comp)->first;
}
4

3 に答える 3

1

m 回出現する要素を見つけた回数を格納する変数を作成します (m は要素が出現した現在の最大回数です)。アルゴリズムの終了点で複数の要素が m 回出現する場合、最も頻繁に出現する要素は 1 つも存在しないことがわかります。

于 2013-04-30T15:40:21.140 に答える
0
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

string most_occurred(vector<string> &vec) {
    map<string, unsigned long> str_map1;
    std::string max_element = "";
    int max_count = 0;

    typedef vector<string>::const_iterator iter;
    iter end = vec.end();

    for (iter it = vec.begin(); it != end; ++it) {
        int count = ++str_map1[*it];
        if(count == max_count) max_element = "no max found";
        if(count >  max_count) {
            max_count = count;
            max_element = *it;
        }
    }

    return max_element;
}

int main() {
    std::string arr1[5] = {"aa" , "bb", "cc", "dd", "ee"}; // no max found
    std::string arr2[5] = {"aa" , "aa", "cc", "dd", "ee"};// aa
    std::string arr3[5] = {"aa" , "aa", "cc", "cc", "ee"}; // no max found
    std::vector<std::string> input1(arr1, arr1+5);
    std::vector<std::string> input2(arr2, arr2+5);
    std::vector<std::string> input3(arr3, arr3+5);
    std::cout << most_occurred(input1) << std::endl;
    std::cout << most_occurred(input2) << std::endl;
    std::cout << most_occurred(input3) << std::endl;
}

結果は次のとおりです。

no max found
aa
no max found

次のテストの結果はno max found.

int main() {
    std::string arr1[24] = {"Element1", "Element2", "Element33", "1",
    "Element1", "Element2", "Element33", "2", "Element11", "Element2",
    "Element33", "2", "Element11" "Element21" "Element31", "2", "Element11",
    "Element21", "Element31", "1", "Element12", "Element21", "Element31",
    "1"}; // no max found
    std::vector<std::string> input1(arr1, arr1+24);
    std::cout << most_occurred(input1) << std::endl;
}

上記のコードが std::string("") を返す場合、max 要素はありませんでした。それ以外の場合は、max を返します。

于 2013-04-30T16:03:35.150 に答える
0

これは頻繁に行われる操作で、サンプル コードは次のとおりです。

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

using namespace std;

int main(int argc, char* argv[]) {
   // number -> frequency
   map<int, int> idfreq = {{1, 3}, {2, 10}, {3,8}, {9, 15}, {7,30}};
   vector<pair<int, int> > v;
   copy(idfreq.begin(), idfreq.end(), back_inserter(v));
   for (auto& el : v) {
      cout << el.first << " " << el.second << endl;
   }
   cout << endl;
   make_heap(v.begin(), v.end(), [](const pair<int,int> &a, const pair<int,int> &b){ return a.second < b.second; });
   // with -std=c++14
   //make_heap(v.begin(), v.end(), [](auto& a, auto& b){ return a.second < b.second; });
   cout << v.front().first << " " << v.front().second << " most frequent element\n";
   cout << "after make heap call\n";
   for (auto& el : v) {
      cout << el.first << " " << el.second << endl;
   }
   cout << endl;

   return 0;
}
于 2017-03-10T19:22:36.860 に答える