在'word'之前和之后输出字符串向量,而不是使用正则表达式。制作一致性计划

时间:2011-05-04 22:13:08

标签: c++ string vector return concordion

我正在研究一致性程序,目前正在研究getContext函数。我需要这个函数有点像正则表达式,但我希望它返回指定单词之前和之后的字符串向量。我不知道我在想什么是正确的,但这是我能想到的全部。

所以这就是我的想法:它接受一个单词并创建两个向量并返回指定单词的左右两个。

感谢。 :d

我认为我不需要包含整个代码文件,但如果有人需要,我也可以把它放在一起。

/* Get context for input parameter word (case-insensitive comparison)
* Return a dynamically allocated vector of strings, each string
* consisting of contextSize number of words before word and contextSize
* number of words after word, with word in the middle (set off with "<<"
* before the word and ">>" after the word). ContextSize defaults to 5.
* It is user's responsibility to delete the vector.
*/
vector<string>*Concordance::getContext(string word, int contextSize = 5){
    vector<string> before;
    vector<string> after;


    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果您只想在std::string中寻找std::vector<std::string>,那么您可以使用std::find

bool IsWordInArrayOfWords(const std::vector<string>& arrayOfWords, const std::string& word)
{
    auto found = std::find(arrayOfWords.cbegin(), arrayOfWords.cend(), word);
    return found != arrayOfWords.cend();
}

如果您正在寻找一种方法来搜索单词的匹配和基于百分比或其他更复杂的上下文的最佳匹配,并且正则表达式不是一个选项,那么我认为我们需要更好的描述您正在努力实现的目标以及您希望解决的真正问题是什么。