|
-
May 22nd, 2012, 01:21 PM
#11
Re: How would I alphabetize this?
 Originally Posted by smurf12125
alright but why won't this work?
Code:
sort (s.begin(), s.end(), greater<int>());
its saying that 's' isn't declared and if I do declare it then begin and end need to be declared so would I need to declare all three of them?
You are not understanding what you're programming.
Code:
string s1;
string s2;
string s3;
string s4;
string s5;
Why are you still declaring 5 string variables? Again, what if you had 1000 names? Would you declare 1,000 variables? How about 10,000 names? Would you now declare 10,000 variables and write the same code over and over again 10,000 times? What if you wanted to sort all the names in the New York City phone book listing? 10,00,000 different variables and 10,00,000 copies of the same code? See how silly it will get? If that was the only way to write such programs in C++, no one would write programs in C++.
All you need is one variable -- a variable that can hold multiple names. That variable is the StringArray that I mentioned.
Code:
StringArray s; // a container that holds 5 names
std::string inString;
for (int i = 0; i < 5; ++i )
{
getline (cin, inString);
myfile << inString << endl;
s.push_back( inString );
}
This loop does exactly what your entire code did with those 5 separate input statements. If for some reason we need 1,000 names, then all I need to do is change that loop to go to 1000 instead of 5. Do you understand what's going on now?
Then when you sort, you are sorting s, which contains all of the names.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 22nd, 2012 at 01:25 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|