C++에서 set 사용법을 간단하게 알아보자


환경 및 선수조건

  • C++


세트 기본 함수

기본형태

  • set<T>: 원하는 자료형 및 클래스 T를 통해 생성

iterator(반복자)

  • begin(): beginning iterator를 반환
  • end(): end iterator를 반환

추가 및 삭제

  • insert(element): 세트에 element를 추가
  • erase(element): 세트에서 해당하는 element를 삭제
  • clear(): 세트에 있는 모든 원소 삭제

조회

  • find(element): element에 해당하는 iterator를 반환
  • count(element): element에 해당하는 개수를 반환

기타

  • empty(): 비어있으면 true 아니면 false를 반환
  • size(): 세트에 포함되어 있는 원소들의 수를 반환

특징

  • 중복을 허용하지 않는다.
  • 중복을 허용하려면 multiset을 사용해야한다.


구현 코드

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main(){

	// set
	set<string> s;


	// insert(element)
	s.insert("abc");
	s.insert("def");
	s.insert("ghi");
	s.insert("jkl");


	// erase(element)
	s.erase("jkl");


	// empty(), size()
	if(!s.empty()) cout << "s size: " << s.size() << '\n';


	// find(element)
	cout << *s.find("abc") << '\n';
	cout << *s.find("def") << '\n';


	// count(element)
	cout << "abc count: " << s.count("abc") << '\n';


	// begin(), end()
	cout << "traverse" << '\n';
	for(auto it = s.begin(); it != s.end(); it++){
		cout << "value: " << *it << '\n';
	}

	return 0;

}


참고자료

C++에서 map 사용법을 간단하게 알아보자


환경 및 선수조건

  • C++


맵 기본 함수

기본형태

  • map<key,value>: key와 value를 pair 형태로 선언합니다.

iterator(반복자)

  • begin(): beginning iterator를 반환
  • end(): end iterator를 반환

추가 및 삭제

  • insert( make_pair(key,value) ): 맵에 원소를 pair 형태로 추가
  • erase(key): 맵에서 key(키값)에 해당하는 원소 삭제
  • clear(): 맵의 원소들 모두 삭제

조회

  • find(key): key(키값)에 해당하는 iterator를 반환
  • count(key): key(키값)에 해당하는 원소들(value들)의 개수를 반환

기타

  • empty(): 맵이 비어있으면 true 아니면 false를 반환
  • size(): 맵 원소들의 수를 반환


구현 코드

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(){

	// map
	// <string, int> => <key, value>
	map< string, int > m;


	// insert(key,value)
	m.insert(make_pair("a", 1));
	m.insert(make_pair("b", 2));
	m.insert(make_pair("c", 3));
	m.insert(make_pair("d", 4));
	m.insert(make_pair("e", 5));
	m["f"] = 6; // also possible


	// erase(key)
	m.erase("d");
	m.erase("e");
	m.erase(m.find("f")); // also possible


	// empty(), size()
	if(!m.empty()) cout << "m size: " << m.size() << '\n';


	// find(key)
	cout << "a: " << m.find("a")->second << '\n';
	cout << "b: " << m.find("b")->second << '\n';


	// count(key)
	cout << "a count: " << m.count("a") << '\n';
	cout << "b count: " << m.count("b") << '\n';


	// begin(), end()
	cout << "traverse" << '\n';
    // map< string, int >::iterator it; also possible
	for(auto it = m.begin(); it != m.end(); it++){
		cout << "key: " << it->first << " " << "value: " << it->second << '\n';
	}

	return 0;

}


참고자료

C++의 next_permutation 함수를 이용해서 조합을 만들어보자


환경 및 선수조건


next_permutation 함수

기본 사용법

위 링크를 통해서 더 자세하게 볼 수 있지만 간단하게나마 정리하고 갑니다.

example.cpp

#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

