operator>>
uses whitespaces as separators, thus it is reading the input word by word already:
#include <iostream>
#include <sstream>
#include <vector>
int main() {
std::istringstream in("Hi
My name is Joe
How are you doing?");
std::string word;
std::vector<std::string> words;
while (in >> word) {
words.push_back(word);
}
for (size_t i = 0; i < words.size(); ++i)
std::cout << words[i] << ", ";
}
outputs: Hi, My, name, is, Joe, How, are, you, doing?,
In case you are going to look for specific keywords within this vector, just prepare this keyword in form of std::string
object and you might do something like:
std::string keyword;
...
std::vector<std::string>::iterator i;
i = std::find(words.begin(), words.end(), keyword);
if (i != words.end()) {
// TODO: keyword found
}
else {
// TODO: keyword not found
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…