Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;使用映射对记事本字符进行排序_C++ - Fatal编程技术网

C++ c++;使用映射对记事本字符进行排序

C++ c++;使用映射对记事本字符进行排序,c++,C++,我希望通过使用映射对文本文件进行排序,以查找文本文件中每个字符的数量。我想我已经很接近了 void CharStatistic(string filename) { ifstream infile; infile.open(filename); char x; int i; map <char, int> count; while (infile >> x) { count[x]++;

我希望通过使用映射对文本文件进行排序,以查找文本文件中每个字符的数量。我想我已经很接近了

void CharStatistic(string filename)
{
    ifstream infile;
    infile.open(filename);
    char x;
    int i;
    map <char, int> count;
    while (infile >> x)
    {
        count[x]++;
        for (auto it = count.begin(); it != count.end(); it++)
            cout << it->first << it->second << endl;

    }
}
如果可能的话,我也想弄清楚如何打印出出现次数最多的字符。一如既往,非常感谢您的帮助

  • 不要每次在循环中打印map的值
  • 要按顺序打印最高频率,请将地图内容转储到std::pair的“std::vector”中,并使用比较器根据pair的第二个元素比较值对其进行排序
  • 最后显示排序后的向量
      • 不要每次在循环中打印map的值
      • 要按顺序打印最高频率,请将地图内容转储到std::pair的“std::vector”中,并使用比较器根据pair的第二个元素比较值对其进行排序
      • 最后显示排序后的向量

        • 在这种情况下,我不会使用
          地图。由于只有256个值,一个简单数组只占用1KB(假设每个计数有4个字节)。即使输入可能是稀疏的,因此该数组中的某些元素在仅1K时未使用,但使用不同的结构不太可能获得任何好处


          完成计数后,转换为可以按计数排序的结构数组非常容易(或者,如果您确实只需要计数最大的结构,只需对其进行线性扫描)。

          在这种情况下,我不会使用
          映射。由于只有256个值,一个简单数组只占用1KB(假设每个计数有4个字节)。即使输入可能是稀疏的,因此该数组中的某些元素在仅1K时未使用,但使用不同的结构不太可能获得任何好处

          完成计数后,转换为可以按计数排序的结构数组非常容易(或者,如果您确实只需要计数最大的结构,只需对其进行线性扫描即可)。

          更正代码

          void CharStatistic(string filename) {
              ifstream infile(filename);
              if (!infile.is_open()) return;    // Check if file opened correctly
              char x;
              map<char, int> count;
              while (infile >> x) count[x]++;
              for (auto it : count)    // Pull this out of the while loop
                  cout << it.first << " " << it.second << endl;
          }
          
          更正您的代码

          void CharStatistic(string filename) {
              ifstream infile(filename);
              if (!infile.is_open()) return;    // Check if file opened correctly
              char x;
              map<char, int> count;
              while (infile >> x) count[x]++;
              for (auto it : count)    // Pull this out of the while loop
                  cout << it.first << " " << it.second << endl;
          }
          

          扫描map
          count
          ,并找到最大值的一个。您的代码统计了单个非空白字符的频率,但“查找排序”文件到底是什么意思?按字符排序很少有用/需要,这真的是你的意思吗。。。类似于
          aaabbccccfhh…aaaaaaaabbbccee
          ?如果不是,那是什么?我是说我想看看每个角色有多少个。像一个1 b 3 c 24等。。。希望清除它,可以向上扫描map
          count
          ,并找到具有最大值的一个。您的代码计算了单个非空白字符的频率,但“查找排序”文件到底是什么意思?按字符排序很少有用/需要,这真的是你的意思吗。。。类似于
          aaabbccccfhh…aaaaaaaabbbccee
          ?如果不是,那是什么?我是说我想看看每个角色有多少个。像一个1 b 3 c 24等。。。当我把cout行从循环中取出时,我希望这会清除迭代器的错误。抱歉,我对这东西有点陌生。@SaladSnake:移动
          for
          循环和
          cout
          行。谢谢tony和P0W,效果很好。我觉得自己很笨,哈哈,一个简单的错误。当我把cout行从循环中取出时,迭代器出现了一个错误。抱歉,我对这东西有点陌生。@SaladSnake:移动
          for
          循环和
          cout
          行。谢谢tony和P0W,效果很好。我觉得自己很傻哈哈,一个简单的错误。我需要一个图书馆来放那个最大发生点吗?我在“*”@saldsnake
          #include
          上没有得到合适的转换函数错误。我需要一个库来存储最大出现位吗?我在“*”@saldsnake
          #include
          上没有得到合适的转换函数错误。
          int max = *max_element(count.begin(), count.end(), count.value_comp());