【问题标题】:C++ most frequent element in mapC ++地图中最常见的元素
【发布时间】:2013-04-24 14:16:37
【问题描述】:

我制作了一个程序,它从文件中获取数据,将其放入向量中,然后检查向量中最常见的元素。 (使用地图) 问题是当我在数据中有相同数量的元素时(两个 Element1、两个 Element2、一个 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;
}

【问题讨论】:

  • 你打破平局的逻辑应该是什么?你是什​​么都不还,还是所有关系都还?
  • 我只需要最频繁的元素,如果有任何或没有的信息。

标签: c++ arrays map


【解决方案1】:

创建一个变量来存储你找到一个元素出现 m 次的次数(其中 m 是当前任何元素出现的最大次数)。如果在算法的终止点你有不止一个元素出现 m 次,那么你知道没有一个最频繁出现的元素。

【讨论】:

  • 我一直在尝试这样做,但我真的找不到如何从地图中获取我需要的信息
【解决方案2】:
#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("") 则没有最大元素,否则返回最大值。

【讨论】:

  • 我一直在玩你最近几分钟给我的答案。它不输入 if(count == max_count) max_element = "";部分。它仍然找到最频繁的元素。我的测试数据文件如下所示: Element1 Element2 Element33 1 Element1 Element2 Element33 2 Element11 Element2 Element33 2 Element11 Element21 Element31 2 Element11 Element21 Element31 1 Element12 Element21 Element31 1
  • @user2336450 我测试了上面的代码,它按我的预期工作。
  • @user2336450 我刚刚测试了您的输入,但没有找到最大值。您确定正确解析文件吗?
  • 我刚刚检查了您的代码和手动数组。它必须是从文件中解析的东西。我将一一查看。谢谢你的帮助。
  • 这是一个可怕的分段错误错误。修复它并且它正在工作。再次感谢您的帮助。
【解决方案3】:

这是一个频繁的操作这里是我的示例代码:

#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;
}

【讨论】:

    猜你喜欢
    • 2018-09-27
    • 2018-09-11
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多