Here's one possibility to treat an input from cin
with an arbitrary number of whitespaces between the entries, and to store the data in a vector by using the boost library:
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
int main() {
std::string temp;
std::vector<std::string> entries;
while(std::getline(std::cin,temp)) {
boost::split(entries, temp, boost::is_any_of(" "), boost::token_compress_on);
std::cout << "number of entries: " << entries.size() << std::endl;
for (int i = 0; i < entries.size(); ++i)
std::cout << "entry number " << i <<" is "<< entries[i] << std::endl;
}
return 0;
}
edit
The same result could be obtained without using the awesome boost library, e.g., in the following way:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main() {
std::string temp;
std::vector<std::string> entries;
while(std::getline(std::cin,temp)) {
std::istringstream iss(temp);
while(!iss.eof()){
iss >> temp;
entries.push_back(temp);
}
std::cout << "number of entries: " << entries.size() << std::endl;
for (int i = 0; i < entries.size(); ++i)
std::cout<< "entry number " << i <<" is "<< entries[i] << std::endl;
entries.erase(entries.begin(),entries.end());
}
return 0;
}
example
Input:
AA 12 6789 K7
Output:
number of entries: 4
entry number 0 is AA
entry number 1 is 12
entry number 2 is 6789
entry number 3 is K7
Hope this helps.