Last minute question about string() argument

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Argument String
Click For Summary

Discussion Overview

The discussion revolves around the usage of the `string()` function in C++, specifically focusing on the meaning of its arguments in a given line of code. Participants explore the function's behavior and its application in a programming context.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant asks about the purpose of the "1" in the `string()` function call, indicating a lack of understanding of its role.
  • Another participant inquires about the library from which the `string()` function originates.
  • A participant clarifies that the function is part of the `` library in C++ and expresses confusion regarding its use in a solution provided by a teacher.
  • One participant explains that the `string` function is an overloaded constructor, detailing that the first parameter specifies how many times the second parameter is repeated, providing examples to illustrate this behavior.
  • A later reply expresses gratitude for the clarification provided.

Areas of Agreement / Disagreement

Participants do not appear to disagree on the function's behavior, but there is initial confusion regarding its usage and context. The discussion reflects a progression from uncertainty to understanding without explicit consensus on all details.

Contextual Notes

Some participants express confusion about the function's application in a specific exercise, indicating potential limitations in their understanding or the clarity of the original problem.

Math Is Hard
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!
 
Computer science news on Phys.org
What library is this function coming from?
 
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"
 
ah! ok, thanks so much!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 212 ·
8
Replies
212
Views
18K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
55
Views
7K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
8
Views
2K