What is the most efficient way of outputting strings stored in a stack but with the letters of the strings reversed in order?
up vote
-2
down vote
favorite
What the program does is allows the user to input a bunch of strings, stores them in a stack, and then outputs all of those strings in the stack but in reverse order and flipped. Is there a more efficient way of doing what I did? Maybe using a different data structure (other than a stack)? I though a stack would be best because the strings stored in the stacks need to be outputted in the opposite order that they were inputted.
Here's the code:
#include <iostream>
#include <stack>
using namespace std;
int main()
stack <string> elements;
string element;
cout << "Hello, welcome to Flippy-McBackwardson! nn" << endl;
cout << "Please enter a bunch of strings (type END to terminate your list): " << endl;
do
getline(cin, element);
elements.push(element);
while (element != "END");
elements.pop();
cout << "nFlippy Backward Version: " << endl;
for (int x = elements.size()-1; x >= 0; x--)
for (int i = elements.top().length()-1; i >= 0; i--)
cout << elements.top()[i];
elements.pop();
cout << endl;
return 0;
c++ stack
add a comment |
up vote
-2
down vote
favorite
What the program does is allows the user to input a bunch of strings, stores them in a stack, and then outputs all of those strings in the stack but in reverse order and flipped. Is there a more efficient way of doing what I did? Maybe using a different data structure (other than a stack)? I though a stack would be best because the strings stored in the stacks need to be outputted in the opposite order that they were inputted.
Here's the code:
#include <iostream>
#include <stack>
using namespace std;
int main()
stack <string> elements;
string element;
cout << "Hello, welcome to Flippy-McBackwardson! nn" << endl;
cout << "Please enter a bunch of strings (type END to terminate your list): " << endl;
do
getline(cin, element);
elements.push(element);
while (element != "END");
elements.pop();
cout << "nFlippy Backward Version: " << endl;
for (int x = elements.size()-1; x >= 0; x--)
for (int i = elements.top().length()-1; i >= 0; i--)
cout << elements.top()[i];
elements.pop();
cout << endl;
return 0;
c++ stack
1
Probably should be astd::reverse
in there somewhere.
– user4581301
Nov 10 at 1:16
1
Is there a reason you're using astack
rather than avector
?
– Spencer
Nov 10 at 1:16
2
What makes you worried about the efficiency of this code ?
– Sid S
Nov 10 at 1:17
@SidS I am worried about efficiency because I am getting graded on efficiency in this computer science course that I am taking, so I just needed help with that. I am sure that the way I did it is reasonably efficient, but I just wanted to see if there are more efficient ways of solving this problem and why. That's all. Just had a question to ask and wanted to learn. I don't know why my question got down voted. By the way, this isn't my homework, I just thought of this and wanted to see if there are more efficient ways of solving it
– Zaid A
Nov 11 at 2:14
@Zaid, In that case I would show this code to your professor or TA and ask them how it would be graded and whether it's necessary to improve it, and if so - how.
– Sid S
Nov 11 at 2:21
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
What the program does is allows the user to input a bunch of strings, stores them in a stack, and then outputs all of those strings in the stack but in reverse order and flipped. Is there a more efficient way of doing what I did? Maybe using a different data structure (other than a stack)? I though a stack would be best because the strings stored in the stacks need to be outputted in the opposite order that they were inputted.
Here's the code:
#include <iostream>
#include <stack>
using namespace std;
int main()
stack <string> elements;
string element;
cout << "Hello, welcome to Flippy-McBackwardson! nn" << endl;
cout << "Please enter a bunch of strings (type END to terminate your list): " << endl;
do
getline(cin, element);
elements.push(element);
while (element != "END");
elements.pop();
cout << "nFlippy Backward Version: " << endl;
for (int x = elements.size()-1; x >= 0; x--)
for (int i = elements.top().length()-1; i >= 0; i--)
cout << elements.top()[i];
elements.pop();
cout << endl;
return 0;
c++ stack
What the program does is allows the user to input a bunch of strings, stores them in a stack, and then outputs all of those strings in the stack but in reverse order and flipped. Is there a more efficient way of doing what I did? Maybe using a different data structure (other than a stack)? I though a stack would be best because the strings stored in the stacks need to be outputted in the opposite order that they were inputted.
Here's the code:
#include <iostream>
#include <stack>
using namespace std;
int main()
stack <string> elements;
string element;
cout << "Hello, welcome to Flippy-McBackwardson! nn" << endl;
cout << "Please enter a bunch of strings (type END to terminate your list): " << endl;
do
getline(cin, element);
elements.push(element);
while (element != "END");
elements.pop();
cout << "nFlippy Backward Version: " << endl;
for (int x = elements.size()-1; x >= 0; x--)
for (int i = elements.top().length()-1; i >= 0; i--)
cout << elements.top()[i];
elements.pop();
cout << endl;
return 0;
c++ stack
c++ stack
edited Nov 10 at 1:31
1201ProgramAlarm
16k42438
16k42438
asked Nov 10 at 1:12
Zaid A
510
510
1
Probably should be astd::reverse
in there somewhere.
– user4581301
Nov 10 at 1:16
1
Is there a reason you're using astack
rather than avector
?
– Spencer
Nov 10 at 1:16
2
What makes you worried about the efficiency of this code ?
– Sid S
Nov 10 at 1:17
@SidS I am worried about efficiency because I am getting graded on efficiency in this computer science course that I am taking, so I just needed help with that. I am sure that the way I did it is reasonably efficient, but I just wanted to see if there are more efficient ways of solving this problem and why. That's all. Just had a question to ask and wanted to learn. I don't know why my question got down voted. By the way, this isn't my homework, I just thought of this and wanted to see if there are more efficient ways of solving it
– Zaid A
Nov 11 at 2:14
@Zaid, In that case I would show this code to your professor or TA and ask them how it would be graded and whether it's necessary to improve it, and if so - how.
– Sid S
Nov 11 at 2:21
add a comment |
1
Probably should be astd::reverse
in there somewhere.
– user4581301
Nov 10 at 1:16
1
Is there a reason you're using astack
rather than avector
?
– Spencer
Nov 10 at 1:16
2
What makes you worried about the efficiency of this code ?
– Sid S
Nov 10 at 1:17
@SidS I am worried about efficiency because I am getting graded on efficiency in this computer science course that I am taking, so I just needed help with that. I am sure that the way I did it is reasonably efficient, but I just wanted to see if there are more efficient ways of solving this problem and why. That's all. Just had a question to ask and wanted to learn. I don't know why my question got down voted. By the way, this isn't my homework, I just thought of this and wanted to see if there are more efficient ways of solving it
– Zaid A
Nov 11 at 2:14
@Zaid, In that case I would show this code to your professor or TA and ask them how it would be graded and whether it's necessary to improve it, and if so - how.
– Sid S
Nov 11 at 2:21
1
1
Probably should be a
std::reverse
in there somewhere.– user4581301
Nov 10 at 1:16
Probably should be a
std::reverse
in there somewhere.– user4581301
Nov 10 at 1:16
1
1
Is there a reason you're using a
stack
rather than a vector
?– Spencer
Nov 10 at 1:16
Is there a reason you're using a
stack
rather than a vector
?– Spencer
Nov 10 at 1:16
2
2
What makes you worried about the efficiency of this code ?
– Sid S
Nov 10 at 1:17
What makes you worried about the efficiency of this code ?
– Sid S
Nov 10 at 1:17
@SidS I am worried about efficiency because I am getting graded on efficiency in this computer science course that I am taking, so I just needed help with that. I am sure that the way I did it is reasonably efficient, but I just wanted to see if there are more efficient ways of solving this problem and why. That's all. Just had a question to ask and wanted to learn. I don't know why my question got down voted. By the way, this isn't my homework, I just thought of this and wanted to see if there are more efficient ways of solving it
– Zaid A
Nov 11 at 2:14
@SidS I am worried about efficiency because I am getting graded on efficiency in this computer science course that I am taking, so I just needed help with that. I am sure that the way I did it is reasonably efficient, but I just wanted to see if there are more efficient ways of solving this problem and why. That's all. Just had a question to ask and wanted to learn. I don't know why my question got down voted. By the way, this isn't my homework, I just thought of this and wanted to see if there are more efficient ways of solving it
– Zaid A
Nov 11 at 2:14
@Zaid, In that case I would show this code to your professor or TA and ask them how it would be graded and whether it's necessary to improve it, and if so - how.
– Sid S
Nov 11 at 2:21
@Zaid, In that case I would show this code to your professor or TA and ask them how it would be graded and whether it's necessary to improve it, and if so - how.
– Sid S
Nov 11 at 2:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You can use reverse iterators (accessible using std::rbegin()
and std::rend()
) which should be cleaner and safer as they don't involve tricky subscripting math that is prone to off-by-one errors (at the very least).
while(!elements.empty())
std::for_each(std::rbegin(elements.top()), std::rend(elements.top()),
(char c) std::cout << c; );
std::cout << 'n';
elements.pop();
What exactly are iterators?
– Zaid A
Nov 11 at 2:14
@Galik, While this is good advice, it doesn't answer the question.
– Sid S
Nov 11 at 2:23
@SidS I suppose I could argue that there are different kinds of "efficiency" to be considered. :)
– Galik
Nov 11 at 2:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use reverse iterators (accessible using std::rbegin()
and std::rend()
) which should be cleaner and safer as they don't involve tricky subscripting math that is prone to off-by-one errors (at the very least).
while(!elements.empty())
std::for_each(std::rbegin(elements.top()), std::rend(elements.top()),
(char c) std::cout << c; );
std::cout << 'n';
elements.pop();
What exactly are iterators?
– Zaid A
Nov 11 at 2:14
@Galik, While this is good advice, it doesn't answer the question.
– Sid S
Nov 11 at 2:23
@SidS I suppose I could argue that there are different kinds of "efficiency" to be considered. :)
– Galik
Nov 11 at 2:32
add a comment |
up vote
1
down vote
You can use reverse iterators (accessible using std::rbegin()
and std::rend()
) which should be cleaner and safer as they don't involve tricky subscripting math that is prone to off-by-one errors (at the very least).
while(!elements.empty())
std::for_each(std::rbegin(elements.top()), std::rend(elements.top()),
(char c) std::cout << c; );
std::cout << 'n';
elements.pop();
What exactly are iterators?
– Zaid A
Nov 11 at 2:14
@Galik, While this is good advice, it doesn't answer the question.
– Sid S
Nov 11 at 2:23
@SidS I suppose I could argue that there are different kinds of "efficiency" to be considered. :)
– Galik
Nov 11 at 2:32
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use reverse iterators (accessible using std::rbegin()
and std::rend()
) which should be cleaner and safer as they don't involve tricky subscripting math that is prone to off-by-one errors (at the very least).
while(!elements.empty())
std::for_each(std::rbegin(elements.top()), std::rend(elements.top()),
(char c) std::cout << c; );
std::cout << 'n';
elements.pop();
You can use reverse iterators (accessible using std::rbegin()
and std::rend()
) which should be cleaner and safer as they don't involve tricky subscripting math that is prone to off-by-one errors (at the very least).
while(!elements.empty())
std::for_each(std::rbegin(elements.top()), std::rend(elements.top()),
(char c) std::cout << c; );
std::cout << 'n';
elements.pop();
edited Nov 10 at 1:39
answered Nov 10 at 1:29
Galik
33.1k34674
33.1k34674
What exactly are iterators?
– Zaid A
Nov 11 at 2:14
@Galik, While this is good advice, it doesn't answer the question.
– Sid S
Nov 11 at 2:23
@SidS I suppose I could argue that there are different kinds of "efficiency" to be considered. :)
– Galik
Nov 11 at 2:32
add a comment |
What exactly are iterators?
– Zaid A
Nov 11 at 2:14
@Galik, While this is good advice, it doesn't answer the question.
– Sid S
Nov 11 at 2:23
@SidS I suppose I could argue that there are different kinds of "efficiency" to be considered. :)
– Galik
Nov 11 at 2:32
What exactly are iterators?
– Zaid A
Nov 11 at 2:14
What exactly are iterators?
– Zaid A
Nov 11 at 2:14
@Galik, While this is good advice, it doesn't answer the question.
– Sid S
Nov 11 at 2:23
@Galik, While this is good advice, it doesn't answer the question.
– Sid S
Nov 11 at 2:23
@SidS I suppose I could argue that there are different kinds of "efficiency" to be considered. :)
– Galik
Nov 11 at 2:32
@SidS I suppose I could argue that there are different kinds of "efficiency" to be considered. :)
– Galik
Nov 11 at 2:32
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%2f53235183%2fwhat-is-the-most-efficient-way-of-outputting-strings-stored-in-a-stack-but-with%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
1
Probably should be a
std::reverse
in there somewhere.– user4581301
Nov 10 at 1:16
1
Is there a reason you're using a
stack
rather than avector
?– Spencer
Nov 10 at 1:16
2
What makes you worried about the efficiency of this code ?
– Sid S
Nov 10 at 1:17
@SidS I am worried about efficiency because I am getting graded on efficiency in this computer science course that I am taking, so I just needed help with that. I am sure that the way I did it is reasonably efficient, but I just wanted to see if there are more efficient ways of solving this problem and why. That's all. Just had a question to ask and wanted to learn. I don't know why my question got down voted. By the way, this isn't my homework, I just thought of this and wanted to see if there are more efficient ways of solving it
– Zaid A
Nov 11 at 2:14
@Zaid, In that case I would show this code to your professor or TA and ask them how it would be graded and whether it's necessary to improve it, and if so - how.
– Sid S
Nov 11 at 2:21