C++ 拆分句子并检索特定单词
我成功地编写了以下代码:C++ 拆分句子并检索特定单词,c++,string,vector,C++,String,Vector,我成功地编写了以下代码: #include <iostream> #include <vector> #include <string> using namespace std; int main() { string sentence; getline(cin, sentence); char* ptr = &sentence[0]; while ( *ptr != '\0' ){ if ( *
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string sentence;
getline(cin, sentence);
char* ptr = &sentence[0];
while ( *ptr != '\0' ){
if ( *ptr == ' ' ){
cout << endl;
}
else{
cout << *ptr;
}
ptr++;
}
}
我不知道如何从上面的代码开始。我想将每个字母存储在一个字符数组中,然后将这些数组转换成字符串,并将它们存储在一个向量中,但我想不出来
我希望只使用给定的库,如果可能的话,不使用任何拆分函数
编辑:这是我最近尝试的方法。但不起作用。
#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 != '\0'){
while ( *ptr != ' ' ){
temp.push_back(*ptr);
ptr++;
}
words.push_back(str(temp));
}
#include <iostream>
#include <string>
using namespace std;
int wordbyword(string sentence, char** words)
{
int i = 0, j = 0, k = 0;
while (sentence[i] != '\0'){
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;
}
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符字母;
向量词;
向量温度;
向量句;
而(cin>>字母){//ctrl-z要断开
句子。推回(字母);
}
char*ptr=&句子[0];
而(*ptr!='\0'){
而(*ptr!=''){
温度回推(*ptr);
ptr++;
}
单词。推回(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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
单词;
代币。推回(单词);
}
cout n;
你想把它复制到向量中吗
istringstream iss(sentence);
vector<string> tokens;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokens));
istringstream-iss(句子);
向量标记;
复制(istream_迭代器(iss),
istream_迭代器(),
背面插入器(令牌);
这实际上非常直截了当。您需要设置一个在获取输入时运行的循环。对于每一次迭代,您应该将新字符串推回到向量:
#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];
}
#包括
#包括
#包括
int main()
{
向量词;
字符串字;
while(std::cin>>word)
{
单词。推回(单词);
}
int-idx=0;
std::cout>idx;
你能数一数到目前为止你看到的空白的数量吗。第四个单词在第三个空白之后。(根据你对单词的定义,事情可能会变得更复杂)你可以使用一个std::istringstream
,然后用>
读入一个std::string
。这将在空白处分割。std::stringstream
就是为了这个而设计的。(#include
).拆分只依赖于空格。back\u inserter
使用得不够多!虽然对非初学者有很好的建议,但我认为没有足够的解释来解释OP。当然,他可以无意识地复制和使用它,但从长远来看,这对他真的没有帮助。是的,但重点是将句子作为一个整体输入,然后拆分判决。
#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];
}