Program Language/C & C++
[C++] List 에서 pair로 된 데이터 찾기
야곰야곰+책벌레
2022. 9. 21. 17:29
728x90
반응형
list를 사용하다가 두 개의 데이터를 담아야 해서 pair를 사용했다.
찾을 때는 통상 first로 찾았는데, 이는 사실 map을 이용하는 편이 훨씬 편하다.
list에서 pair를 사용하여 둘 다 만족하는 데이터를 검색하는 약간의 뻘짓을 해보았다.
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<pair<string, string>> list1;
list1.emplace_back("1", "1");
list1.emplace_back("2", "1");
list1.emplace_back("2", "3");
string row = "2";
string col = "1";
auto func = [row,col](pair<string, string> const & b) { return b.first == row && b.second == col; };
auto pos = find_if(list1.begin(), list1.end(), func);
cout << pos->first << " | " << pos->second << endl;
return 0;
}
람다 함수와 algorithm의 find_if 를 사용하여 테스틀 해봤는데, 잘 찾았다.
728x90
반응형