How to repeat a line the number of times in C ++?

I want to insert 'n' spaces (or any line) at the beginning of a line in C ++. Is there any direct way to do this using std :: string or char * strings?

eg. in Python you can just do

>>> "." * 5 + "lolcat" '.....lolcat' 
+82
c ++
Oct 03 '08 at 12:43
source share
8 answers
 std::string foo = std::string(5, '.') + "lolcat"; 

Take a look at the std :: string constructors .

+123
Oct 03 '08 at 12:48
source share

There is no direct idiomatic way of repeating strings in C ++ equivalent to the * operator in Python or the x operator in Perl. If you repeat a single character, a constructor with two arguments (as suggested in previous answers) works well:

 std::string(5, '.') 

This is a contrived example of how you can use ostringstream to repeat a string n times:

 #include <sstream> std::string repeat(int n) { std::ostringstream os; for(int i = 0; i < n; i++) os << "repeat"; return os.str(); } 

Depending on the implementation, this may be slightly more efficient than just concatenating a string n times.

+31
Oct 03 '08 at 16:43
source share

Use one of the forms of the string :: insert:

 std::string str("lolcat"); str.insert(0, 5, '.'); 

At the beginning of the line (position 0), "....." (five dots) will be inserted.

+18
Oct 03 '08 at
source share

I know this is an old question, but I wanted to do the same and found what I consider a simpler solution. It looks like cout has this function built into cout.fill (), see Link for a “full” explanation

http://www.java-samples.com/showtutorial.php?tutorialid=458

 cout.width(11); cout.fill('.'); cout << "lolcat" << endl; 

exits

 .....lolcat 
+10
Jul 07 '10 at 9:50
source share

As Commodore Jaeger said, I don't think any other answers actually answer this question; The question asks how to repeat a string, not a character.

Although Commodore's answer is correct, it is rather inefficient. Here's a faster implementation, the idea is to minimize copy and memory allocation operations by exponentially expanding the string first:

 #include <string> #include <cstddef> std::string repeat(std::string str, const std::size_t n) { if (n == 0) { str.clear(); str.shrink_to_fit(); return str; } else if (n == 1 || str.empty()) { return str; } const auto period = str.size(); if (period == 1) { str.append(n - 1, str.front()); return str; } str.reserve(period * n); std::size_t m {2}; for (; m < n; m *= 2) str += str; str.append(str.c_str(), (n - (m / 2)) * period); return str; } 

We can also define operator* to get something closer to the Python version:

 #include <utility> std::string operator*(std::string str, std::size_t n) { return repeat(std::move(str), n); } 

On my machine, this is about 10 times faster than the Commodore implementation, and about 2 times faster than the naive "add n - 1 time" solution.

+6
Dec 16 '15 at 20:39
source share

You must write your own stream manipulator.

cout <multi (5) <"independently" <<"Lolcat";

+4
Oct 03 '08 at 12:53
source share

ITNOA

You can use the C ++ function to do this.

  std::string repeat(const std::string& input, size_t num) { std::ostringstream os; std::fill_n(std::ostream_iterator<std::string>(os), num, input); return os.str(); } 
+1
Apr 02
source share

For the purposes of the example provided by OP, std :: string ctor is sufficient: std::string(5, '.') . However, if someone is looking for a function to repeat std :: string several times:

 std::string repeat(const std::string& input, unsigned num) { std::string ret; ret.reserve(input.size() * num); while (num--) ret += input; return ret; } 
0
Apr 10 '18 at 1:12
source share



All Articles