-1

This code pop all the required strings from the stack. But i want to store those string elements in a final one string variable. How to do it?

#include <sstream>
#include <stack>
#include <string>
#include<iostream>
using namespace std;

int main()
{
    istringstream iss("abdd hhh |post_exp| a * b / (c + d) ^ f - g |\\post_exp| anndd jjss");
    stack <string> mudassir;
    string subs;
    while (iss >> subs) {
        if (subs == "|post_exp|")
        {
            while (iss >> subs && subs.find("|\\post_exp|") == string::npos)
            {
                mudassir.push(subs);
               
            }
        }
    }

    while (!mudassir.empty()) {
        mudassir.top();
        mudassir.pop();
    }
    cout << endl;

    return 0;
}
James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

3

Use a while loop

#include <iostream>
#include <stack>
#include <string>
#include <vector>

int main()
{
    std::stack<std::string> stack;
    stack.push("!");
    stack.push("world");
    stack.push("hello ");
    
    std::string str;

    while (!stack.empty())
    {
        str.append(stack.top());
        stack.pop();
    }

    std::cout << str; 
    
    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
2

You're almost good, but in the while-loop you'd like to build the string. This can be done multiple ways, what I'd recommend is:

    std::ostringstream oss;
    while (!mudassir.empty()) {
        oss << mudassir.top();
        mudassir.pop();
    }
    // if you'd like it in a variable,
    // std::string result = oss.str();
    std::cout << oss.str() << std::endl;
lorro
  • 10,687
  • 23
  • 36
  • 1
    Is there a reason to prefer `std::ostringstream` over plain `std::string`? – Evg Nov 05 '22 at 17:00
  • @Evg Mainly that it's more idiomatic and the interface is more like `std::cout`; also, it _might_be_ (but not necessarily is) more performant as it can be optimized for multiple strings to be printed, while `std::string` is not expected to be optimized for `operator+`. – lorro Nov 05 '22 at 17:04
  • I agree with lorro, stream would be more efficient. – Pepijn Kramer Nov 05 '22 at 17:06
  • Actually, if we're optimizing for efficiency, we could 1. query and sum the lengths of each element (if it were not stack but, say, stack emulated on a vector/list), 2. preallocate a string and then copy to it. – lorro Nov 05 '22 at 17:15
  • I doubt that `<<` on a string stream could be more efficient than `+=` on a string. – Evg Nov 05 '22 at 17:19
  • 1
    @Evg `std::ostringstream` is not required to store characters continuously in memory. This goes well with optimization: if you destroy a string that you stream to `std::ostringstream`, you don't need to physically move the memory, just add it to the 'list of buffers'. For `std::string`, this does not hold. – lorro Nov 05 '22 at 21:04
  • 1
    @Evg - but this had been said before me: https://stackoverflow.com/questions/31003445/performance-of-stdstring-operator-versus-stringstream – lorro Nov 05 '22 at 21:07
  • That's interesting. Thanks for your comments. – Evg Nov 05 '22 at 23:03