Trying push a string backwards into a vector












-1















#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

class thing{
public:
vector<unsigned short> vec;
thing(string);
};

int main(){
thing adam("12345");
for(int i = adam.vec.size() - 1;i >= 0;i--){
cout << adam.vec[i] << endl;
}
}

thing::thing(string str){
for(int i = str.size() - 1; i >= 0;i--){
cout << str[i] << endl;
vec.push_back(str[i]);
}
}


I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse, but instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49. This happens when I call push_back() and I'm confused on why.










share|improve this question




















  • 4





    Hint: std::string::rbegin is a thing. As is constructing a string from a pair of (reverse) iterators. You are writing way more code than you need to in order to solve rhis.

    – Jesper Juhl
    Nov 12 '18 at 22:25








  • 3





    This has nothing to do with strings or vectors. You'd get the same results by doing unsigned short n = '5'; cout << n << "n";.

    – melpomene
    Nov 12 '18 at 22:28
















-1















#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

class thing{
public:
vector<unsigned short> vec;
thing(string);
};

int main(){
thing adam("12345");
for(int i = adam.vec.size() - 1;i >= 0;i--){
cout << adam.vec[i] << endl;
}
}

thing::thing(string str){
for(int i = str.size() - 1; i >= 0;i--){
cout << str[i] << endl;
vec.push_back(str[i]);
}
}


I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse, but instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49. This happens when I call push_back() and I'm confused on why.










share|improve this question




















  • 4





    Hint: std::string::rbegin is a thing. As is constructing a string from a pair of (reverse) iterators. You are writing way more code than you need to in order to solve rhis.

    – Jesper Juhl
    Nov 12 '18 at 22:25








  • 3





    This has nothing to do with strings or vectors. You'd get the same results by doing unsigned short n = '5'; cout << n << "n";.

    – melpomene
    Nov 12 '18 at 22:28














-1












-1








-1








#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

class thing{
public:
vector<unsigned short> vec;
thing(string);
};

int main(){
thing adam("12345");
for(int i = adam.vec.size() - 1;i >= 0;i--){
cout << adam.vec[i] << endl;
}
}

thing::thing(string str){
for(int i = str.size() - 1; i >= 0;i--){
cout << str[i] << endl;
vec.push_back(str[i]);
}
}


I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse, but instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49. This happens when I call push_back() and I'm confused on why.










share|improve this question
















#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

class thing{
public:
vector<unsigned short> vec;
thing(string);
};

int main(){
thing adam("12345");
for(int i = adam.vec.size() - 1;i >= 0;i--){
cout << adam.vec[i] << endl;
}
}

thing::thing(string str){
for(int i = str.size() - 1; i >= 0;i--){
cout << str[i] << endl;
vec.push_back(str[i]);
}
}


I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse, but instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49. This happens when I call push_back() and I'm confused on why.







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:22









Remy Lebeau

333k18251446




333k18251446










asked Nov 12 '18 at 22:21









Adam Jean-LaurentAdam Jean-Laurent

1




1








  • 4





    Hint: std::string::rbegin is a thing. As is constructing a string from a pair of (reverse) iterators. You are writing way more code than you need to in order to solve rhis.

    – Jesper Juhl
    Nov 12 '18 at 22:25








  • 3





    This has nothing to do with strings or vectors. You'd get the same results by doing unsigned short n = '5'; cout << n << "n";.

    – melpomene
    Nov 12 '18 at 22:28














  • 4





    Hint: std::string::rbegin is a thing. As is constructing a string from a pair of (reverse) iterators. You are writing way more code than you need to in order to solve rhis.

    – Jesper Juhl
    Nov 12 '18 at 22:25








  • 3





    This has nothing to do with strings or vectors. You'd get the same results by doing unsigned short n = '5'; cout << n << "n";.

    – melpomene
    Nov 12 '18 at 22:28








4




4





Hint: std::string::rbegin is a thing. As is constructing a string from a pair of (reverse) iterators. You are writing way more code than you need to in order to solve rhis.

– Jesper Juhl
Nov 12 '18 at 22:25







Hint: std::string::rbegin is a thing. As is constructing a string from a pair of (reverse) iterators. You are writing way more code than you need to in order to solve rhis.

– Jesper Juhl
Nov 12 '18 at 22:25






3




3





This has nothing to do with strings or vectors. You'd get the same results by doing unsigned short n = '5'; cout << n << "n";.

– melpomene
Nov 12 '18 at 22:28





This has nothing to do with strings or vectors. You'd get the same results by doing unsigned short n = '5'; cout << n << "n";.

– melpomene
Nov 12 '18 at 22:28












2 Answers
2






active

oldest

votes


















2















I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse




You probably don't really want to do that. As @JesperJuhl suggests - you can always just access the string in reverse. Why the needless copy?




instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49




