在不带映射的字符串C++中查找重复字符串的总数

Finding the total number of repeat strings in a string C++ -- without maps

本文关键字:字符串 查找 C++ 映射      更新时间:2023-10-16

我的程序必须找到字符串中重复字符串的总数。除了length()之外,我不能使用映射或内置字符串函数

示例:字符串输入=";你好你好你好"

你好:2

我遇到了一个障碍,用空格分隔字符串并阅读它们。我不知道该写些什么来实现这一点。

我想做的是创建一个临时字符串来与下一个字符串进行比较,如果它们相等,就将其存储在一个向量中,然后在最后从向量中读取。

我可以用什么函数来做这件事?

下面是我的代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector <string> mystring;
int numberString(string const&in)
{
    int total = 0;
    char temp;
    for (int i = 0; i < in.length(); i++)
    {
        temp = in[i];
        if (temp == ' ')
            total++;
    }
    total++;
    return total;
}
void findRepeats(string const &in)
{
    int numberOfStrings = numberString(in);
    int asciiArray[256];
    for (int i = 0; i < 256; i++)
        asciiArray[i] = 0;
    int counter = 0;
    string temp = "blank";
    while (numberOfStrings != counter)
    {
        temp = in;
    }
}
int main()
{
    string input;
    cout << "Enter a string : ";
    getline(cin, input);
    findRepeats(input);
    return 0; 
}

在以空格分隔的字符串中计算子字符串的简单方法是将它们插入映射并跟踪出现次数:

std::string input = "Hello hello Hello";
std::istringstream iss(input);
std::map<std::string, size_t> m;
std::string temp;
while(iss >> temp)
{
    auto it = m.find(temp);
    if(it != std::end(m))
    {
        ++(it->second);
    }
    else
    {
        m.insert(std::make_pair(temp, 0));
    }
}
//display counts as:
for(auto it = std::begin(m); it != std::end(m); ++it)
{
    std::cout<<"string ""<<it->first<<"" was found "<<it->second<<" times"<<std::endl;
}

该代码未经测试。

只要所有单词都用一个空格分隔,以下代码就会找到重复的单词:

#include <string>
#include <map>
#include <sstream>
#include <iostream>
using namespace std;
int main() 
{
    string input = "Hello hello hello";
    map<string, int> wordCount;
    for (string::size_type p = 0; p < input.size(); )
    {
        const auto p2 = input.find_first_of(' ', p);
        const auto word = input.substr(p, (p == string::npos) ? string::npos : (p2 - p));
        ++wordCount[word];
        if (p2 == string::npos)
            break;
        p = p2 + 1;
    }
    for (const auto& it : wordCount)
        if (it.second > 1)
            std::cout << it.first << " " << it.second << std::endl;

    return 0;
}

请注意,此代码不仅可以查找连续的重复项。所以"a b a"输出"a 2"。

行++wordCount[word]增加单词的计数器,或者如果在映射中没有找到"word",则将其初始化为1(这是有效的,因为模板使用int()初始化值,保证初始化为零)

最后,你会得到一张地图,上面有每个唯一单词的条目(第一个=单词,第二个=计数)

如果你想只计算连续的重复,这段代码可能会帮助你:

#include <string>
#include <map>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    string input = "Hello hello hello Hello";
    vector<pair<string, int>> wordCount;
    for (string::size_type p = 0; p < input.size(); )
    {
        const auto p2 = input.find_first_of(' ', p);
        const auto word = input.substr(p, (p == string::npos) ? string::npos : (p2 - p));
        if (wordCount.empty() || wordCount.back().first != word)
            wordCount.push_back(make_pair(word, 1));
        else
            ++wordCount.back().second;
        if (p2 == string::npos)
            break;
        p = p2 + 1;
    }
    for (const auto& it : wordCount)
        if (it.second > 1)
            std::cout << it.first << " " << it.second << std::endl;

    return 0;
}

这个代码不使用地图,因为一个单词根据其位置可以有不同的计数("a a a b a a a"将输出"a 2"answers"a 3")

这两个示例都以"作为分隔符逐词扫描字符串。如果要按制表符或句点分割字符串,可以在find_first_of中指定多个分隔符。(input.find_first_of("\t.",p))