0

I'm trying to parse a string with spaces into several strings and store them into a list, which consists of strings without any space. I do not know how long the input of the string will me and I have the following code:

#include <bits/stdc++.h>
#include <sstream>

using namespace std;

vector<string> myWords;
vector<char> myBuffer;

int main() {
    string mySentence;
    getline(cin, mySentence);

    int j = 0;
    for (int i = 0; i < mySentence.length(); i++) {
        if (mySentence[i] != ' ') myBuffer.push_back(mySentence[i]);
        else {
            myWords.push_back(myBuffer);
            myBuffer.clear();
            j++;
        }
    }

    return 0;
}

The error in which I'm getting is at myWords.push_back(myBuffer);. How do I get around this?

Richard
  • 7,037
  • 2
  • 23
  • 76
  • What does you debug output say that you have you've found? I don't get what you wan't to achieve. – Ted Lyngmo Nov 17 '18 at 04:52
  • Also, "The error" .. would be good to know. – Ted Lyngmo Nov 17 '18 at 05:00
  • 2
    `#include ` -- [Don't do this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – PaulMcKenzie Nov 17 '18 at 05:03
  • 1
    *I'm trying to parse a string with spaces into several strings and store them into a list* -- This is much simpler if you used [std::istringstream](https://www.ideone.com/bPnfVM) – PaulMcKenzie Nov 17 '18 at 05:05
  • Sorry, I've got the solution already. It seems like I should adjust `myBuffer` to `string` type in order to `push_back` it to my vector. – Richard Nov 17 '18 at 05:11
  • @PaulMcKenzie Could you please provide me a link to what `istringstream` does? Your solution is far more efficient, I think. I don't quite understand what `std::istringstream strm(mySentence)` annd `strm >> word` mean. – Richard Nov 17 '18 at 05:15
  • `istringstream` is like `cin` from a string. You put what you want parsed in the stream and then read it out with `>>`. – user4581301 Nov 17 '18 at 05:22
  • @user4581301 So the code `std::istringstream strm(mySentence)` means "Insert the contents of `mySentence` into this new variable called `strm`"? By 'reading' (I assume it's done with the code `strm >> word`, do you mean that I'm taking the strings, separated by a whitespace, and insert it into `word`? Could you give me a short reading/link about this matter and its implementation, if possible :-)? I'd like to see more examples. – Richard Nov 17 '18 at 05:26
  • A great example is option 2 of this answer to the question [Read file line by line using ifstream in C++](https://stackoverflow.com/a/7868998/4581301). – user4581301 Nov 17 '18 at 05:28
  • @user4581301 Is my understanding correct in regard to the above matter (my previous comment)? – Richard Nov 17 '18 at 05:32
  • Still brooding over the `error` ... – Ted Lyngmo Nov 17 '18 at 05:48
  • 1
    @TedLyngmo The error says: `error: no matching function for call to 'std::vector::push_back(std::vector&)'`. – Richard Nov 17 '18 at 05:50
  • @WealthyPlayer My apologies. Yes. – user4581301 Nov 17 '18 at 06:19
  • "Get around" may not be the correct approch. What is your purpose with the push-backs? Describe how you'd like the program to work. – Ted Lyngmo Nov 17 '18 at 06:41
  • @TedLyngmo I'm trying to divide a long sentence, e.g. "I am a horse" that is contained in a `string` type variable to "I", "am", "a", "horse" and insert it into a `vector` – Richard Nov 17 '18 at 07:06

1 Answers1

3

The problem is that you are trying to push a std::vector<char> where a std::string is expected. So simply change the type of myBuffer to a std::string:

#include <iostream>
#include <string>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;
    std::string myBuffer;

    for (int i = 0; i < mySentence.length(); i++) {
        if (mySentence[i] != ' ')
            myBuffer.push_back(mySentence[i]);
        else {
            myWords.push_back(myBuffer);
            myBuffer.clear();
        }
    }

    if (!myBuffer.empty()) {
        myWords.push_back(myBuffer);
    }

    // use myWords as needed...

    return 0;
}

That being said, using a std::istringstream would be much simpler, as operator>> reads whitespace-delimited values from a stream for you:

#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;
    std::string myBuffer;

    std::istringstream iss(mySentence);
    while (iss >> myBuffer) {
        myWords.push_back(myBuffer);
    }

    // use myWords as needed...

    return 0;
}

Alternatively, let the standard library handle the reading and pushing for you:

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

int main() {
    std::string mySentence;
    std::getline(std::cin, mySentence);

    std::vector<std::string> myWords;

    std::istringstream iss(mySentence);
    std::copy(
        std::istream_iterator<std::string>(iss),
        std::istream_iterator<std::string>(),
        std::back_inserter(myWords)
    );

    // use myWords as needed...

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770