【发布时间】:2013-11-21 06:13:17
【问题描述】:
好吧,伙计们......
这是我的一套,里面有所有的字母。我将一个词定义为由集合中的连续字母组成。
const char LETTERS_ARR[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
const std::set<char> LETTERS_SET(LETTERS_ARR, LETTERS_ARR + sizeof(LETTERS_ARR)/sizeof(char));
我希望这个函数能接收一个表示句子的字符串,并返回一个字符串向量,这些字符串是句子中的单个单词。
std::vector<std::string> get_sntnc_wrds(std::string S) {
std::vector<std::string> retvec;
std::string::iterator it = S.begin();
while (it != S.end()) {
if (LETTERS_SET.count(*it) == 1) {
std::string str(1,*it);
int k(0);
while (((it+k+1) != S.end()) && (LETTERS_SET.count(*(it+k+1) == 1))) {
str.push_back(*(it + (++k)));
}
retvec.push_back(str);
it += k;
}
else {
++it;
}
}
return retvec;
}
例如,以下调用应返回字符串“Yo”、“dawg”等的向量。
std::string mystring("Yo, dawg, I heard you life functions, so we put a function inside your function so you can derive while you derive.");
std::vector<std::string> mystringvec = get_sntnc_wrds(mystring);
但一切都没有按计划进行。我尝试运行我的代码,它将整个句子放入向量的第一个也是唯一一个元素中。我的函数是非常混乱的代码,也许你可以帮我想出一个更简单的版本。我不希望您能够在我编写该函数的可怜尝试中追踪我的思维过程。
【问题讨论】:
标签: c++