Print a character based on variable

I know Google Spreadsheets has something like this
=REPT("|",100)
Is there something like that C++ has?
If you just want a single character repeated n times, then a std::string would do.

1
2
3
4
5
6
7
#include <iostream>
#include <string>

int main()
{
    std::cout << std::string (100, '|');
}



Last edited on
1
2
3
4
5
6
7
std::string rept( char c, std::size_t n ) { return std::string( n, c ) ; }

std::string rept( std::string str, std::size_t n ) {

    if( n < 2 ) return str ;
    else return str + rept( str, n-1 ) ;
}

http://coliru.stacked-crooked.com/a/c75c96c5f9a9dee0
char reps[101] = {0};
memset(reps, '|', 100);

only works on byte sized stuff though. Not very useful in general.
Last edited on
Topic archived. No new replies allowed.