拆分句子并检索特定单词

时间:2013-06-05 15:42:32

标签: 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 ( *ptr == ' ' ){
            cout << endl;
        }
        else{
            cout << *ptr;
        }
        ptr++;
    }
}

通过使用它我可以分别打印句子的每个单词。但是我想存储它们然后检索它们。这是一个示例运行:

Enter the sentence:This is a sample sentence.
Which word do you want to see ?:4
sample

我不知道如何继续上面的代码。我想将每个字母存储在一个char数组中,然后将这些数组转换为字符串并将它们存储在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 != '\0'){
        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;
}

EDIT3:Rite,明白了。这就是我想要的解决方案。

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

    }

2 个答案:

答案 0 :(得分:3)

您希望将其复制到矢量中:

istringstream iss(sentence);
vector<string> tokens;
copy(istream_iterator<string>(iss),
     istream_iterator<string>(),
     back_inserter<vector<string> >(tokens));

答案 1 :(得分:0)

这实际上很直截了当。您需要设置一个在输入时运行的循环。对于每次迭代,您应该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];
}