Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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/3/arrays/13.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++_Arrays_String - Fatal编程技术网

C++ 如何知道数组中字符串的出现次数?

C++ 如何知道数组中字符串的出现次数?,c++,arrays,string,C++,Arrays,String,基本上,我想检查一个字符串在数组中出现了多少次 我在做在线挑战,我遇到了这个问题 首先,输入数组的元素数。然后输入一些字符串 例如: 5 LOL CODE CODE LOL CODE 因此,我必须输出输入次数最多的字符串。在这种情况下,这将是code 我怎么能用C++来做? 我不确定用哈希图之类的方法,但是我启动了一个程序,它本质上是做事情的蛮力方式。基本上,您需要使用动态数组跟踪每个字符串及其显示的类型。一旦您完成了输入并分析了每个字符串以及它出现的次数,您就可以查看动态数组并查看哪个字符串

基本上,我想检查一个字符串在数组中出现了多少次

我在做在线挑战,我遇到了这个问题

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

例如:

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;
}
#包括
#包括
#包括
使用名称空间std;
//此结构表示字符串及其出现次数
struct strefcount{//字符串和引用计数
无符号整数计数;
字符串str;
};
strRefCount strMode(字符串数据)//字符串模式:返回最常出现的字符串
{
vector allStrings;//每次字符串出现的计数以及字符串是什么
字符串curString=”“;//我们当前正在读取的字符串(初始化为空)
unsigned int strPos=0;//位置(以数据[strPos]的形式),表示我们在分析字符串时所取得的进展
strefcount*modeStringp;//指向最常出现的字符串的指针
while(data[strPos]!=NULL){//我们将遍历数据,直到在这个循环中找到NULL终止符
curString.clear();
while(data[strPos]!=''&&data[strPos]!=NULL)//在字符串中前进,直到找到空格或终止空字节
{
curString+=data[strPos];//追加字符串
strPos++;//在数据中前进一个字节
}
bool flagStringFound=false;//此标志表示以前已找到该字符串
for(无符号整数i=0;imodedringp->count)//如果allStrings中的当前条目大于
{
modeStringp=&(allStrings[i]);//用allStrings中的当前条目替换modeStringp
}
}
返回*p;
}
int main()
{
字符串数据;
getline(cin,data);//获取输入(不能使用cin,因为它不允许整行只允许空格分隔的字符串)
strefcount dataModeString=strMode(数据);//调用strMode函数
本可以为我工作

程序将创建一个包含以下记录的数组:

"Lucio", "John", "Lucio"

然后它把信息发送给返回最引用的名字的函数。所以它返回代码>卢西奥< /Cord>

我现在不知道了……我不是专业的程序员。我现在在高中,我想扩大我的C++知识,将来我要做大学,所以我在网上挑战,只学习电子书。奈威,这不是你想要的问题的答案。基本上,我没有尝试任何东西,因为我不知道该怎么做。我查阅了哈希表,但实际上没有得到任何东西,所以任何例子或暗示做什么都是欢迎的。你正确地看哈希地图。继续寻找,这就是解决方案。谷歌C++。tl映射。@Lucio:我的问题是,在你输入数组中的所有字符串后,你必须输出最常提到的字符串。@JohnSmith:如果你一遇到难题就放弃,为什么还要投身于挑战中呢?与其花时间学习难题,还不如去挑战。