C++ 如果一个单词在一个字符串中重复多次,我如何计算该单词的重复次数及其位置?

C++ 如果一个单词在一个字符串中重复多次,我如何计算该单词的重复次数及其位置?,c++,C++,如果一个单词在一个字符串中重复多次,我如何计算该单词的重复次数及其位置 #include <cstring> #include <iostream> #include <string> using namespace std; int main() { string str; getline(cin, str); string str2; getline(cin, str2); const char* p = str

如果一个单词在一个字符串中重复多次,我如何计算该单词的重复次数及其位置

#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;
    getline(cin, str);
    string str2;
    getline(cin, str2);
    const char* p = strstr(str.c_str(), str2.c_str());
    if (p)
        cout << "'" << str2 << "' find in " << p - str.c_str();
    else
        cout << target << "not find \"" << str << "\"";

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串str;
getline(cin,str);
字符串str2;
getline(cin,str2);
const char*p=strstrstr(str.c_str(),str2.c_str());
如果(p)

cout就在我的头顶上,您可以在
std::string
中使用
find()
find()
返回字符串中子字符串的第一个匹配项(或者如果没有匹配项,则返回
std::string::npos
),因此您需要循环,直到
find()
无法找到任何其他匹配项为止

比如:

#include <string>
#include <vector>
#include <cstdio>

int main(void) {

    std::string largeString = "Some string with substrings";
    std::string mySubString = "string";
    
    int numSubStrings = 0;
    std::vector<size_t> locations;
    
    size_t found = 0;

    while(true) {
        found = largeString.find(mySubString, found+1);
        if (found != std::string::npos) {
            numSubStrings += 1;
            locations.push_back(found);
        }

        else {
            break; // there are no more matches
        }
    }

    printf("There are %d occurences of: \n%s \nin \n%s\n", numSubStrings, mySubString.c_str(), largeString.c_str());
}

下面的代码使用了大量的标准库来为我们做一些常见的事情。我使用一个文件将单词收集到一个大字符串中。然后我使用
std::stringstream
来分隔空格中的单词,并将单个单词存储在
std::vector
中(一个数组,可以管理其大小并在需要时进行扩展)。为了获得良好的字数,还必须删除标点符号和大写字母,这是在
sanitize_word()中完成的
函数。最后,我将单词添加到以单词为键的映射中,
int
是该单词出现的次数计数。最后,我打印映射以获得完整的单词计数

我直接进行字符串解析的唯一地方是在sanitize函数中,它是使用恰当命名的erase/remove习惯用法完成的。如果可能的话,让标准库为我们完成这项工作要简单得多

在单词被分离和消毒后,定位单词出现的位置也变得微不足道

input.txt的内容:

I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past, I will turn the inner eye to see its path. Where the fear has gone, there will be nothing. Only I will remain.
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//删除puncuation标记并将单词转换为所有小写字母
std::string sanitize_-word(std::string-word){
word.erase(std::remove_if(word.begin(),word.end(),
[punc=std::字符串(“,?!”)(自动c){
返回punc.find(c)!=std::string::npos;
}),
word.end());
用于(自动和控制:word){
c=std::tolower(c);
}
返回词;
}
int main(){
//设立
std::ifstream-fin(“input.txt”);
如果(!fin){
标准:cerr>tmp;){
字。推回(tmp);
}
用于(自动和输入:文字){
i=消毒(i);
}
//std::map的操作符[]()函数将在map中创建一个新元素,如果
//还不存在
映射字数;
for(自动i:words){
++字数[i];
}
用于(自动i:wordCounts){

STD::使用<代码>字符串:在C++中查找< /COD>而不是<代码> STRSTR> <代码>。<代码> STD::String Strue >返回到<代码>::向量< /代码>。在向量中迭代并计数:<代码> STD::MAP< /COD>。我建议使用<代码> STD::MAP< /COD>。第一个参数是单词。第二个参数是向量位置。
I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past, I will turn the inner eye to see its path. Where the fear has gone, there will be nothing. Only I will remain.
and: 2
be: 1
brings: 1
eye: 1
face: 1
fear: 5
gone: 2
has: 2
i: 5
inner: 1
is: 2
it: 2
its: 1
little-death: 1
me: 2
mind-killer: 1
must: 1
my: 1
not: 1
nothing: 1
obliteration: 1
only: 1
over: 1
pass: 1
past: 1
path: 1
permit: 1
remain: 1
see: 1
that: 1
the: 4
there: 1
through: 1
to: 2
total: 1
turn: 1
when: 1
where: 1
will: 5


fear: 5
Found at locations: 3 4 8 20 50