-3
void staircase(int n)
{
for(int i = 1 ; i <= n ; i++)
{
    string h(i,'#');
    string s(n-i,' ');   
    cout<<s<<h<<endl;
}}

Please explain the first two lines in the for loop. Can we declare like string h(i,'#'); in strings?

Does it means filling the ith position in the string with "#"?

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

Can we declare like string h(i,'#'); in strings?

Yes. Actually, that's defining the string, not declaring.

Does it means filling the ith position in the string with "#"?

No.

Please use the reference pages. They are and will be your friend in the C++ journey.

For your particular use case, refer to the constructor of std::string especially this one

Constructor

which has this description

  1. Constructs the string with count copies of character ch.

These are the std::string constructors used in your code. So std::string h(i,'#') constructs a string with i copies of #. Same logic for std::string s(n-i,' ').

Zoso
  • 3,273
  • 1
  • 16
  • 27