6
votes

Stack STL avec 2 paramètres

Je suis en train de mettre en œuvre un arbre B en C ++, j'ai une pile qui enregistre des paires. Mon problème est, comment je mets dans cette pile parce que vous n'acceptez que 1 argument. merci


1 commentaires

Pouvez-vous clarifier votre problème? Maintenant, il semble un simple stack.push (std :: make_pair (premier, seconde)); avec pile être un std :: pile > est tout ce dont vous avez besoin.


4 Réponses :


6
votes

Utiliser std :: paire fournie par la bibliothèque standard.

Vous pouvez les créer avec la fonction make_pair . P>

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


1 commentaires

Merci. Je ne sais pas stl. C'est juste que j'en ai besoin. Merci à tout le monde.



4
votes
#include <utility>

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

0 commentaires

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

0 commentaires

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

0 commentaires