dont understand "input.size()"

I received this code as help for formatting two arrays to print out into two columns. This code worked perfectly and solved my problem, but I don't completely understand it. I don't recognize: "input.size()" on line 15. Is that a function?? Why is there a "." in it? I'm new to this so I looked through the tutorials and found something that looked similar under Library Arrays. But there wasn't much said about it. Am I on the right track? Would anyone mind schooling me on whatever this is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  // You may use optional argument c, in case you wish to prefill
// the rest of the string with eg. dots. If you'd like to create
// tables of contents for example
string setLength (string input, int n, char c = ' ' ) 
{
	// Instead of creating empty string - 
	// prefill it with c's. The string
	// will contain n times the character c
	string output(n, c);

	// Now copy the required amount of letters from
	// the input. If there are too many, they will
	// be cut off. If there are too little, the rest
	// will be spaces (or whatever c is).
	for (int i = 0; i < n && i < input.size(); i++)
	{
		output[i] = input[i];
	}
	return output;
}
Since you don't even recognize the syntax, I think you probably haven't read enough to learn about classes or methods (string is a class, and size() is a method). Try reading this part of the tutorial first, then see what kind of questions (if any) you still have, then ask again.

http://www.cplusplus.com/doc/tutorial/classes/
In the word of object-oriented programming, we "encapsulate" data and methods used to access/manipulate that data into one class or object.

In this case, input is a string which contains not only specific characters (the data), but also several methods to help us with the data. You access those methods with functions after the ..

Here is a list of all methods defined by the string class:
http://www.cplusplus.com/reference/string/string/

.size() returns the number of characters in the string. That's defined here:
http://www.cplusplus.com/reference/string/string/size/

You also have things like: .find('a'); which will find the position of the first 'a' in the string.

Even the [] operator you use in input[i] is a method which specifies which character to return. That's defined here:
http://www.cplusplus.com/reference/string/string/operator%5B%5D/

Actually, your definition of output is called a "constructor". The string class has defined what that first parameter (int n) and that second parameter (char c) are used for in the construction of output. That's defined here (as constructor #6):
http://www.cplusplus.com/reference/string/string/string/

Without the encapsulation, you'd have to manipulate everything on your own. After working with string, I can't stand the tediousness of using character arrays anymore.
Last edited on
I think that "methods" is a bit too arbitrary for someone new. They're really just member functions of a class. They are functions which are members of a class, and they either modify the class in some way, or perform an action using the information contained in that instance of the class.

In this case, std::string::size() returns the number of characters that the string contains.
Last edited on
Wow! Thanks for the help all! Looks like I've got some reading and learning to do. Thank you for your explanations and pointing me in the right direction. Lookin' forward to diggin' in!
Last edited on
Topic archived. No new replies allowed.