开发者

Stack STL with 2 params

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


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


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


#include <utility>

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