(c++20) STL::Container #2 - contains, starts_with, ends_with👨🏻💻 programming/◽ c, c++2024. 6. 17. 18:31
Table of Contents
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
'👨🏻💻 programming > ◽ c, c++' 카테고리의 다른 글
(c++) 함수 뒤 const (0) | 2024.06.27 |
---|---|
(c++20) STL::Container #1 - std::to_array, erase, erase_if (0) | 2024.06.17 |
(c++20) Conditional Explicit Constructor (0) | 2024.06.17 |
(c++20) [Three-way Comparsion(3방향 비교 연산자)/우주선] 연산자 (0) | 2024.06.14 |
(c++20) consteval, constinit (0) | 2024.06.14 |
@핑크코냥 :: 핑크코냥
안 하는 것 보다 낫겠지
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!