如何知道字符串在数组中的出现次数

How to know the number of appearances of a string inside an array?

本文关键字:何知道 字符串 数组      更新时间:2023-10-16

基本上,我想检查字符串在数组中出现的次数。

我正在网上挑战,我遇到了这个。

首先,输入数组中元素的数量。然后输入一些字符串。

示例:

5
LOL
CODE
CODE
LOL
CODE

因此,我必须输出大多数时候键入的字符串。在这种情况下,这将是CODE

我如何用C++做到这一点?

我不确定是否有使用哈希图之类的方法,但我启动了一个程序,它本质上是一种暴力的做事方式。基本上,您需要使用动态数组跟踪每个字符串以及它显示的类型。一旦您完成了输入并分析了每个字符串以及它出现的次数,您就可以通过动态数组查看哪个字符串出现的次数最多。然后你只需输出它。

试着在没有我的程序帮助的情况下自己做这件事。如果你不能或如果你被卡住了,只需参考下面的工作程序,它会满足你的要求:

#include <vector>
#include <string>
#include <iostream>
using namespace std;
//This struct represents a string and how many times it appears
struct strRefCount { //String and Reference Count
    unsigned int count;
    string str;
};
strRefCount strMode(string data) //String mode: returns the string which appears most often
{
    vector<strRefCount> allStrings; //Count of each time a string appears and what the string is
    string curString = ""; //The string we are currently reading (initialize to be empty)
    unsigned int strPos = 0; //The position (in the form of data[strPos]) which represents how far we have gotten in analyzing the string
    strRefCount *modeStringp; //Pointer to the string that appears most often
    while(data[strPos] != NULL) { //We will advance through data until we hit the null terminator in this loop
        curString.clear();
        while(data[strPos] != ' ' && data[strPos] != NULL) //Advance in the string until we hit a space or terminating null byte
        {
            curString += data[strPos]; //Append the string
            strPos++; //Advance one byte in data
        }
        bool flagStringFound = false; //This flag indicates that the string was already found before
        for(unsigned int i = 0; i < allStrings.size(); i++)
        {
            if(allStrings[i].str == curString) //If this string is the same as the current entry
            {
                allStrings[i].count++;
                flagStringFound = true;
                break;
            }
        }
        if(flagStringFound == false) //If the string is not present in allStrings, put it there and initialize it
        {
            strRefCount addElem; //Element to add to the end of the vector
            addElem.str = curString; //Last element's string is curString
            addElem.count = 1; //Last element's reference count is curString
            allStrings.push_back(addElem); //Add the element
        }
        //Make sure we don't repeat the loop if we are at the end of the string
        if(data[strPos] != NULL)
        {
            break;
        }
    }
    //Now we have every string which appears in data and the number of times it appears
    //Go on to produce the correct output
    modeStringp = &(allStrings[0]); //Set modeStringp to the first string
    for(unsigned int i = 1; i < allStrings.size(); i++) //Note that by setting i to 1 we skip the first element which is already in modeStringp
    {   
        if(allStrings[i].count > modeStringp->count) //If the current entry in allStrings is bigger than 
        {
            modeStringp = &(allStrings[i]); //Replace modeStringp with the current entry in allStrings
        }
    }
    return *modeStringp;
}
int main()
{
    string data;
    getline(cin, data); //Get the input (can't use cin as it doesn't allow for an entire line just space seperated string)
    strRefCount dataModeString = strMode(data); //Call out strMode function
    cout << endl << dataModeString.str << " appears most often with a total of " << dataModeString.count << " appearances.";
    getchar(); //This line is only here to make sure we don't quit before we see the output.
    return 0;
}

这个程序对我有效。

该程序创建一个带有下一条记录的数组:

"Lucio", "John", "Lucio"

然后,它将该信息发送给一个函数,该函数返回引用次数最多的名称。因此它返回Lucio