You're mistaking the characters '5', '4', '3' with their integer values. The values of string elements are numbers, but those numbers are indices into some character set. Without going into too much detail about - here's the relevant fragment of the character set in your case:



enter image description here



Indeed, the character '5' has index 53 (decimal) within the character set, '4' has 52 and so on.






share|improve this answer



















  • 1





    We could do without another image of the ASCII table.

    – n.m.
    Nov 12 '18 at 22:43











  • @n.m.: To be honest, this question is rather redundant. If you find a good dupe, I promise to remove my entire answer, not just the table...

    – einpoklum
    Nov 12 '18 at 22:54













  • I couldn't find a dupe though I know there are some.

    – n.m.
    Nov 12 '18 at 22:58



















0















for(int i = adam.vec.size() - 1;i >= 0;i--)
cout << adam.vec[i] << endl;



Your code to insert in reverse order is working just fine. But you are displaying the vector in reverse order also, so it goes back to previous order. To display in correct order, just use:



thing adam("12345");
for(size_t i = 0; i < adam.vec.size(); i++)
cout << adam.vec[i] << ' ';
cout << endl;
//or
for(const auto &element : adam.vec)
cout << element << ' ';
cout << endl;


Your method for(int i = str.size() - 1; i >= 0;i--){...} for iterating backward works in most cases, but casting size in to int can be a problem. The standard library offers other methods. Example:



thing::thing(string str) 
{
std::copy(str.crbegin(), str.crend(), std::back_inserter(vec));
}


