【问题标题】:how to find the maximum occurrence of a string in string array in c++如何在c ++中找到字符串数组中字符串的最大出现次数
【发布时间】:2021-01-22 16:49:08
【问题描述】:

我有一个类似的字符串数组

{"A", "B", "AA", "ABB", "B", "ABB", "B"}

如何在 c++ 中找到像这样的数组中出现次数最多的字符串("B")?

谢谢

【问题讨论】:

  • 您是如何发现"B" 在您的示例中最常出现的?这就是你的做法;)。请出示您的代码。无论它多么破碎或不完整,它都可以帮助我们为您提供帮助。
  • 抱歉,SO 不是代码编写服务。你有没有尝试过任何东西?您遇到了哪些具体问题?
  • 我只是做了一个简单的数组作为例子。
  • 可能的重复项中有this question
  • @Jithin D Mathew 数组元素的类型是什么?那是显示如何声明数组。类型的元素是 std::string 还是 const char *?

标签: c++ arrays string max multiple-occurrence


【解决方案1】:

如果数组元素的类型为std::string,那么在不使用额外内存资源的情况下,查找数组中出现频率更高的元素的函数可以如下所示,如下面的演示程序所示。

#include <iostream>
#include <string>

size_t max_occurence( const std::string a[], size_t n )
{
    size_t max = 0;
    size_t max_count = 0;
    
    for ( size_t i = 0; i != n; i++ )
    {
        size_t j = 0;
        
        while ( j != i && a[j] != a[i] ) ++j;
        
        if ( j == i )
        {
            size_t count = 1;
            while ( ++j != n )
            {
                if ( a[j] == a[i] ) ++count;
            }
            
            if ( max_count < count )
            {
                max = i;
                max_count = count;
            }
        }
    }
    
    return max;
}

int main() 
{
    std::string a[] = { "A", "B", "AA", "ABB", "B", "ABB", "B" };
    
    auto max = max_occurence( a, sizeof( a ) / sizeof( *a ) );
    
    std::cout << "The most often encountered string is " << a[max] << '\n';

    return 0;
}

程序输出是

The most often encountered string is B

另一种方法是编写通用模板函数。例如

#include <iostream>
#include <string>
#include <functional>
#include <iterator>

template <typename ForwardIterator, 
          typename BinaryPredicate = std::equal_to<typename std::iterator_traits<ForwardIterator>::value_type>>
ForwardIterator max_occurence( ForwardIterator first, 
                               ForwardIterator last, 
                               BinaryPredicate binary_predicate = BinaryPredicate() )
{
    auto max = first;
    typename std::iterator_traits<ForwardIterator>::difference_type max_count = 0;
    
    for ( auto current = first; current != last; ++current )
    {
        auto prev = first;
        
        while ( prev != current && !binary_predicate( *prev, *current ) ) ++prev;
        
        if ( prev == current )
        {
            typename std::iterator_traits<ForwardIterator>::difference_type count = 1;
            
            while ( ++prev != last )
            {
                if ( binary_predicate( *prev, *current ) ) ++count;
            }
            
            if ( max_count < count )
            {
                max = current;
                max_count = count;
            }
        }
    }
    
    return max;
}


int main() 
{
    std::string a[] = { "A", "B", "AA", "ABB", "B", "ABB", "B" };
    
    auto max = max_occurence( std::begin( a ), std::end( a ) );
    
    std::cout << "The most often encountered string is " << *max << '\n';

    return 0;
}

程序输出同上图

The most often encountered string is B

【讨论】:

  • 哇。您可能需要考虑包含一个使用 std::map&lt;string, int&gt; 的示例,其中 string 是读取的字符串,int 是频率。我相信它会比你的前两个例子短。
  • @ThomasMatthews 使用这种方法至少有一个缺点是您不会获得指向数组目标元素的迭代器。
【解决方案2】:

这是一个使用 std::map 的示例。

typedef std::map<std::string, int> Frequency_Map;
int main()
{
  const std::vector<std::string> test_data =
  {
      "A", "B", "AA", "ABB", "B", "ABB", "B",
  };

  Frequency_Map    frequency_table;
  std::string s;
  const size_t length = test_data.length();
  for (unsigned int i = 0; i < length; ++i)
  {
      Frequency_Map::iterator iter(frequency_table.find(test_data[i]);
      if (iter != frequency_table.end())
      {
          ++(iter->second);
      }
      else
      {
          frequency_table[test_data[i]] = 1;
      }
  }

  for (Frequency_Map::iterator iter = frequency_table.begin();
       iter != frequency_table.end();
       ++iter)
  {
      std::cout << iter->first << "    " << iter->second << "\n";
  }
  return 0;
}

上面的代码使用std::map 构建了一个频率表,然后输出字符串及其频率。上面的代码可以很容易地修改为找到具有最大(最大)频率的字符串。

【讨论】:

  • 不需要map::find++frequency_table[test_data[i]] 就足够了。
  • 类型定义? map.find?没有初始化变量?没有结构化的输出绑定?哇。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多