👨🏻💻 programming/◽ c, c++
(c++20) STL::Container #2 - contains, starts_with, ends_with
핑크코냥
2024. 6. 17. 18:31
728x90
"c++20 STL::Container #2"
1. contains
기존 map, set 데이터 find함수 사용 시 반복자와 end반복자가 같지 않은 지 비교하는 방식이였다.
std::set s {1, 2, 3, 4, 5};
auto findset = s.find(2);
if (findset != s.end())
{
cout << *findset << endl;
}
std::map<int, int> m {{1, 1000}, { 2, 2000 }};
auto findmap = m.find(2);
if(findmap != m.end())
{
cout << findmap->second <<endl;
}
c++20에서 새로 추가 된 contains는 associate container의 내장함수로 찾으려는 키값을 넣으면 true, false로 그 key의 존재 여부를 알 수 있다.
if (findset.contains(2))
{
cout << "있다!" << endl;
}
if (findmap.contains(2))
{
cout << "있다!" << endl;
}
2. starts_with, ends_with
std::string / string_view 에 추가 된 내장함수로 perfix/suffic checking 접두사와 접미사를 체크 할 수 있는 함수가 c++20에 새로 생겼다.
std::string str = "Hello World";
std::string str2 = "Hello";
bool a = str.starts_with("Hello");
bool b = str.ends_with("World");
bool c = str.starts_with(str2);
a, b, c는 모두 true를 리턴한다.
아마 게임제작에서는 리소스 관리할 때 image_XXX, map_XXX, Effect_XXX 이런식으로 앞에 접두사를 쓰는 경우가 많은데 이때 자주 사용할거 같다.

바이용 ~
1. 내용 출저: 인프런 Inflearn - Rookiss 쌤 강의인 C++20 훑어보기 중.
2.https://en.cppreference.com/w/
728x90