string(size_t count, char ch)

김민수·2025년 1월 22일

C++

목록 보기
61/68
  • 이 생성자는 주어진 문자 chcount만큼 반복하여 문자열을 초기화한다.
  • 예를 들어, std::string str(5, 'a');"aaaaa"라는 문자열을 생성한다.

예시

#include <iostream>
#include <string>

int main() {
    // 문자 'x'를 10번 반복한 문자열 생성
    std::string repeatedStr(10, 'x');

    // 결과 출력
    std::cout << "생성된 문자열: " << repeatedStr << std::endl;

    // 다른 예제: '*'를 5번 반복
    std::string stars(5, '*');
    std::cout << "별 모양 문자열: " << stars << std::endl;

    return 0;
}

출력

생성된 문자열: xxxxxxxxxx
별 모양 문자열: *****
profile
안녕하세요

0개의 댓글