Last minute question about string() argument

In summary, the string() function in this line is an overloaded constructor that takes in two parameters. The first parameter, 1, determines the number of times the second parameter, '0'+i%10, is repeated. This line of code is part of a solution to an exercise and is using the <string> library in C++.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
38
What does the "1" do in string() in this line?

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

Thanks!
 
Computer science news on Phys.org
  • #2
What library is this function coming from?
 
  • #3
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.
 
  • #4
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"
 
  • #5
ah! ok, thanks so much!
 

FAQ: Last minute question about string() argument

What is a string() argument?

A string() argument is a type of variable or data type in programming that represents a sequence of characters. It is often used to store and manipulate text-based data.

How is a string() argument used in programming?

A string() argument can be used for a variety of purposes in programming, such as storing user input, displaying messages, and manipulating and comparing text-based data.

Can a string() argument be used with other data types?

Yes, a string() argument can be used with other data types in programming. For example, it can be concatenated with a number to create a message with a numerical value.

What are some common methods for manipulating a string() argument?

Some common methods for manipulating a string() argument include concatenation, substring extraction, and searching for specific characters or patterns within the string.

Are there any limitations to using a string() argument?

Although strings are a useful and versatile data type, they do have some limitations. For example, strings can only represent a finite number of characters and may have difficulty representing non-ASCII characters or special symbols.

Similar threads

Replies
2
Views
439
Replies
5
Views
651
7
Replies
212
Views
11K
Replies
16
Views
1K
Replies
2
Views
729
Replies
10
Views
3K
Replies
1
Views
1K
Back
Top