C++ 函数将每个单词从字符串中分离出来,并将它们放入向量中,而不使用auto关键字?
我真的被困在这里了。所以我不能编辑主函数,在它里面有一个函数调用,唯一的参数是字符串。如何使此函数不使用auto关键字而将字符串中的每个单词放入向量中?我意识到这段代码可能真的是错的,但这是我最好的尝试C++ 函数将每个单词从字符串中分离出来,并将它们放入向量中,而不使用auto关键字?,c++,C++,我真的被困在这里了。所以我不能编辑主函数,在它里面有一个函数调用,唯一的参数是字符串。如何使此函数不使用auto关键字而将字符串中的每个单词放入向量中?我意识到这段代码可能真的是错的,但这是我最好的尝试 #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; vector<string> ext
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> extract_words(const char * sentence[])
{
string word = "";
vector<string> list;
for (int i = 0; i < sentence.size(); ++i)
{
while (sentence[i] != ' ')
{
word = word + sentence[i];
}
list.push_back(word);
}
}
int main()
{
sentence = "Help me please" /*In the actual code a function call is here that gets input sentence.*/
if (sentence.length() > 0)
{
words = extract_words(sentence);
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
向量提取_单词(常量字符*句子[])
{
字串=”;
向量表;
for(int i=0;i<句子大小();++i)
{
while(句子[i]!=“”)
{
单词=单词+句子[i];
}
列表。推回(word);
}
}
int main()
{
句子=“Help me please”/*在实际代码中,这里有一个函数调用,用于获取输入句子*/
if(句子长度()>0)
{
单词=摘录单词(句子);
}
}
你知道如何阅读std::cin
中的“单词”吗
然后,您可以将该字符串放入类似于std::cin
但用于“读取”字符串的格式中
在循环中使用stream extract操作符>
,逐个获取所有单词,并将它们添加到向量中
也许是这样的:
std::vector<std::string> get_all_words(std::string const& string)
{
std::vector<std::string> words;
std::istringstream in(string);
std::string word;
while (in >> word)
{
words.push_back(word);
}
return words;
}
std::vector获取所有单词(std::string const&string)
{
向量词;
std::istringstream输入(字符串);
字符串字;
while(在>>word中)
{
单词。推回(单词);
}
返回单词;
}
<> P>对C++及其标准类和函数有了更多的了解,实际上可以使函数的长度变短:
std::vector<std::string> get_all_words(std::string const& string)
{
std::istringstream in(string);
return std::vector<std::string>(std::istream_iterator<std::string>(in),
std::istream_iterator<std::string>());
}
std::vector获取所有单词(std::string const&string)
{
std::istringstream输入(字符串);
返回std::vector(std::istream_迭代器(in),
std::istreamu迭代器();
}
我建议将函数的参数设置为const std::string&
而不是const char*句子[]
。一个std::string
有,like,以及更多可能会有很大帮助的东西
下面是一个使用上述内容的示例:
std::vector<std::string> extract_words(const std::string& sentence)
{
/* Control char's, "whitespaces", that we don't want in our words:
\a audible bell
\b backspace
\f form feed
\n line feed
\r carriage return
\t horizontal tab
\v vertical tab
*/
static const char whitespaces[] = " \t\n\r\a\b\f\v";
std::vector<std::string> list;
std::size_t begin = 0;
while(true)
{
// Skip whitespaces by finding the first non-whitespace, starting at
// "begin":
begin = sentence.find_first_not_of(whitespaces, begin);
// If no non-whitespace char was found, break out:
if(begin == std::string::npos) break;
// Search for a whitespace starting at "begin + 1":
std::size_t end = sentence.find_first_of(whitespaces, begin + 1);
// Store the result by creating a substring from "begin" with the
// length "end - begin":
list.push_back(sentence.substr(begin, end - begin));
// If no whitespace was found, break out:
if(end == std::string::npos) break;
// Set "begin" to the char after the found whitespace before the loop
// makes another lap:
begin = end + 1;
}
return list;
}
请创建一个。展示有问题的代码,展示您的最佳尝试,并解释到底是什么阻止您解决问题。请注意,
auto
关键字是一个很好的工具,您不应该禁止自己使用它。@alterigel我肯定是的,但是类只允许使用材质中的内容,而auto不在材质中。@seaccumber24不,但是std::string
显然是可以的,所以不要使用const char*句子[]
-查看您得到的答案,看看如何将参数带到函数中。语句.size()
不适用于数组。试试strlen(句子)或将函数的参数改为字符串而不是数组。@JerryJeremiah好的,但如何创建一个循环来分析字符串中空格的位置,从而将单词分隔开?不幸的是,我只能使用课堂上教过的内容,因此我也不能使用stringstream。@SeaCucumber24如果可以,循环std::string::find
,在std::string substr
的帮助下查找空格并在空格之间存储字符串。如果你不能使用查找
,听起来就像是被迫演奏几小节威廉·泰尔序曲。获取一个字节。获取一个字节。获取一个字节。在字符串中循环寻找空格。
std::vector<std::string> extract_words(const std::string& sentence)
{
static const char whitespaces[] = " \t\n\r\a\b\f\v";
std::vector<std::string> list;
std::size_t begin = 0;
bool loop = true;
while(loop)
{
begin = sentence.find_first_not_of(whitespaces, begin);
if(begin == std::string::npos) {
loop = false;
} else {
std::size_t end = sentence.find_first_of(whitespaces, begin + 1);
list.push_back(sentence.substr(begin, end - begin));
if(end == std::string::npos) {
loop = false;
} else {
begin = end + 1;
}
}
}
return list;
}