push_back all contents in vector to combine them as the first element of vector











up vote
0
down vote

favorite












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?










share|improve this question






















  • What does you debug output say that you have you've found? I don't get what you wan't to achieve.
    – Ted Lyngmo
    8 hours ago












  • Also, "The error" .. would be good to know.
    – Ted Lyngmo
    8 hours ago






  • 2




    #include <bits/stdc++.h> -- Don't do this
    – PaulMcKenzie
    8 hours ago






  • 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
    – PaulMcKenzie
    8 hours ago








  • 1




    @TedLyngmo The error says: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'.
    – Wealthy Player
    7 hours ago















up vote
0
down vote

favorite












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?










share|improve this question






















  • What does you debug output say that you have you've found? I don't get what you wan't to achieve.
    – Ted Lyngmo
    8 hours ago












  • Also, "The error" .. would be good to know.
    – Ted Lyngmo
    8 hours ago






  • 2




    #include <bits/stdc++.h> -- Don't do this
    – PaulMcKenzie
    8 hours ago






  • 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
    – PaulMcKenzie
    8 hours ago








  • 1




    @TedLyngmo The error says: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'.
    – Wealthy Player
    7 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question













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?







c++ string






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









Wealthy Player

1506




1506












  • What does you debug output say that you have you've found? I don't get what you wan't to achieve.
    – Ted Lyngmo
    8 hours ago












  • Also, "The error" .. would be good to know.
    – Ted Lyngmo
    8 hours ago






  • 2




    #include <bits/stdc++.h> -- Don't do this
    – PaulMcKenzie
    8 hours ago






  • 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
    – PaulMcKenzie
    8 hours ago








  • 1




    @TedLyngmo The error says: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'.
    – Wealthy Player
    7 hours ago


















  • What does you debug output say that you have you've found? I don't get what you wan't to achieve.
    – Ted Lyngmo
    8 hours ago












  • Also, "The error" .. would be good to know.
    – Ted Lyngmo
    8 hours ago






  • 2




    #include <bits/stdc++.h> -- Don't do this
    – PaulMcKenzie
    8 hours ago






  • 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
    – PaulMcKenzie
    8 hours ago








  • 1




    @TedLyngmo The error says: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'.
    – Wealthy Player
    7 hours ago
















What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago






What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago














Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago




Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago




2




2




#include <bits/stdc++.h> -- Don't do this
– PaulMcKenzie
8 hours ago




#include <bits/stdc++.h> -- Don't do this
– PaulMcKenzie
8 hours ago




1




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
– PaulMcKenzie
8 hours ago






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
– PaulMcKenzie
8 hours ago






1




1




@TedLyngmo The error says: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'.
– Wealthy Player
7 hours ago




@TedLyngmo The error says: error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'.
– Wealthy Player
7 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348309%2fpush-back-all-contents-in-vectorchar-to-combine-them-as-the-first-element-of-v%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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;
    }





    share|improve this answer



























      up vote
      0
      down vote













      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;
      }





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        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;
        }





        share|improve this answer














        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;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 5 hours ago

























        answered 5 hours ago









        Remy Lebeau

        326k18244430




        326k18244430






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348309%2fpush-back-all-contents-in-vectorchar-to-combine-them-as-the-first-element-of-v%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin