string 함수와 sorting을 할 수 있는 편리한 함수들. sort에 대한 기본 정보를 알고 구현할 수 있어야 하지만, 실제 문제를 풀이할 때는 아래의 함수를 활용하면 편리하다.
#include< algorithm > 을 사용한다.
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b){
return a.first<b.first;
}
int main() {
vector<pair<int, int>> v;
v.push_back(make_back(5, 1));
v.push_back(make_back(4, 2));
v.push_back(make_back(3, 3));
v.push_back(make_back(2, 1));
v.push_back(make_back(1, 2));
sort(v.begin(), v.end(), compare);
}
sort 함수는 compare 함수를 기준으로 하여 어떠한 범위의 값들을 정렬해주는 함수다.
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b){
return a.first<b.first;
}
int main() {
vector<pair<int, int>> v;
v.push_back(make_back(5, 1));
v.push_back(make_back(4, 2));
v.push_back(make_back(3, 3));
v.push_back(make_back(2, 1));
v.push_back(make_back(1, 2));
stable_sort(v.begin(), v.end(), compare);
}
sort와 크게 다르지 않다. 하지만 가장 중요한 것은 기존의 정렬 순서를 유지한다는 것이다. 즉 정렬 조건이 1개 이상일 때 사용하면 유리하다.
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
void trans (string st) {
transform(st.begin(), st,end(), st.begin(), ::tolower);
cout<<st<<endl;
return 0;
}
transform은 시작점과 끝 지점까지의 string을 대문자 또는 소문자로 전환시켜주는 함수이다.
주의점은 시작위치, 끝위치, 변환 시작 위치, 방법 이 순서로 적어줘야 한다.
헤더파일은 algorithm파일이 필요하다.
::tolower -> 대문자 to 소문자
::toupper -> 소문자 to 대문자
#include <vector>
#include <string>
#include <iostream>
using namespace std;
void strSub (string st) {
string result;
result=st.substr(0,st.size());
cout<<result<<endl;
return 0;
}
start에서 end-1 index 까지의 string의 부분을 반환하는 함수
start는 필수고 end는 생략가능(단, 생략시 끝까지)