1️⃣ 오늘 할 일

<aside>

2️⃣ 공부한 내용 정리

코드카타

<aside>

정수 → 문자열 변환(to_string)을 고려하는 경우


#include <iostream>
#include <string>

int main() {
    long long n = 14275;
    std::string s = std::to_string(n); // "14275"
    
    // 각 자리 출력 or 각 자리수 합
    int sum = 0;
    for(char c : s){
        std::cout << c << " "; // 1 4 2 7 5
        sum += (c - '0'); // // '0' = 48 ASCII
    }
    
    //자리수 뒤집기 or 팰린드롬 검사
    std::string r = s;
    std::reverse(r.begin(), r.end()); // "57241"
    bool isPalindrome = (s == r); // false
		long long rev = std::stoll(r);    // 숫자로 다시 변환 가능 //string to long long
		
		
		std::string a = "100000000000000000000";
		std::string b = "99999999999999999999";

		// long long으로 표현할 수 있는 (약 9×10^18)을 넘어간 수끼리 비교
		if(a.length() > b.length()){
				std::cout << "a is bigger\\n";
		}else if(a.length() < b.length()){
				std::cout << "b is bigger\\n";
		}else if(a > b){ // 자리수 같으면 사전순 비교
				std::cout << "a is bigger\\n";
		}else {
				std::cout << "b is bigger\\n";
		}
		
		return 0;
}

<algorithm>


#include <string>
#include <vector>
#include <algorithm>

// std::sort 예시
// n번째 char로 오름차순 정렬하는데, 같으면 사전식 오름차순하는 sorting
// [capture] (parameter) -> return_type { body } 
// [capture] (parameter) { body } // 리턴타입은 컴파일러가 return 문을 보고 자동 추론

std::vector<std::string> solution(std::vector<std::string> strings, int n) {
    std::sort(
        strings.begin(),
        strings.end(),
        [&n](std::string a, std::string b) //바깥변수 n을 볼 수 있도록
        {
            if(a[n] == b[n]){
                return a < b; //bool 반환
            }
            return a[n] < b[n]; //bool 반환
        }
    );
    return strings;
}
// std::transform 예시
// [capture] (parameter) -> return_type { body } // 익명 함수
#include <string>
#include <vector>
#include <algorithm>
#include <cctype> //::toupper()

std::vector<int> src = {1, 2, 3, 4};
std::vector<int> dst(src.size()); //사이즈 미리 지정해주기

std::transform(
		src.begin(),
    src.end(),
    dst.begin(),
    [](int x) { return x * 2; }
);

//대문자 변환 안전 ver.
std::transform(
    s.begin(),
    s.end(),
    s.begin(),
    [](unsigned char c) //UB(Undefined Behavior) 방지용, 양수만 받기
    {
        return std::toupper(c); //int toupper(int ch); 이어서 UB 발생 가능
    }
);
// std::count_if 예시

// 'a' 이상 'e' 이하인 문자 개수
int cnt = std::count_if(s.begin(), s.end(),
    [](char c){ return c >= 'a' && c <= 'e'; });

<numeric>


함수 설명
accumulate() init + 모든 원소 std::accumulate(v.begin(), v.end(), init);
std::accumulate(v.begin(), v.end(), 0);
std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()) • 원소 누적합을 위해서는 그냥 init에 0 넣기
• 누적곱 때는 1
partial_sum() prefix sum(누적합 배열) 생성 std::vector<int> v2 = { 1,2,3,4 };
std::vector<int> prefix(4);

std::partial_sum(v2.begin(), v2.end(), prefix.begin()); //1 3 6 10 | • 구간합에 유용 • prefix[3] - prefix[1] == 7 | | iota | 연속 값 채우기 | std::iota(v.begin(), v.end(), 5); //5 6 7 8 9 | • permutation 시작용으로 매우 자주 사용 | | gcd(a, b) lcm(a, b) | 최대공약수 최소공배수 | std::gcd(a, b) std::lcm(a, b) | • Greatest common divisor • Least common multiple |

<cmath>


C++

<aside>

</aside>

<aside>

</aside>

3️⃣ 오늘의 이슈와 해결 방법

<aside>

</aside>

4️⃣ 내일 할 일

<aside>

</aside>