1

我设法编写了以下代码:

#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 数组中,然后将这些数组转换为字符串并将它们存储在 avector<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;
            }

    }
4

2 回答 2

3

您想将其复制到向量中:

istringstream iss(sentence);
vector<string> tokens;
copy(istream_iterator<string>(iss),
     istream_iterator<string>(),
     back_inserter<vector<string> >(tokens));
于 2013-06-05T15:52:20.420 回答
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];
}
于 2013-06-06T00:00:36.313 回答