srupのメモ帳

競プロで解いた問題や勉強したことを記録していくメモ帳

queueの要素に構造体やclassを使う

概要

queueの中に構造体,classを入れる方法.
以下のように, 構造体やclassを定義して, queue<> の <> のなかに型をいれればいい. STLだからね.
pushするときは, que.push(構造体名{})てな感じでやればいい.
構造体もclassも同じこと.

コード

構造体

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<pint> vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;

struct PAIR{
public:
    int first, second;
};

int main(void){
    queue<PAIR> que;
    que.push(PAIR{1, 2});
    que.push(PAIR{2, 4});
    while(!que.empty()){
        PAIR d = que.front();
        que.pop();
        printf("%d %d\n", d.first, d.second);
    }
    return 0;
}

class

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<pint> vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;

class PAIR{
public:
    int first, second;
};

int main(void){
    queue<PAIR> que;
    que.push(PAIR{1, 2});
    que.push(PAIR{2, 4});
    while(!que.empty()){
        PAIR d = que.front();
        que.pop();
        printf("%d %d\n", d.first, d.second);
    }
    return 0;
}

結果

1 2
2 4