stl - Gakgu/Gakgu.github.io GitHub Wiki
C++에서 쓰이는 Standard Template Library.
- key, value 로 구성되어 있다.
#include <map>
#include <iostream>
using namespace std;
int main()
{
map<const char*, double> m;
pair key_value = pair("pi", 3.14);
m.insert(key_value);
cout << m["pi"];
return 0;
}
- 모든 자료형을 담을 수 있다.
- shared_ptr의 속성을 가지고 있다.
- 복사 가능한 자료만 담을 수 있다.
#include <any>
#include <iostream>
int main()
{
std::any ret1 = 1;
std::any ret2 = "asdf";
std::cout
<< std::any_cast<int>(ret1) << "\n"
<< std::any_cast<const char*>(ret2)
<< std::endl;
return 0;
}
- 공용체 형식으로 지정된 타입의 변수를 담을 수 있다.
- 동적 할당을 사용하지 않기 때문에 any보다 속도가 빠르다.
#include <variant>
#include <string>
using namespace std;
int main()
{
variant<int, string> ret = "asdf";
string str = get<string>(ret);
// int i = get<int>(ret); // Error
string* pstr = get_if<string>(&ret);
int* null_pointer = get_if<int>(&ret);
bool equal_type_false = holds_alternative<int>(ret);
bool equal_type_true = holds_alternative<string>(ret);
return 0;
}
- 값이 존재하지 않음을 나타낼 수 있다.
#include <optional>
#include <iostream>
int main()
{
std::optional<int> ret1 = 1;
std::optional<int> ret2 = {};
std::cout
<< ret1.value() << "\n"
<< ret1.value_or(100) << "\n"
<< ret2.value_or(100) << std::endl;
return 0;
}