Or use iterators to traverse forward or backward.






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%2f53270947%2ftrying-push-a-string-backwards-into-a-vector%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse




    You probably don't really want to do that. As @JesperJuhl suggests - you can always just access the string in reverse. Why the needless copy?




    instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49




    You're mistaking the characters '5', '4', '3' with their integer values. The values of string elements are numbers, but those numbers are indices into some character set. Without going into too much detail about - here's the relevant fragment of the character set in your case:



    enter image description here



    Indeed, the character '5' has index 53 (decimal) within the character set, '4' has 52 and so on.






    share|improve this answer



















    • 1





      We could do without another image of the ASCII table.

      – n.m.
      Nov 12 '18 at 22:43











    • @n.m.: To be honest, this question is rather redundant. If you find a good dupe, I promise to remove my entire answer, not just the table...

      – einpoklum
      Nov 12 '18 at 22:54













    • I couldn't find a dupe though I know there are some.

      – n.m.
      Nov 12 '18 at 22:58
















    2















    I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse




    You probably don't really want to do that. As @JesperJuhl suggests - you can always just access the string in reverse. Why the needless copy?




    instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49




    You're mistaking the characters '5', '4', '3' with their integer values. The values of string elements are numbers, but those numbers are indices into some character set. Without going into too much detail about - here's the relevant fragment of the character set in your case:



    enter image description here



    Indeed, the character '5' has index 53 (decimal) within the character set, '4' has 52 and so on.






    share|improve this answer



















    • 1





      We could do without another image of the ASCII table.

      – n.m.
      Nov 12 '18 at 22:43











    • @n.m.: To be honest, this question is rather redundant. If you find a good dupe, I promise to remove my entire answer, not just the table...

      – einpoklum
      Nov 12 '18 at 22:54













    • I couldn't find a dupe though I know there are some.

      – n.m.
      Nov 12 '18 at 22:58














    2












    2








    2








    I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse




    You probably don't really want to do that. As @JesperJuhl suggests - you can always just access the string in reverse. Why the needless copy?




    instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49




    You're mistaking the characters '5', '4', '3' with their integer values. The values of string elements are numbers, but those numbers are indices into some character set. Without going into too much detail about - here's the relevant fragment of the character set in your case:



    enter image description here



    Indeed, the character '5' has index 53 (decimal) within the character set, '4' has 52 and so on.






    share|improve this answer














    I'm trying to make a constructor that takes in a string and fills the vector with the string in reverse




    You probably don't really want to do that. As @JesperJuhl suggests - you can always just access the string in reverse. Why the needless copy?




    instead of filling the vector with the values 5,4,3,2,1, it fills it with 53,52,51,59,49




    You're mistaking the characters '5', '4', '3' with their integer values. The values of string elements are numbers, but those numbers are indices into some character set. Without going into too much detail about - here's the relevant fragment of the character set in your case:



    enter image description here



    Indeed, the character '5' has index 53 (decimal) within the character set, '4' has 52 and so on.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 '18 at 22:37









    einpoklumeinpoklum

    33.9k27122241




    33.9k27122241








    • 1





      We could do without another image of the ASCII table.

      – n.m.
      Nov 12 '18 at 22:43











    • @n.m.: To be honest, this question is rather redundant. If you find a good dupe, I promise to remove my entire answer, not just the table...

      – einpoklum
      Nov 12 '18 at 22:54













    • I couldn't find a dupe though I know there are some.

      – n.m.
      Nov 12 '18 at 22:58














    • 1





      We could do without another image of the ASCII table.

      – n.m.
      Nov 12 '18 at 22:43











    • @n.m.: To be honest, this question is rather redundant. If you find a good dupe, I promise to remove my entire answer, not just the table...

      – einpoklum
      Nov 12 '18 at 22:54













    • I couldn't find a dupe though I know there are some.

      – n.m.
      Nov 12 '18 at 22:58








    1




    1





    We could do without another image of the ASCII table.

    – n.m.
    Nov 12 '18 at 22:43





    We could do without another image of the ASCII table.

    – n.m.
    Nov 12 '18 at 22:43













    @n.m.: To be honest, this question is rather redundant. If you find a good dupe, I promise to remove my entire answer, not just the table...

    – einpoklum
    Nov 12 '18 at 22:54







    @n.m.: To be honest, this question is rather redundant. If you find a good dupe, I promise to remove my entire answer, not just the table...

    – einpoklum
    Nov 12 '18 at 22:54















    I couldn't find a dupe though I know there are some.

    – n.m.
    Nov 12 '18 at 22:58





    I couldn't find a dupe though I know there are some.

    – n.m.
    Nov 12 '18 at 22:58













    0















    for(int i = adam.vec.size() - 1;i >= 0;i--)
    cout << adam.vec[i] << endl;



    Your code to insert in reverse order is working just fine. But you are displaying the vector in reverse order also, so it goes back to previous order. To display in correct order, just use:



    thing adam("12345");
    for(size_t i = 0; i < adam.vec.size(); i++)
    cout << adam.vec[i] << ' ';
    cout << endl;
    //or
    for(const auto &element : adam.vec)
    cout << element << ' ';
    cout << endl;


    Your method for(int i = str.size() - 1; i >= 0;i--){...} for iterating backward works in most cases, but casting size in to int can be a problem. The standard library offers other methods. Example:



    thing::thing(string str) 
    {
    std::copy(str.crbegin(), str.crend(), std::back_inserter(vec));
    }


    Or use iterators to traverse forward or backward.






    share|improve this answer




























      0















      for(int i = adam.vec.size() - 1;i >= 0;i--)
      cout << adam.vec[i] << endl;



      Your code to insert in reverse order is working just fine. But you are displaying the vector in reverse order also, so it goes back to previous order. To display in correct order, just use:



      thing adam("12345");
      for(size_t i = 0; i < adam.vec.size(); i++)
      cout << adam.vec[i] << ' ';
      cout << endl;
      //or
      for(const auto &element : adam.vec)
      cout << element << ' ';
      cout << endl;


      Your method for(int i = str.size() - 1; i >= 0;i--){...} for iterating backward works in most cases, but casting size in to int can be a problem. The standard library offers other methods. Example:



      thing::thing(string str) 
      {
      std::copy(str.crbegin(), str.crend(), std::back_inserter(vec));
      }


      Or use iterators to traverse forward or backward.






      share|improve this answer


























        0












        0








        0








        for(int i = adam.vec.size() - 1;i >= 0;i--)
        cout << adam.vec[i] << endl;



        Your code to insert in reverse order is working just fine. But you are displaying the vector in reverse order also, so it goes back to previous order. To display in correct order, just use:



        thing adam("12345");
        for(size_t i = 0; i < adam.vec.size(); i++)
        cout << adam.vec[i] << ' ';
        cout << endl;
        //or
        for(const auto &element : adam.vec)
        cout << element << ' ';
        cout << endl;


        Your method for(int i = str.size() - 1; i >= 0;i--){...} for iterating backward works in most cases, but casting size in to int can be a problem. The standard library offers other methods. Example:



        thing::thing(string str) 
        {
        std::copy(str.crbegin(), str.crend(), std::back_inserter(vec));
        }


        Or use iterators to traverse forward or backward.






        share|improve this answer














        for(int i = adam.vec.size() - 1;i >= 0;i--)
        cout << adam.vec[i] << endl;



        Your code to insert in reverse order is working just fine. But you are displaying the vector in reverse order also, so it goes back to previous order. To display in correct order, just use:



        thing adam("12345");
        for(size_t i = 0; i < adam.vec.size(); i++)
        cout << adam.vec[i] << ' ';
        cout << endl;
        //or
        for(const auto &element : adam.vec)
        cout << element << ' ';
        cout << endl;


        Your method for(int i = str.size() - 1; i >= 0;i--){...} for iterating backward works in most cases, but casting size in to int can be a problem. The standard library offers other methods. Example:



        thing::thing(string str) 
        {
        std::copy(str.crbegin(), str.crend(), std::back_inserter(vec));
        }


        Or use iterators to traverse forward or backward.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 5:24









        Barmak ShemiraniBarmak Shemirani

        21.1k42145




        21.1k42145






























            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%2f53270947%2ftrying-push-a-string-backwards-into-a-vector%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

            Coverage of Google Street View

            Full-time equivalent

            Surfing