petercartagena
petercartagena

Reputation: 360

Stack STL with 2 params

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

Upvotes: 6

Views: 8026

Answers (4)

Abhishek Yadav
Abhishek Yadav

Reputation: 11

int main() 
{
    stack <pair<int,int> > s;
    s.push({1,2});
    cout << s.top().first << " " << s.top().second; 
}

Upvotes: 0

wilhelmtell
wilhelmtell

Reputation: 58685

#include <utility>

// ...
stack<pair<string,string> > s;
s.push(make_pair("roses", "red"));

Upvotes: 4

anon
anon

Reputation:

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

int main() {
    stack <pair<int,int> > s;
    s.push( make_pair( 1, 2 ) );
    pair <int, int> p = s.top();
    cout << p.first << " " << p.second << endl;
}

Upvotes: 5

RC.
RC.

Reputation: 28267

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;
}

Upvotes: 6

Related Questions