What does the '1' argument do in string()?

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Argument String
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
What does the "1" do in string() in this line?

answer = string(1,'0'+i%10) + answer;

Thanks!
 
Physics news on Phys.org
sorry - this is using <string> in C++ ..
I think.. I am really confused. Teacher posted it as part of a solution to an exercise we did. I did something different so I am trying to figure out what this means.
 
Ok, now I know what your doing.

The string function is an overloaded constructor. In this case there are two parameters. The first parameter is the number of times the second parameter is repeated. Therefore,

string(1,'H') => "H"
string(2,'H') => "HH"

The string constructor also has a one parameter function like so:

string('H') => "H"