如何在C++; 在一个C++代码中,我试图在句子中搜索一个单词,但是它一直在进行部分搜索。我希望它只搜索完整的单词,而不是它的一部分,有什么帮助吗 size_t kk; string word="spo"; string sentence="seven spoons"; kk=sentence.find(word); if (kk !=string::npos) cout << "something" << endl; size\u t kk; string word=“spo”; 字符串句子=“七勺”; kk=句子。查找(单词); if(kk!=string::npos) cout

如何在C++; 在一个C++代码中,我试图在句子中搜索一个单词,但是它一直在进行部分搜索。我希望它只搜索完整的单词,而不是它的一部分,有什么帮助吗 size_t kk; string word="spo"; string sentence="seven spoons"; kk=sentence.find(word); if (kk !=string::npos) cout << "something" << endl; size\u t kk; string word=“spo”; 字符串句子=“七勺”; kk=句子。查找(单词); if(kk!=string::npos) cout,c++,string,find,C++,String,Find,这里有很多选择: a)搜索[space]WORD[space]而不仅仅是WORD string word="spo"; string sentence="seven spoons"; kk=sentence.find(" "+word+" "); 请注意,如果单词之间用换行符或其他空格分隔,则此操作无效 b)将字符串拆分为单词,将它们存储在向量中,并使用std::find检查所需单词是否在向量中的某个位置 stringstream parser(sentence); istream_itera

这里有很多选择:

a)搜索
[space]WORD[space]
而不仅仅是
WORD

string word="spo";
string sentence="seven spoons";
kk=sentence.find(" "+word+" ");
请注意,如果单词之间用换行符或其他空格分隔,则此操作无效

b)将字符串拆分为单词,将它们存储在
向量中,并使用
std::find
检查所需单词是否在向量中的某个位置

stringstream parser(sentence);
istream_iterator<string> start(parser);
istream_iterator<string> end;

vector<string> words(start, end);
if(find(words.begin(), words.end(), word)!=words.end()) cout<<"found!";

我认为最好的方法是使用空格和标点符号作为分隔符,然后对结果使用
std::find

#include <boost/algorithm/string.hpp>
#include <vector>
#include <string>
#include <algorithm>

int main()
{
    std::string word="spo";
    std::string sentence="seven spoons";
    std::vector<std::string> words;
    boost::split(words, sentence, boost::is_any_of("\n\t .,!?\"()"));
    auto match = std::find(begin(words), end(words), word);
    if (match != end(words))
    {
        // Found it!
    }
    else
    {
        // Not there.
    }
 }
#包括
#包括
#包括
#包括
int main()
{
std::string word=“spo”;
std::string语句=“七匙”;
向量词;
boost::split(单词、句子、boost::是(“\n\t.,!?\”()”)中的任何一个);
自动匹配=标准::查找(开始(单词)、结束(单词)、单词);
如果(匹配!=结束(单词))
{
//找到了!
}
其他的
{
//不在那里。
}
}
类似这样的内容:

std::size_t kk;
std::string word="spoo";
std::string sentence="seven spoons tables";

std::stringstream ss(sentence) ;
std::istream_iterator<std::string> f ;

auto it =std::find_if(  std::istream_iterator<std::string> (ss),
                        f,
                        [=](const std::string& str){
                        return str == word;
                        }
 );

if(it != f )
 std::cout << "Success" <<std::endl;
std::size\u t kk;
std::string word=“spoo”;
std::string-sense=“七匙表”;
std::stringstream ss(句子);
std::istream_迭代器f;
auto it=std::find_if(std::istream_迭代器(ss),
F
[=](常量std::string和str){
返回str==word;
}
);
如果(it!=f)
std::cout
string word=“spo”;
字符串句子=“七勺”;
字符串::size\u type nIndex=句子.find(单词,0);
if(nIndex!=string::npos)
{  
if((nIndex+word.length()+1)=句子.length())
{  

cout听起来您想要的是由正则表达式中的单词边界或单词字符的概念来处理的

这是一个只返回完全匹配的程序。也就是说,如果某个单词与您正在搜索的单词完全匹配,它将只返回该单词。如果
句子中的某个单词将您的目标单词作为严格的子字符串,则不会返回该单词

#include <regex>
#include <string>
#include <iostream>

int main() {
  std::string word = "spo"; // spo is a word?
  std::string sentence = "seven spoons";

  std::regex r("\\b" + word + "\\b"); // the pattern \b matches a word boundary
  std::smatch m;

  if (std::regex_search(sentence, m, r)) { // this won't find anything because 'spoons' is not the word you're searching for
    std::cout << "match 1: " << m.str() << '\n';
  }

  sentence = "what does the word 'spo' mean?";    
  if (std::regex_search(sentence, m, r)) { // this does find the word 'spo'
    std::cout << "match 2: " << m.str() << '\n';
  }
}
这将产生:

match 1: spoons
match 2: spo

这似乎奏效了

#include <string>

/*find word in sentence and return the index of first occurrence*/
int find_whole(string sentence,string word){
    size_t pos=sentence.find(word);
    size_t offset=pos+sentence.size()+1;

    if((pos!=string::npos) && (sentence.substr(pos,offset)==word))
        return pos;
    return string::npos;
}
#包括
/*在句子中查找单词并返回第一次出现的索引*/
整型(字符串句子、字符串单词){
大小\u t pos=句子。查找(单词);
size\u t offset=pos+句子。size()+1;
if((pos!=字符串::npos)和&(句子.substr(pos,offset)==单词))
返回pos;
返回字符串::npos;
}

如果搜索词是“七”(在那里,但前面没有空格)或“勺子”(在那里,但后面没有空格),该怎么办?@FredLarson词边界的定义解释了这一点。它很有效。啊,明白了。+1适合你。
  std::string partial_word = "spo";
  std::regex r("\\w*" + partial_word + "\\w*"); // the pattern \w matches a word character
match 1: spoons
match 2: spo
#include <string>

/*find word in sentence and return the index of first occurrence*/
int find_whole(string sentence,string word){
    size_t pos=sentence.find(word);
    size_t offset=pos+sentence.size()+1;

    if((pos!=string::npos) && (sentence.substr(pos,offset)==word))
        return pos;
    return string::npos;
}