Assuming that you are reading your input from stdin
, one (trivial) way to do this would be to read directly from cin
as follows:
std::vector<std::string> first_names, last_names;
while (std::cin)
{
std::string first_name, last_name;
std::cin >> first_name >> last_name;
first_names.push_back(first_name);
last_names.push_back(last_name);
}
This works given the very simple format of your input, but anything more complex might not be so straightforward. It would be better as a general rule to read each line from the input into a string and process it there:
std::string line;
while (std::getline(std::cin, line))
{
// Do some stuff here with 'line', e.g.
size_t space_pos = line.find_first_of(' ');
std::string first_name = line.substr(0, space_pos);
std::string last_name = line.substr(space_pos + 1);
}
This will give you more options such as using a string tokeniser or a regular expression pattern matcher to extract based on more complex criteria.
Naturally, if you aren't reading your name pairs from stdin
, but from an array or vector instead, you can simply iterate over the collection and substitute line
with the target of the iterator.