Trying push a string backwards into a vector
#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++
add a comment |
#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++
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 doingunsigned short n = '5'; cout << n << "n";
.
– melpomene
Nov 12 '18 at 22:28
add a comment |
#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++
#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++
c++
edited Nov 12 '18 at 23:22
Remy Lebeau
331k18251443
331k18251443
asked Nov 12 '18 at 22:21
Adam 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 doingunsigned short n = '5'; cout << n << "n";
.
– melpomene
Nov 12 '18 at 22:28
add a comment |
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 doingunsigned 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
add a comment |
2 Answers
2
active
oldest
votes
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:
Indeed, the character '5'
has index 53 (decimal) within the character set, '4'
has 52 and so on.
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
Indeed, the character '5'
has index 53 (decimal) within the character set, '4'
has 52 and so on.
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
add a comment |
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:
Indeed, the character '5'
has index 53 (decimal) within the character set, '4'
has 52 and so on.
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
add a comment |
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:
Indeed, the character '5'
has index 53 (decimal) within the character set, '4'
has 52 and so on.
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:
Indeed, the character '5'
has index 53 (decimal) within the character set, '4'
has 52 and so on.
answered Nov 12 '18 at 22:37
einpoklum
33.4k26118234
33.4k26118234
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 5:24
Barmak Shemirani
20.8k42045
20.8k42045
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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