我需要在字符串数组中找到出现次数最多的元素。我不确定该怎么做,因为我对此没有太多经验。我不知道指针/哈希表。
我已经为整数做了这个,但我不能让它适用于字符串。
我的版本:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a[]={1,2,3,4,4,4,5};
int n = sizeof(a)/sizeof(int );
int *b=new int [n];
fill_n(b,n,0); // Put n times 0 in b
int val=0; // Value that is the most frequent
for (int i=0;i<n;i++)
if( ++b[a[i]] >= b[val])
val = a[i];
cout<<val<<endl;
delete[] b;
return 0;
}
感谢您在字符串数组中查找最频繁出现的元素的任何帮助!