简体   繁体   中英

What is the best way to take an input line and add the words in the line to a vector in C++?

Basically I want to take an input line with multiple words (length not specified), word by word and add each word to a vector. I can use getline and write a function to split it but wanted a more concise way to read each word and keep on adding it to a vector till Enter key is pressed. Something like this till enter key is pressed. Thanks!

vector<string> inp;
    while(????)
    {
    string str;
    cin>>str;

    inp.push_back(str);
    }

I am looking for something without using libraries, just some way of stop taking input when enter key is pressed, some condition in the while loop in the above code such that when enter key is encountered, it breaks out and stops taking input. Something like:

while(1)
{
  string str;
  cin>>str;
  // if( character entered =='\0')
        //break;
  inp.push_back(str);
}

Any help would be appreciated.

What's best is impossible to answer; that depends on how you measure goodness and is very much a question of taste and personal preference.
For instance, some people enjoy writing explicit loops, other people avoid them whenever they can.

One way that doesn't use an explicit loop is to use std::copy and std::istringstream .

std::vector<std::string> words;
std::string line;
if (std::getline(std::cin, line))
{
    std::istringstream is(line);
    std::copy(std::istream_iterator<std::string>(is),
              std::istream_iterator<std::string>(),
              std::back_inserter(words));
}

A nice way to split each word of the string and store them into a vector would be taking the help of std::istringstream (from sstream library):

#include <iostream>
#include <vector>
#include <sstream>

int main(void) {
  std::string input;
  std::string temp;
  std::vector<std::string> words;

  std::getline(std::cin, input);

  // Using input string stream here
  std::istringstream iss(input);

  // Pushing each word sep. by space into the vector
  while (iss >> temp)
    words.push_back(temp);
  
  for(const auto& i : words)
    std::cout << i << std::endl;

  return 0;
}

As a sample test case, you can see:

$ g++ -o main main.cpp && ./main
Hello world, how are you?      
Hello
world,
how
are
you?

Following code is almost the same as @Rohan Bari's answer, but I'm posting since there's a difference on splitting strings.

#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
#include <string>

int main(void)
{
    std::string input;
    std::getline(std::cin, input);
    std::stringstream ss(input);
    auto words = std::vector<std::string>(std::istream_iterator<std::string>(ss), {});  // difference

    for (const std::string& s : words)
        std::cout << s << std::endl;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM