1

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.

adikj
  • 15
  • 4
  • 1
    Please show the code that uses a getline, and puts the strings into a vector. Otherwise, as far as we know, your solution might be as concise as it gets. – cigien Aug 18 '20 at 14:19
  • Pro tip: consider asking questions like this with the phrasing "how can I" rather than "what is the best way to". The former will produce objective solutions whereas the latter will elicit opinions and arguments. Computer folks can be pedantic about words like "best". – Wyck Aug 18 '20 at 14:27
  • See https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string for a number of different solutions. – cigien Aug 18 '20 at 15:03

3 Answers3

2

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));
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • An alternative to `std::copy` would be `words.insert(words.end(), std::istream_iterator(is), std::istream_iterator())` – Caleth Aug 19 '20 at 10:55
1

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?
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • Pet peeve: "one of the best" means nothing. (Is it in the top 3? Top 10? Top 100000? How are you measuring best-ness?) – Wyck Aug 18 '20 at 14:35
  • 2
    @Wyck I said it's one of the best method because: it doesn't requires any other library except `vector` to store and `sstream` for `istringstream`. I've used `istringstream` here rather than `stringstream` which performs faster than that, that makes it better. – Rohan Bari Aug 18 '20 at 14:36
  • 2
    See? You should say _that_ instead of saying that it's _one of the best_. It's much more informative. Like I said, it's just a pet peeve. Don't take it too hard - but maybe think about it in your future writing. :) – Wyck Aug 18 '20 at 14:39
  • Thanks this did help! I was actually looking for something without using libraries, just some way of stop taking input when enter key is pressed. I'll edit my question and add this. – adikj Aug 19 '20 at 08:09
  • @adikj that's why libraries are made. It's to make the life simpler, you're just trying to make it more complex. – Rohan Bari Aug 19 '20 at 08:48
  • Yeah I get that but I wanted to know the solution without using libraries. Just thought there would some very simple solution to it without using any library. – adikj Aug 19 '20 at 10:18
  • @adikj this is the simplest. Doing this without using `sstream` library will make it hard. – Rohan Bari Aug 19 '20 at 10:27
0

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;
}
John Park
  • 1,644
  • 1
  • 12
  • 17