把一个句子分开,然后找回一个特定的单词

Splitting a sentence and retrieving a specific word

本文关键字:一个 单词 句子 然后      更新时间:2023-10-16

我写了下面一段代码:

#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];
}