查找字符串C ++中重复字符串的总数 - 没有地图

时间:2016-04-18 14:31:23

标签: c++ string

我的程序必须找到字符串中重复字符串的总数。我不能使用map或内置字符串函数而不是length()

  

示例:string input =" Hello hello Hello"

     

你好: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; 
}

2 个答案:

答案 0 :(得分:1)

在空格分隔的字符串中计算子字符串的直接方法是将它们插入到地图中并跟踪出现次数:

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;
}

代码未经测试。

答案 1 :(得分:1)

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

#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;
}

请注意,此代码不仅会找到连续的重复项。所以&#34; a b a&#34;输出&#39; a 2&#39;。

行++ wordCount [word]会增加单词的计数器,或者如果&#39; word&#39;则将其初始化为1。在地图中尚未找到(这是有效的,因为模板使用int()初始化值,保证初始化为零)

最后,您会得到一张地图,其中包含每个唯一单词的条目(第一个= Word,第二个=计数)

如果您只想计算连续重复项,这段代码可能会对您有所帮助:

#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;
}

此代码不使用地图,因为单个单词可以根据其位置具有不同的计数(&#34; aabaa a&#34;将输出&#34; a 2&#34;和&#34; a 3& #34)

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