【问题标题】:Find number of letters in a vector of strings c++查找字符串向量中的字母数c ++
【发布时间】:2012-01-31 22:33:04
【问题描述】:

我有一个字符串向量 例子: 德夫 edf 联邦调查局 海德夫

我想浏览列表并计算每个字母出现的次数

例如字母 d 出现 5 次 e 出现 5 次 f 出现 5 次 h出现1次

到目前为止,我还没有代码,但我正在尝试先看看如何使用逻辑来做到这一点。

我正在尝试编码,但不知道从哪里开始。

我在想我可以将每个字母存储到一个字符串中。 字符串将是 {dedfeedffedfhedf}

然后取出字符串并计算每次字母在该字符串中的时间 但这是我遇到问题的地方。有什么想法吗?

任何建议也将不胜感激。

谢谢

【问题讨论】:

    标签: c++ vector


    【解决方案1】:

    一般算法可能是:

    create empty array/map/storage container for counting
    for each string in the vector
       for each character in the string
           counter[character] += 1
    

    【讨论】:

      【解决方案2】:

      你可以有一个数组来保存每个字母的计数。如果我们只假设字母表,您将拥有一个由 26 个元素(可能是整数)组成的数组,全部初始化为 0。然后您可以遍历每个字符串,每次遇到一个字符时,您都会增加该计数。

      //let's call your vector of strings stringvec
      int counts[26];
      
      //initialize counts to 0
      
      //go through each string in the vector
      for (int i = 0; i < stringvec.size(); i++) {
          //get current string
          string curstr = stringvec[i];
      
          //for each letter in the string
          for (int j = 0; j < curstr.size(); j++) {
              //curstr[j] is a character at position j in the string
              //subtracting 'a' from it will give you the position in relation to 'a' in ASCII, so a character 'a' = 0, 'b' = 1, 'c' = 2, and so on...
              counts[curstr[j] - 'a']++;
          }
      }
      

      然后你可以随心所欲地处理计数。

      【讨论】:

        【解决方案3】:

        您可以通过几种方式(当然是伪代码):

        for each letter you are interested in:
            for each character in the string:
                if letter matches character:
                    increment counter
            print letter and counter
        

        declare array of counters, one for each letter
        for each character in the string:
            if character is a letter:
                increment that counter in the array
        print counters from array
        

        sort the characters in the string
        for each character in the sorted string:
            if character is a letter:
                count the number of times that letter occurs
                print letter and count
        

        这些方法中的每一种都有不同的性能特征。一些权衡空间(在计数器数组中)以换取额外的时间(嵌套循环或排序)。看看您是否可以确定哪个性能最适合您的情况。

        【讨论】:

          【解决方案4】:

          使用数组来存储字母的数量会很聪明,这样您就可以在 O(1) 中访问随机选择的字母的数量。

          int letters[26] = {0};
          ...
          char c;
          if (c >= 'a' && c <= 'z')
              letters[c - 'a']++;
          ...
          return 0;
          

          检查this lecture by Richard Buckland (video) - 15:20 开始对您有帮助的部分;)

          【讨论】:

            【解决方案5】:
            #include <iostream>
            #include <vector>
            #include <string>
            #include <unordered_map>
            
            using namespace std;
            
            typedef vector<string> StrVector;
            typedef unordered_map<char, int> CharIntMap;
            
            int main() {
                //the following code will work only with a c++11 compiler
                StrVector words = {"dedf", "eedf", "fedf", "hedf"};
                CharIntMap counts;
                for (string &word : words) {
                    for (char &letter : word) {
                        counts[letter]++;
                    }
                }
                for (auto it : counts) {
                    cout << "Count of letter " << it->first << " = " << it->second << endl;
                }
                return 0;
            }
            

            【讨论】:

            • 我想你的意思是cout &lt;&lt; "Count of letter " &lt;&lt; it.first &lt;&lt; " = " &lt;&lt; it.second &lt;&lt; "\n"; 否则,+1。
            【解决方案6】:

            您需要一个数据结构,它允许您将字母映射到计数。遍历向量,遍历字符串中的每个字符,并在映射中查找字符,并增加计数。

            【讨论】:

              猜你喜欢
              • 2022-06-12
              • 1970-01-01
              • 2021-08-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-12-10
              • 1970-01-01
              • 2015-09-22
              相关资源
              最近更新 更多