aalok wrote: |
---|
Yanson's code shows me that I have to study C++ more. |
By the way, mastery of the STL doesn't indicate mastery of C++. C++ is not the STL. Knowing C++ means you know the syntax and semantix associated with creating objects/classes, using initializer lists, different types of inheritance/casting, being able to implement various design patterns, and template meta-programming.
The STL is just a library, if you find it confusing, you're welcome to try any number of other libraries. Qt has a good library with a QString class, QFile, QVector, etc. It aligns closely with the STL so familiarity with one will help with the other. Boost is another very good library, but can be tough to handle for beginners as it prioritizes functionality over simplicity.
You could also make your own libraries too!
naraku9333's code is only 3 lines long, but there is a ton of stuff there. That code demonstrates knowledge of the STL and C++. If you know C++, but not the STL, it'll take some time to read this, but it's still possible using STL references.
Here's an explanation:
line 10
vector<string> v;
This one is pretty simple, you're making a vector of strings. v is an object of class vector. vector uses a template parameter of a string type.
line 11
copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(v));
This line is much tougher. I read general->Specific. So I would start with the copy function:
http://www.cplusplus.com/reference/algorithm/copy/
It takes three parameters, The first two are iterators of the same type. The second is an iterator for a different type. Really, everything between parameter 1 and 2 are copied into parameter 3.
Parameter 1:
istream_iterator<string>(cin)
Here we are making an object without assigning it to something we can handle. It's a temporary object which is inserted as a parameter into copy. The object is not available at any other time in the main function. The object is an istream_iterator templated with a string.
http://www.cplusplus.com/reference/iterator/istream_iterator/
It is instantiated with cin, calling the "initialization" constructor described here:
http://www.cplusplus.com/reference/iterator/istream_iterator/istream_iterator/
That makes the object point to the first <string> in cin. The object uses operator++ to get the next element<string>.
Parameter 2:
istream_iterator<string>()
This is just like parameter 1, but instead of calling the initialization constructor and setting the iterator to the first element of cin, we are calling the default constructor. According to the reference this "Constructs an end-of-stream istream iterator". This satisfies the second parameter of copy().
Parameter 3:
back_inserter(v)
The back_inserter function returns an iterator to the end of the vector. As per
http://www.cplusplus.com/reference/iterator/back_inserter/. If this is set to something, then it pushes the object into the vector.
Now we have all three iterators, two source iterators and a destination. Now we can see that we've "tokenized" the input into a vector.
line12
copy(v.rbegin(), v.rend(), ostream_iterator<string>(cout, " "));
This is another interesting line. We're copying all data between
v.rbegin()
and
v.rend()
into
ostream_iterator<string>(cout, " "));
. but what does that mean?
Parameter 1:
v is a vector<string>. Vector's method
v.rbegin()
is described here:
http://www.cplusplus.com/reference/vector/vector/rbegin/
It returns a
reverse iterator which means it points to the last <string> in v. When incremented, it move backwards through the vector towards the start.
Parameter 2:
The first element of the vector v is where we want to stop copying.
v.rend()
gives us this element. So copy is going from the last element of the vector, to the first element.
Parameter 3:
ostream_iterator<string>(cout, " ")
. This is very similar to
istream_iterator<string(cin)
from line 11. See:
http://www.cplusplus.com/reference/iterator/ostream_iterator/. We're creating a temporary object again which is fed to the copy function. It is of type
ostream_iterator
. An ostream_iterator is an object which allows us to write to a stream using iterators. So when copy() writes and increments the destination iterator, it's actually writing to a stream. The constructor we are calling is the initialization constructor (like in the last line):
http://www.cplusplus.com/reference/iterator/ostream_iterator/ostream_iterator/
This initializer constructor has two parameters, the first is the stream object which will be written to. In this case it's cout. The second parameter is the delimiter. This means we will put a space between each string.
As you can see there is a lot going on in just 3 lines. Mastery of C++ will let you read this and understand this. You can only write this if you are familiar with these methods in the STL. As you continue to code, your familiarization with that specific library will increase and you'll be more comfortable writing this stuff. Just take the time to read it first and really understand why it works. Note, you don't need to use STL, you can use Qt, boost, or some other library too.