把一个句子分开,然后找回一个特定的单词
Splitting a sentence and retrieving a specific word
我写了下面一段代码:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string sentence;
getline(cin, sentence);
char* ptr = &sentence[0];
while ( *ptr != ' ' ){
if ( *ptr == ' ' ){
cout << endl;
}
else{
cout << *ptr;
}
ptr++;
}
}
通过使用,我可以单独打印句子中的每个单词。然而,我想要存储它们,然后再检索它们。下面是一个示例:
Enter the sentence:This is a sample sentence.
Which word do you want to see ?:4
sample
我不知道如何从上面的代码继续。我想将每个字母存储在一个字符数组中,然后将这些数组转换为字符串并将它们存储在vector<string>
中,但无法弄清楚。
我希望只使用给定的库,如果可能的话,不使用任何分裂函数。
编辑:这是我最近尝试的。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
char letter;
vector<string> words;
vector<char> temp;
vector<char> sentence;
while( cin >> letter ){ // ctrl-z to break
sentence.push_back(letter);
}
char* ptr = &sentence[0];
while ( *ptr != ' '){
while ( *ptr != ' ' ){
temp.push_back(*ptr);
ptr++;
}
words.push_back(str(temp));
}
}
EDIT2:这是一个解决方案与sstream
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
cout << "Sentence: " << endl;
string sentence;
getline(cin, sentence);
istringstream sin(sentence);
vector<string> tokens;
string word;
while (!sin.eof()){
sin >> word;
tokens.push_back(word);
}
cout << "Which word ?: " << endl;
int n;
cin >> n;
cout << tokens[n - 1] << endl;
}
编辑3:我明白了。这是我想要的解决方案。
#include <iostream>
#include <string>
using namespace std;
int wordbyword(string sentence, char** words)
{
int i = 0, j = 0, k = 0;
while (sentence[i] != ' '){
if (sentence[i] != ' '){
words[j][k] = sentence[i];
k++;
}
else {
j++;
k = 0;
}
i++;
}
return j;
}
int main()
{
string sentence;
cout << "Sentence: "<< endl;
getline(cin, sentence);
int size = sentence.length();
char** words = new char*[size];
for ( int i = 0; i < size; i++)
words[i] = new char[size];
int wordCount = wordbyword(sentence, words) + 1;
while(1){
cout << "Word number: " << endl;
int n;
cin >> n;
if ( n == 0){
cout << "Terminating..." << endl;
break;
}
else if ( n > wordCount || n < 0)
cout << "Word doesn't exist" << endl;
else
cout << words[n - 1] << endl;
}
}
你想要复制到vector:
istringstream iss(sentence);
vector<string> tokens;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokens));
这实际上非常直接。您需要设置一个循环,它将在接受输入时运行。对于每次迭代,您应该将push_back
转换为新字符串的向量:
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> words;
std::string word;
while (std::cin >> word)
{
words.push_back(word);
}
int idx = 0;
std::cout << "Which word do you want to see? ";
std::cin >> idx;
std::cout << words[idx];
}
相关文章:
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 如何检查流中文件的下一个单词是否为 alpha?
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 计算一个单词在映射中出现的次数 - 使用 transform() - 调用不匹配
- 如何将一个单词拆分成字母,并将它们放入一个无序的列表/集合中
- 将字符串中的单个单词替换为另一个单词
- 如何插入一个单词并将字母放在2D数组中的不同索引中
- C++ ifstream 只读一个单词的数量
- 我如何计算一个单词在数组中重复的次数
- 如何将 c 字符串转换为指针数组,其中每个指针都是 char 数组中的一个单词
- 将 C++ 类属性与分号前面的最后一个单词对齐;在 emacs 中
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我如何在一串随机生成的字母I C 中查找一个单词
- 在字符串中找到最后一个单词
- C++ 输出在读取文本文件时跳过最后一个单词
- 我如何将句子分成一个单词
- 在向量中通过一个单词查找行
- 'Try This'矢量在示例词典中哔哔一个单词。编程原理与实践
- 我怎样才能从字符串中得到一个单词
- 如何将文本(诗歌)拆分为行(字符串/字符[])并查找每个行的最后一个单词