如何使用c ++中的int * count(const string& s)来计算字符串中每个数字的出现次数?

时间:2018-05-14 04:35:32

标签: c++ count type-conversion

注意:我无法在算法库

中使用地图或任何内容

我只有主要功能,但我完全迷失了我应该写的功能

#include <string>
#include <iostream>


using namespace std;


int* count(const string& s);

int main() {

    string userinput = "random word";

    int *counts = count(userinput);

    for (int i = 0; i < 11; i++) {
        cout << "Letter " << i << " occured " << counts[i] << " times.";
    }




    system("pause");

    return 0;
}



int* count(const string& s) {

    for (int i = 0; i < 11; i++)
    {
        return s[i];
    }
}

int * count函数不正确,无法编译。如何编写一个可以返回出现的函数?

2 个答案:

答案 0 :(得分:0)

  

如何使用int *计算字符串中每个数字的出现次数   在c ++中count(const string&amp; s)?注意:我无法使用地图或任何内容   算法库

有几点需要注意:

  1. 总是不好主意,如果我们不采用最简单的方法解决问题,那么老师是这种情况的推动者会感到很难过。

  2. 不需要int*。它不以任何方式提供您的解决方案。如果它是数组来计算字符串的字符,那么它可能是有意义的(即指向数组的指针)。

  3. 尽量避免使用using namespace std;

  4. 进行练习

    如果您不允许使用std::map<char, int>(提示替代解决方案),您可以执行以下操作。

    我对此有所评论,以便更好地理解。

    #include <string>
    #include <iostream>
    #include <cstddef>
    
    // consider 128 ASCII decimal and their coresponding character codes
    int charASCIIArray[128] = {0};
    
    void count_char(const std::string& s)
    {
       for(const auto& it: s)
       {
            if(('A' <= it && it <= 'Z') ||     // if char between (A,B,....,Z) or
               ('a' <= it && it <= 'z') )      //         between (a,b,....,z)
               charASCIIArray[static_cast<int>(it)]++; // we count each corresponding array then
       }
    }
    
    int main()
    {
       std::string userinput = "random words WITH *- aLl";
    
       count_char(userinput);
       for(std::size_t index = 0; index < 128; ++index)
          if(charASCIIArray[index] != 0)
            std::cout << "Letter " << static_cast<char>(index)  // convert back to char
                      << " occured " << charASCIIArray[index] << " times.\n";
    
       return 0;
    }
    

    在此处查看实时操作https://www.ideone.com/uFG2HJ

    Letter H occured 1 times.
    Letter I occured 1 times.
    Letter L occured 1 times.
    Letter T occured 1 times.
    Letter W occured 1 times.
    Letter a occured 2 times.
    Letter d occured 2 times.
    Letter l occured 1 times.
    Letter m occured 1 times.
    Letter n occured 1 times.
    Letter o occured 2 times.
    Letter r occured 2 times.
    Letter s occured 1 times.
    Letter w occured 1 times.
    
      

    更新

    受到@Fei Xiang评论的启发,这里有两个过度设计的解决方案:

    首先,返回指向动态数组的指针(这将符合实际值     问题要求):https://www.ideone.com/ouHqK4

    #include <string>
    #include <iostream>
    #include <cstddef>
    
    int* count_char(const std::string& s)
    {
        // consider 128 ASCII decimal and their coresponding character codes
        int *charASCIIArray = new int[128]{0};
        for(const auto& it: s)
        {
            if(('A' <= it && it <= 'Z') ||     // if char between (A,B,....,Z) or
               ('a' <= it && it <= 'z') )      //         between (a,b,....,z)
               charASCIIArray[static_cast<int>(it)]++; // we count each corresponding array then
        }
        return charASCIIArray;
    }
    
    int main()
    {
        std::string userinput = "random words WITH *- aLl";
    
        int *charASCIIArray = count_char(userinput);
        for(std::size_t index = 0; index < 128; ++index)
            if(charASCIIArray[index] != 0)
            std::cout << "Letter " << static_cast<char>(index)  // convert back to char
                      << " occured " << charASCIIArray[index] << " times.\n";
    
        delete[] charASCIIArray;
        return 0;
    }
    

    其次使用智能指针std::unique_ptr):https://www.ideone.com/dfc62J

    #include <string>
    #include <iostream>
    #include <cstddef>
    #include <memory>
    #include <utility>
    
    std::unique_ptr<int[]> count_char(const std::string& s)
    {
        // consider 128 ASCII decimal and their coresponding character codes
        std::unique_ptr<int[]> charASCIIArray = std::unique_ptr<int[]>(new int[128]{0});
        for (const auto& it : s)
        {
            if (('A' <= it && it <= 'Z') ||     // if char between (A,B,....,Z) or
                ('a' <= it && it <= 'z'))       //         between (a,b,....,z)
                charASCIIArray[static_cast<int>(it)]++; // we count each corresponding array then
        }
        return std::move(charASCIIArray);
    }
    
    int main()
    {
        std::string userinput = "random words WITH *- aLl";
    
        std::unique_ptr<int[]> charASCIIArray = count_char(userinput);
        for (std::size_t index = 0; index < 128; ++index)
            if (charASCIIArray[index] != 0)
                std::cout << "Letter " << static_cast<char>(index)  // convert back to char
                << " occured " << charASCIIArray[index] << " times.\n";
    
        return 0;
    }
    

答案 1 :(得分:0)

不太确定你想要这个答案,但我希望它有所帮助 你可以把它分成单独的功能,但我一次性完成了

-0.028450677