int main (){

	vector<int> v;

	// 1부터 4까지 대입
	for(int i=0; i<4 ;i++){
		v.push_back(i+1);
	}

	// 정렬
	sort(v.begin(), v.end());

	//순열
	do{
		// 출력
		for(int i=0; i<v.size(); i++){
			printf("%d ", v[i]);
		}

		printf("\n");

	}while(next_permutation(v.begin(), v.end()));

	return 0;

}


중복이 있는 원소들의 경우

중복이 있는 원소의 경우 중복인 경우를 제외하고 순열을 만들어줍니다. 즉, 예를 들어서 0 1 1이 있다면 아래와 같은 경우만 순열을 출력해줍니다.

0 1 1
1 0 1
1 1 0

example.cpp

#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

int main (){

	vector<int> v;

	// 0 1 1 대입
	v.push_back(0);
	v.push_back(1);
	v.push_back(1);

	// 정렬
	sort(v.begin(), v.end());

	//순열
	do{
		// 출력
		for(int i=0; i<v.size(); i++){
			printf("%d ", v[i]);
		}

		printf("\n");

	}while(next_permutation(v.begin(), v.end()));

	return 0;

}


next_permutation을 이용해 조합(Combinataion) 구하기

원리

전체 n개의 원소들 중에서 k개를 뽑는 조합(=nCk)을 구한다면 n개의 벡터 원소에 1k0을 나머지인 n-k개 집어넣어서 순열을 돌리고 1에 해당하는 인덱스만 가져오면 된다.


코드

1부터 6까지의 수 중에서 4개를 뽑아서 조합을 만들어보자.

example.cpp

#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

int main (){

	// 1부터 6까지 담을 벡터
	vector<int> n;

	// 1부터 6까지 생성
	for(int i=0; i<6; i++){
		n.push_back(i+1);
	}

	// 0과1을 저장 할 벡터 생성
	vector<int> ind;

	// k=4, 4개를 뽑으니까
	int k = 4;

	// k개의 1 추가
	for(int i=0; i<k; i++){
		ind.push_back(1);
	}

	// 2개(6개-2개)의 0 추가
	for(int i=0; i<n.size()-k; i++){
		ind.push_back(0);
	}

	// 정렬
	sort(ind.begin(), ind.end());

	//순열
	do{
		// 출력
		for(int i=0; i<ind.size(); i++){
			if(ind[i] == 1){
				printf("%d ", n[i]);
			}
		}

		printf("\n");

	}while(next_permutation(ind.begin(), ind.end()));

	return 0;

}

구현 결과

3 4 5 6
2 4 5 6
2 3 5 6
...
1 2 3 6
1 2 3 5
1 2 3 4


참고자료

How to fix “Page build warning”(about theme setting) mail from github


Environment and Prerequisite

  • Jekyll
  • YAML


Problem

Sometimes we receive mail of which title is Page build warning from github page.

...
You are attempting to use a Jekyll theme, [Your theme name], which is not supported by GitHub Pages.
...


Solutions

There are two solutions


1. Set to comment or erase theme variable in _config.yml

_config.yml

...
#theme: [Any theme name on above link]
...


2. Add proper theme variable value in _config.yml

Set theme value to one of values in https://pages.github.com/themes/

...
theme: [Any theme name on above link]
...


Reference

“Page build warning”이 나올 때 메일이 오는데 해결 방법을 알아보자


환경 및 선수조건

  • Jekyll
  • YAML


문제

다음 아래와 같이 Page build warning이라는 제목으로 메일이 올 때가 있다.

...
You are attempting to use a Jekyll theme, [Your theme name], which is not supported by GitHub Pages.
...


해결방법

2가지 해결방법이 있는데 아래와 같다.


1. _config.yml에서 theme 변수 주석 처리

공식으로 지원하는 theme가 아니면 아래 변수를 주석 처리해주거나 삭제하면 메일이 오지 않는다.

_config.yml

...
#theme: [Any theme name on above link]
...


2. _config.yml에서 theme 변수에 값 제대로 추가

https://pages.github.com/themes/에 나와있는 테마중 하나로 변수를 변경해줍니다.

_config.yml

...
theme: [Any theme name on above link]
...


참고자료