헤더파일

문자열 함수 본문

C++

문자열 함수

헤더파일 2018. 6. 11. 15:20

제일 긴 단어 찾기


auto p = max_element(v.begin(), v.end(), [](string a, string b)

{

return a.length() < b.length();

});


cout << "길이가 제일 긴 단어: " << *p << endl;




긴 단어 20개 뽑기


nth_element(v.begin(), v.begin() + 20, v.end(), [](const string& a, const string& b)

{

return a.length() > b.length();

});


정렬된 걸 보증하지 않는다. 길이가 긴 100개만 뽑을 뿐 긴 순서대로 정렬하지 않는다.


순서대로 20개를 정렬하고 싶다면 partial_sort를 써야한다.


partial_sort(v.begin(), v.begin() + 100, v.end(), [](const string& a, const string& b)

{

return a.length() > b.length();

});




s로 시작하는 단어 개수 찾기


auto count = count_if(v.begin(), v.end(), [](const string& s) {

return *s.begin() == 's';

});


파티션을 써도 되지만 시간 복잡도면에서 손해를 본다. 하지만 count_if를 써도 5점짜리 답이다.

처음 s로 시작하는 단어 위치에서 t로 시작하는 단어 위치를 뺴면 된다.



단어를 받아서 사전에서 찾기


string input;

cin >> input;

sort(input.begin(), input.end());


auto p = find_if(vv.begin(), vv.end(),[&input](PS a)

{

return a.first == input;

});

for (auto iter = p; (*iter).first == (*p).first; iter++)

{

cout << (*iter).first<<"  "<<(*iter).second << endl;

}


'C++' 카테고리의 다른 글

C++ 정리2  (0) 2019.10.30
C++ 정리  (0) 2019.10.29
알고리즘 함수  (0) 2018.05.31
반복자  (0) 2018.05.21
Map을 이용한 파일입출력  (0) 2018.05.14
Comments