如何知道字符串在数组中的出现次数
How to know the number of appearances of a string inside an array?
基本上,我想检查字符串在数组中出现的次数。
我正在网上挑战,我遇到了这个。
首先,输入数组中元素的数量。然后输入一些字符串。
示例:
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
相关文章:
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 反转字符串.不知道为什么这个逻辑是错误的.C++
- 我如何知道字符串中字符相对于英文字母的位置值?
- 有人知道可以使用 c++ 以字节为单位编码 asm 字符串的任何东西吗?
- 我想知道如何将所有数值与没有任何空格的字符串分开,就像 123we45rt75 一样
- 标准地图如何知道使用标准字符串的数据作为键
- 当我打印字符串时,我不知道它是什么输出
- 如何用 c++ 读取 txt 文件,如果我们不知道其中有多少个数据字符串,以及每行有多长?
- 我如何知道 boost::any 是否包含文字字符串
- 我不知道为什么我的字符串无法保存值
- 我不知道如何创建一个以数字/字母顺序打印出我的整数或字符串的程序
- 如何迭代在不知道其大小的情况下读取的字符串?
- 字符 * 和字符串在C++中有何不同?(在描述中编写代码)
- string::npos如何知道我指的是哪个字符串
- 如何知道基于项目的ListView QT C 中是否存在字符串
- 实现将字符串映射到 C++ 中的数组的哈希表。我一直在"Debug Assertion Failed",我不知道为什么
- 获取一个不知道长度 C++ 的子字符串
- 如何按空格循环字符串?我怎么知道字符串中我当前所在的单词的索引号
- 在不知道字符串宽度的情况下左右对齐
- 如何知道字符串在数组中的出现次数