vegazz
vegazz

Reputation: 1441

STL stack with elements of array

Im new to c++. I want to create a stack with arrays. Im using STL. I need to store two values in an array and then push/pop to stack in python i will simply do:

s = Stack()
s.push((1,"item"))
s.push((3,"item"))

so there will be two tuples in stack (1,"item"), (3,"item)

I tried something like this in c++, but its wrong:

stack<string, list<string> > exampleStack;
string test[2] = {"wtf","lol"};
exampleStack.push(dd);

Upvotes: 0

Views: 780

Answers (2)

shauryachats
shauryachats

Reputation: 10385

As you want to store only two values of different datatypes, you can use std::pair.

  1. Create a stack<pair<int,string> >.
  2. To push the pair<int,string> into the stack, use push() function and the make_pair function to make a pair for the push() function.

Code:

stack<pair<int,string> > s;
s.push(make_pair(1,string("item")));
s.push(make_pair(3,string("item")));

EDIT: (Thanks to @imlyc)

If you enable the -std=c++11 flag when compiling with g++, you can replace

s.push(make_pair(1,string("item")));

with

s.push({1,"item"});

Upvotes: 1

pm100
pm100

Reputation: 50110

stack<list<string> > exampleStack;

list<string> l1 = list<string>;
l1.push_back("aaa");
l1.push_back("bbb");
exampleStack.push(l1);

list<string> l2 = list<string>;
l2.push_back("ddddd");
l2.push_back("eeeee");
exampleStack.push(l2);

Upvotes: 0

Related Questions