컴퓨터 언어/C++

[c++] String 자료형은 덧셈(Concat)이 가능하다.

  • -
728x90
반응형

c++에만 존재하는 String자료형의 경우 concat(이어붙이기)가 가능하다.

 

python과 마찬가지로 string에 대해 덧셈을 하면 문자열을 자동적으로 이어서 연결해준다.

 

해당하는 내용을 이용하여 문제를 풀면 다음과 같다.

 

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(void){
    int how_many_repeat;
    cin >> how_many_repeat;
    string string_store[how_many_repeat];

    for(int i = 0; i < how_many_repeat; i++){
        int repeat_value;
        string input_string;
        cin >> repeat_value >> input_string;
        int size_of_string = input_string.size();
        
        for(int n = 0; n < size_of_string; n++){
            for(int j = 0; j < repeat_value; j++){
                string_store[i] += input_string[n];
            }
        }
        
    }
    
    for(int i = 0; i < how_many_repeat; i++){
        cout << string_store[i] << "\n";
    }
    
    return 0;
}
반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.