필수헤더

#include <algorithm>
#include <utility>

정렬

qsort

#include <stdio.h>
#include <stdlib.h>

int comp(const void *a, const void *b)
{
  if ((int *)a > (int *)b)
    return -1;
  if ((int *)a == (int *)b)
    return 0;
  if ((int *)a < (int *)b)
    return 1;
}

int main()
{
  int arr[3] = {10, 62, 3};

  qsort(arr, 3, sizeof(int), comp);

  printf("%d %d %d \n", arr[0], arr[1], arr[2]);

  return 0;
}

자료구조

Queue

#include <stdio.h>
#include <queue>
using namespace std;

int main()
{

  queue<int> que;

  que.push(23);
  que.push(68);
  que.push(3);

  while (!que.empty())
  {
    printf("%d %d %d\n", que.front(), que.empty(), que.size());
    que.pop();
  }

  return 0;
}

Priority Queue

#include <stdio.h>
#include <queue>
using namespace std;

int main()
{
  // 내림차순
  priority_queue<int> pq;

  pq.push(23);
  pq.push(68);
  pq.push(3);

  while (!pq.empty())
  {
    printf("%d %d %d\n", pq.top(), pq.empty(), pq.size());
    pq.pop();
  }

  //
  //
  //
  // 오름차순
  priority_queue<int, vector<int>, greater<int> > pq_asc;

  pq_asc.push(23);
  pq_asc.push(68);
  pq_asc.push(3);

  while (!pq_asc.empty())
  {
    printf("%d %d %d\n", pq_asc.top(), pq_asc.empty(), pq_asc.size());
    pq_asc.pop();
  }

  return 0;
}

Stack

#include <stdio.h>
#include <stack>
using namespace std;

int main()
{

  stack<int> sta;

  sta.push(23);
  sta.push(68);
  sta.push(3);

  while (!sta.empty())
  {
    printf("%d %d %d\n", sta.top(), sta.empty(), sta.size());
    sta.pop();
  }

  return 0;
}

자료형

vector

#include <vector>

vector<int> dArr;

dArr.push_back(1);
dArr.push_back(3);
dArr.push_back(2);

dArr[0];
dArr[1];
dArr[2];

dArr.erase(dArr.begin() + 1);

set

  • vector와 사용방법은 동일
  • 중복이 허용되지 않고, 오름차순으로 정렬

sorted set

#include <set>

struct NODE {
    int dis, id;
};

struct comp {
    bool operator()(NODE *const &a, NODE *const &b) const
    {
        if (a->dist == b->dist)
            return a->id > b->id;
        return a->dist < b->dist;
    }
};

set<NODE *, comp> school[10]; // 학교에 들어간 학생들

Map

#include <unordered_map>

unordered_map<string, node *> table_map;

table_map["abc"];
table_map["def"] = 3;

auto it = table_map.find(mStr);
if (it == table_map.end()) return false;

Pair

#include <stdio.h>
#include <utility>
using namespace std;

int main()
{

  // pair<int, int> p1(1, 3);
  pair<int, int> p1 = make_pair(1, 3);
  // std::pair <int, int>

  printf("%d %d\n", p1.first, p1.second);

  pair<int, int> pArr[3] = {
      make_pair(1, 2),
      make_pair(2, 3),
      make_pair(3, 4),
  };

  for (int i = 0; i < 3; i++)
  {
    printf("%d %d\n", pArr[i].first, pArr[i].second);
  }

  return 0;
}

Tuple

#include <stdio.h>
#include <tuple>

using namespace std;

int main()
{

  tuple<int, int, double> tup = make_tuple(1, 2, 4.8);

  printf("%d %d %lf\n", get<0>(tup), get<1>(tup), get<2>(tup));

  tuple<int, int, double> tArr[3] = {
      make_tuple(1, 2, 3.0),
      make_tuple(2, 3, 4.0),
      make_tuple(3, 4, 5.0),
  };

  for (int i = 0; i < 3; i++)
  {
    printf("%d %d %lf\n", get<0>(tArr[i]), get<1>(tArr[i]), get<2>(tArr[i]));
  }

  return 0;
}