Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack STL with 2 params

Tags:

c++

stack

stl

Im implementing a B-tree in C++,I have a stack which saves pairs . my problem is, how i put in this stack because push only accept 1 argument. thanks

like image 684
petercartagena Avatar asked May 27 '10 16:05

petercartagena


2 Answers

Use std::pair provided by the standard library.

You can create them with the function make_pair.

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

int main(int argc, char **argv) 
{
    int myInt = 1;
    string myString("stringVal");

    stack<pair<string, int> > myStack; 
    myStack.push(make_pair(myString, myInt));

    return 1;
}
like image 85
RC. Avatar answered Sep 21 '22 20:09

RC.


#include <utility>

// ...
stack<pair<string,string> > s;
s.push(make_pair("roses", "red"));
like image 41
wilhelmtell Avatar answered Sep 20 '22 20:09

wilhelmtell