Sign in to follow this  

Trying a different method

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, I tried it again, but instead I did it like this: #include <iostream> #include <cstring> #include <algorithm> using namespace std; int main (int agrc, char *argv[]) { string text(argv[1]); reverse(text.begin(),text.end()); cout << text << endl; return 0; } Still won't compile. Anyone? -IV

Share this post


Link to post
Share on other sites
std::string has a constructor that takes a char*
std::string has begin() and end() methods returning iterators.
std::reverse reverses in place an iterator range.

Share this post


Link to post
Share on other sites
To understand this you must first understand iterators and sequences. Fortunately, they are simple at a high level.

A sequence is an object that contains other items*, with a specific ordering of its contents. std::string is a sequence which contains text characters. std::vector<foo> and std::list<foo> are sequences which contain foos.

An iterator is an object which
(a) remembers a location in a sequence
(b) supports an operation: "Go to the next position in the sequence" (or "previous"; that's just "next" in the opposite direction.)
(c) supports an operation: "Tell me what item is at your current location".

In C++, iterators also have a meaningful comparison for equality: two iterators are considered equal if they are (a) iterating over the same sequence, and (b) are at the same location.

Idiomatically, and especially in C++, two iterators can be used to represent a "range" within a sequence. If you have iterators x and y within sequence z, and you loop like

for (; x != y; ++x) {
do_something_with(*x);
}

then you will do something with all the elements between x and y (not including y). IIRC, iterators can be compared but are not necessarily *ordered*, so you write != rather than <.

* Here I use "item" to mean either an object or a primitive type. Some languages do not make a distinction, but C++ very definitely does.

Now we can analyze in detail:

std::string provides methods ".begin()" and ".end()", which return iterators that remember the beginning and ending of the string, respectively. Actually, .end() represents a position one past the end of the string, IIRC, which makes basic for loops work. But basic for loops are no fun ( ;) ), the real power is tucked away in the 'algorithm' header.

std::reverse() is a free function defined via the 'algorithm' header. It accepts two iterators, and reverses the sequence contents within that range. Of course, that requires that the sequence is not const, so that it can be modified. The STL enforces that by having const sequences return a different type of iterator, IIRC (so you get a compile-time error because the types of the stuff you give to std::reverse() don't match what it's expecting).

How is it written? I don't know exactly, but I can think of a couple of possibilities. You probably can too. Remember, you start out with handles for the beginning and end of the items you want to rearrange...

Share this post


Link to post
Share on other sites

This topic is 4824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this