C++ 我怎样才能找到一个句子的n-gram?
我得到了测试字符串C++ 我怎样才能找到一个句子的n-gram?,c++,C++,我得到了测试字符串helloworld,其中我必须找到的n-gram,具体是3。因此,我的代码应该为我提供以下输出: hel、ell、llo、low、owo、wor、orl、rld, 我编写的代码如下: vector<string> generate_ngrams(string w, size_t n) { vector<string> ngrams; for (auto i = 0; i < n; i++) { ngrams.push_back(w.su
helloworld
,其中我必须找到的n-gram,具体是3
。因此,我的代码应该为我提供以下输出:
hel、ell、llo、low、owo、wor、orl、rld,
我编写的代码如下:
vector<string> generate_ngrams(string w, size_t n) {
vector<string> ngrams;
for (auto i = 0; i < n; i++) {
ngrams.push_back(w.substr(i * n, n));
}
return ngrams;
n是子字符串的大小,而不是w的大小
for (int i = 0; i <= w.length() - n; ++i)
{
ngrams.push_back(w.substr(i, n));
}
for(inti=0;谢谢!这让我更接近了。现在我面临的问题是如何使它输出最后3个字母?我得到的输出是hel,ell,llo,low,owo,wor,orl,
应该是i
for (int i = 0; i <= w.length() - n; ++i)
{
ngrams.push_back(w.substr(i, n));
}