람다 함수로 사용자 설정 정의해 정렬하기
·
공부/Code Cata
#include #include #include using namespace std;vector solution(vector strings, int n) { sort(strings.begin(), strings.end(), [n](const string& a, const string& b) { if(a[n] == b[n]) { return a 비교할 인덱스 값 n을 capture해서 람다 함수로 넘겨주어 두 인덱스의 값이 같다면 사전순으로 정렬하고, 같지 않다면 바로 오름차순 정렬하였다. 람다함수[capture](parameters) -> return_type { // function body};capture: 람다 함수 외부의 변수를 가져오는 방식..
24.12.31 (화)
·
내배캠/TIL
std::vector 컨테이너를 class로 약식 구현해보았다.https://dong-grae.tistory.com/85 std::vector를 class로 구현#include #include using namespace std;template class SimpleVector{private: T* Data; int CurrentSize; int CurrentCapacity;public: // 기본 생성자 SimpleVector() : CurrentSize(0), CurrentCapacity(10) { Data = new T[10](); } // 매개변수를 받는 생dong-grae.tistory.com   문자열에 있는 영문으로 표기된 숫자를 실제 숫자로 바꾸는 문제를 풀었는데, 다른 사람의 풀이를 보다..
std::vector를 class로 구현
·
내배캠/C++
#include #include using namespace std;template class SimpleVector{private: T* Data; int CurrentSize; int CurrentCapacity;public: // 기본 생성자 SimpleVector() : CurrentSize(0), CurrentCapacity(10) { Data = new T[10](); } // 매개변수를 받는 생성자 SimpleVector(int Capacity) : CurrentSize(0), CurrentCapacity(Capacity) { Data = new T[CurrentCapacity](); } // 복사 생성자 SimpleVector(const SimpleVector& Other) : Curr..
std::regex_replace
·
내배캠/C++
regex_replace정규 표현식(regex)을 사용하여 문자열에서 특정 패턴을 찾아 다른 문자열로 교체하는 함수이다.C++ 표준 라이브러리의 헤더에 포함되어 있다. string regex_replace(const string& input, const regex& pattern, const string& replacement);input 문자열에서 pattern에 매칭되는 모든 부분을 replacement로 교체한 결과를 반환한다.input: 원본 문자열pattern: 찾고자 하는 패턴을 나타내는 정규식 객체replacement: 교체할 문자열  https://dong-grae.tistory.com/83 문자열에서 영문 숫자 변환하기*unordered_map 사용한 초기 코드더보기#include #i..
문자열에서 영문 숫자 변환하기
·
공부/Code Cata
*unordered_map 사용한 초기 코드더보기#include #include #include using namespace std;string ConvertStirng(const unordered_map NumList, const string& s){ string Result; string Temp; for (char c : s) { // 알파벳 발견 시, 한 글자씩 unordered_map 리스트에 검색하여 Result에 저장 if (isalpha(c)) { Temp += c; auto it = NumList.find(Temp); if (it != NumList.end()) ..
시저 암호(알파벳 순환 이동)
·
공부/Code Cata
#include #include using namespace std;string solution(string s, int n) { for(int i = 0; i = 'a') ? 'a' : 'A'; s[i] = Case + (NowChar - Case + n) % 26; } return s;}s[i] = Case + (NowChar - 'A' + n)  % 26 26개의 알파벳을 'A'를 0부터  'Z'를 25까지의 숫자로 변환한다.만약 NowChar가 'Z'라고 가정하면 'Z' - 'A'의 값은 25가 된다.여기서 n의 값이 3이라고 가정한다면 28 % 26의 계산식이 되고최종적으로 'A' + 2의 값인 'C'가 s[i]에 들어가게 된다.