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

using namespace std;


vector<string> split_string(string s)
{
string buf;
stringstream ss(s);

vector<string> tokens;

while (ss >> buf)
    tokens.push_back(buf);

return tokens;
}


int main()
{
   cout << split_string("Alpha Beta Gamma");
}

when i try to split a string into a vector by using whitespaces i am not able to print out my solution.

i doesnt let me use std::cout but in my function the return value is given

why cant i use it like that? how do i fix this?

2
  • 1
    There is no overload operator<< for std::vector. Commented Nov 29, 2016 at 18:48
  • Is this is the same assignment as this one? Commented Nov 29, 2016 at 18:48

1 Answer 1

1

std::cout cannot take a vector, you need to iterate through the container and print each element separately, try using something like this:

int main()
{
    string originalString = "Alpha Beta Gamma";  

    for (const auto& str : split_string(originalString))
        cout << str << '\n';

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.