본문 바로가기
게임 개발/C++

C++ string(n, v)

#include <string>
#include <vector>

using namespace std;

string solution(string my_string, int n) {
    string answer = "";
    for(const auto v : my_string)
    {
        answer += string(n,v);
    }
    return answer;
}

 

이 문제는 받아온 string의 요소 하나하나를 n번씩 출력한 string을 만들어 출력하는 문제다.

hello -> (n=2) hheelllloo

여기서 string(n, v) 는 v를 n개 이어붙인 string이 된다.

 

string(2, 'h') => "hh"