Last minute question about string() argument

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Argument String
AI Thread Summary
The "1" in the string() function call indicates the number of times the second parameter, which is a character, is repeated. In the example provided, string(1, '0' + i % 10) creates a string consisting of a single character derived from the expression '0' + i % 10. The string constructor in C++ can take two parameters: the first is the count of repetitions, and the second is the character to be repeated. For instance, string(1, 'H') results in "H", while string(2, 'H') results in "HH". Additionally, the string constructor can also be called with a single character parameter, such as string('H'), which similarly returns "H". The function is part of the <string> library in C++.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
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!
 
In my discussions elsewhere, I've noticed a lot of disagreement regarding AI. A question that comes up is, "Is AI hype?" Unfortunately, when this question is asked, the one asking, as far as I can tell, may mean one of three things which can lead to lots of confusion. I'll list them out now for clarity. 1. Can AI do everything a human can do and how close are we to that? 2. Are corporations and governments using the promise of AI to gain more power for themselves? 3. Are AI and transhumans...
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...

Similar threads

Back
Top