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;










share|improve this question



















  • 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 a vector?
    – 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














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;










share|improve this question



















  • 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 a vector?
    – 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












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;










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 1:31









1201ProgramAlarm

16k42438




16k42438










asked Nov 10 at 1:12









Zaid A

510




510







  • 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 a vector?
    – 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




    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 a vector?
    – 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












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();






share|improve this answer






















  • 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











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%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

























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();






share|improve this answer






















  • 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















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();






share|improve this answer






















  • 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













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();






share|improve this answer














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();







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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


















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.





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.




draft saved


draft discarded














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





















































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

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20