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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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?










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
    Nov 17 '18 at 4:52













  • Also, "The error" .. would be good to know.

    – Ted Lyngmo
    Nov 17 '18 at 5:00






  • 2





    #include <bits/stdc++.h> -- Don't do this

    – PaulMcKenzie
    Nov 17 '18 at 5: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

    – PaulMcKenzie
    Nov 17 '18 at 5:05








  • 1





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

    – Richard W
    Nov 17 '18 at 5:50


















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?










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
    Nov 17 '18 at 4:52













  • Also, "The error" .. would be good to know.

    – Ted Lyngmo
    Nov 17 '18 at 5:00






  • 2





    #include <bits/stdc++.h> -- Don't do this

    – PaulMcKenzie
    Nov 17 '18 at 5: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

    – PaulMcKenzie
    Nov 17 '18 at 5:05








  • 1





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

    – Richard W
    Nov 17 '18 at 5:50














0












0








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?










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 Nov 17 '18 at 4:46









Richard WRichard W

435115




435115













  • 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 4:52













  • Also, "The error" .. would be good to know.

    – Ted Lyngmo
    Nov 17 '18 at 5:00






  • 2





    #include <bits/stdc++.h> -- Don't do this

    – PaulMcKenzie
    Nov 17 '18 at 5: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

    – PaulMcKenzie
    Nov 17 '18 at 5:05








  • 1





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

    – Richard W
    Nov 17 '18 at 5:50



















  • 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 4:52













  • Also, "The error" .. would be good to know.

    – Ted Lyngmo
    Nov 17 '18 at 5:00






  • 2





    #include <bits/stdc++.h> -- Don't do this

    – PaulMcKenzie
    Nov 17 '18 at 5: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

    – PaulMcKenzie
    Nov 17 '18 at 5:05








  • 1





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

    – Richard W
    Nov 17 '18 at 5:50

















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 4:52







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 4:52















Also, "The error" .. would be good to know.

– Ted Lyngmo
Nov 17 '18 at 5:00





Also, "The error" .. would be good to know.

– Ted Lyngmo
Nov 17 '18 at 5:00




2




2





#include <bits/stdc++.h> -- Don't do this

– PaulMcKenzie
Nov 17 '18 at 5:03





#include <bits/stdc++.h> -- Don't do this

– PaulMcKenzie
Nov 17 '18 at 5:03




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
Nov 17 '18 at 5:05







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
Nov 17 '18 at 5:05






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>&)'.

– Richard W
Nov 17 '18 at 5:50





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

– Richard W
Nov 17 '18 at 5:50












1 Answer
1






active

oldest

votes


















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





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',
    autoActivateHeartbeat: false,
    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









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





    share|improve this answer






























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





      share|improve this answer




























        3












        3








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





        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 Nov 17 '18 at 7:59

























        answered Nov 17 '18 at 7:42









        Remy LebeauRemy Lebeau

        345k19273466




        345k19273466
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            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

            Bressuire

            Vorschmack

            Quarantine