-1

Is there a simple way to convert a string of numbers separated by spaces into a vector of ints or something that I can easily convert into a vector?

I am creating an input operator (>>) to make a binary tree using values input from command line. And this is the main that goes with it

int main(int argc, char **argv)
{
stringstream stream;
stream << " 10" << " 11" << " 10" << " 2" << endl;
tree = new binary_tree();
stream >> *tree;
if (tree->inorder() != string("7 10 11")) //inorder() prints the tree in order
cout << "Test passed" << endl;
delete tree;

return 0;
}

The problem I am having is that while I can create and print the values that I need, I can't convert them and put them into a vector - for which I have a working defined method that creates a tree from the values.

std::istream& operator>>(std::istream &in, binary_tree &value)
{
vector<int> input_tree;
string readline;     
getline(in, readline);
cout << "(" << readline << ")" << endl; //prints correctly - (10 11 10 2)

    //where I need to insert insert values into vector

for(int i = 0; i < input_tree.size(); i++)
{
    insert(input_tree[i], value);    //inserts the values from a vector into a binary tree
}

return in;  
} 

I have tried looping through the string and using stoi() on each character but it always errored whenever the spaces were causing errors.

Thanks for any help and sorry if I've missed any important information out.

KL123
  • 11
  • 1
  • 1
  • You need to show exactly how you tried to convert the input, instead of leaving it out and replacing that code with the "//where I need to insert insert values into vector" comment. stackoverflow.com is not a code-writing service. Nobody will write your code for you. But, if you tried to parse the input into your `int` vector, and it's not working right, and you don't know what's wrong, then this would be a valid question. But "please write the code for me" is not a valid question. – Sam Varshavchik Apr 24 '16 at 02:46
  • The questions is if there is a way to convert a string to a vector type, I'm not asking anyone to write my code I'm asking for a very specific thing while showing my code for reference. – KL123 Apr 24 '16 at 02:51
  • Well, if that's your question, then the answer is "yes, there is a way". – Sam Varshavchik Apr 24 '16 at 02:52

2 Answers2

1

You can do this:

vector<int> vec((istream_iterator<int>(in)), istream_iterator<int>());

This will read integers from in and insert them into vec all in one line. It's a pretty canonical use of istream_iterator which is part of the standard library. Then you don't need to read each line and parse it yourself.

If you want to read more about how this works, see here: How does std::copy work with stream iterators

As for why there appear to be an extra pair of parentheses around the first argument, that's because of the "most vexing parse": https://en.wikipedia.org/wiki/Most_vexing_parse - just a silly quirk of C++ syntax.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I haven't tested it but surely that wouldn't work because in my example I'm using a stringstream and they are input as characters and not as int types? – KL123 Apr 24 '16 at 02:55
  • @KL123: No, this is the entire point. The `istream_iterator` is an interface to an `istream` (which a `stringstream` is) and performs the formatted extraction to `int` as it goes. Click on the link in the answer labelled "If you want to read more about how this works..." – Lightness Races in Orbit Apr 24 '16 at 02:58
  • This is hardly very expressive or clear code, though. As evidenced above. I'd write it in production code because it's the best solution C++ has to offer, but sadly that says more about C++ than much else. – Lightness Races in Orbit Apr 24 '16 at 02:59
  • Sth appears to be wrong either with your code or with my ability to copy-paste your code: http://ideone.com/t9ewIP –  Apr 24 '16 at 03:05
  • This is a common pitfall with parsing, due to C++'s complex syntax. That declaration of `vec` looks like a function declaration, and the compiler is trying to parse it, as such, and fails. – Sam Varshavchik Apr 24 '16 at 03:14
  • @gaazkam: Sorry, I've updated my answer with a small fix for that and an explanation of why the code needed another pair of parens. – John Zwinck Apr 24 '16 at 03:14
  • @KL123: You're welcome. If this answer solved your problem, you can "accept" it by clicking the checkmark on the left side of the answer. Welcome to SO. – John Zwinck Apr 24 '16 at 16:14
1

A little easier to understand solution, I think (though less concise):

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string s= "4 5 6";
    stringstream ss(s);
    vector<int> v;

    int hlp;
    while(ss >> hlp)
    {
        v.push_back(hlp);
    }

    for(auto i: v)
        cout << i << '\n';
    return 0;
}

You can use a stringstream just as you could use cin.

http://ideone.com/Y6UfuW