Advertisement

STL Code Snippet

Started by March 31, 2018 12:16 AM
3 comments, last by Josheir 6 years, 8 months ago

 


int main(){
	stack<string> back_stack;
	stack<string> fwd_stack;
	string temp;

	while(temp!="exit"){
		cout<<"[1] Visit URL   [2] Back    [3]Forward"<<endl;
		cin>>temp;
		if(temp=="exit")
			break;	
		if(temp=="1"){
			cout<<"Enter URL: ";
			cin>>temp;
			back_stack.push(temp);
			fwd_stack=stack<string>();
		}

Hello everyone I'm wondering what the last statement :   fwd_stack=stack<string>();    is doing here?

EDIT: I enclosed the entire program below in case it is needed/wanted!

Thank you,

Josheir

EDIT:  this is the whole program because it may be useful...


int main(){
	stack<string> back_stack;
	stack<string> fwd_stack;
	string temp;

	while(temp!="exit"){
		cout<<"[1] Visit URL   [2] Back    [3]Forward"<<endl;
		cin>>temp;
		if(temp=="exit")
			break;	
		if(temp=="1"){
			cout<<"Enter URL: ";
			cin>>temp;
			back_stack.push(temp);
			fwd_stack=stack<string>();
		}
		else if(temp=="2"){
			cout<<" Going back..."<<endl;
			fwd_stack.push(back_stack.top());
			back_stack.pop();
		}
		else if(temp=="3"){
			if(fwd_stack.empty())
				cout<<"No forward history!"<<endl;
			else{
				cout<<"Going forward... "<<endl;
				back_stack.push(fwd_stack.top());
				fwd_stack.pop();
			}
		}
		if(back_stack.empty())
			break;
		cout<<"\nCurrent URL: "<<back_stack.top()<<endl;
	}
	
	return 0;
}


 

Advertisement

The line assigns a new, empty stack<string> to fwd_stack. Whatever was in fwd_stack before is gone.

To be exact, it calls the parameterless constructor of stack<string> which returns an empty stack of strings.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Thank you, Endurion!

Josheir

This topic is closed to new replies.

Advertisement