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