Let's talk about the problem I want to solve. Lets say I have a string dtr which is
dtr = " |post_exp| a b * c |\post_exp| and |post_exp| (a b) * c) |\post_exp|."
Now what I am doing is I am pushing data inside first tag (|post_exp| ... |\post_exp|
) into the stack variable mudassir
. And when I find the second tag I am pushing data inside that tag into another variable mudassir2
.
istringstream iss(dtr);
std::ostringstream oss;
sstack <string> mudassir;
sstack <string> mudassir2;
string subs;
int count1 = 0;
string m;
while (iss >> subs) {
if (subs == "|post_exp|")
{
string ab;
count1 ;
if (count1 == 1)
{
while (iss >> subs && subs.find("|\\post_exp|") == string::npos)
{
//ab = subs;
mudassir.push(subs);
}
}
else if (count1 == 2)
{
while (iss >> subs && subs.find("|\\post_exp|") == string::npos)
{
//ab = subs;
mudassir2.push(subs);
}
}
}
}
cout << endl;
cout << endl;
But the problem is When I pop the data of both stack variables. i.e string m and string u. Output of m=c*b a
which is what I want but the output of u=c*b ac)*b) (a
which is wrong. It should print c)*b) (a
.
while (!mudassir.isempty()) {
oss << mudassir.gettop();
mudassir.pop();
}
m = oss.str();
std::cout << m << std::endl;
string u;
while (!mudassir2.isempty()) {
oss << mudassir2.gettop();
mudassir2.pop();
}
u = oss.str();
std::cout << u << std::endl;
CodePudding user response:
You need to make string stream buffer empty.
while (!mudassir.isempty()) {
oss << mudassir.gettop();
mudassir.pop();
}
m = oss.str();
oss.str("");
std::cout << m << std::endl;
string u;
while (!mudassir2.isempty()) {
oss << mudassir2.gettop();
mudassir2.pop();
}
u = oss.str();
std::cout << u << std::endl;