Solved

Quick q. on string::const_reverse_iterator

Posted on 2001-07-27
11
293 Views
Last Modified: 2012-05-04
Hi,
why the following code does not work for me?

std::string test("test");
  std::string::const_reverse_iterator backward = test.rbegin();


0
Comment
Question by:proskig
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6326799
Because if 'test' isn't a 'const string', 'reverse_iterator rbegin();' is used instead of 'const_reverse_iterator rbegin() const;', thus leading to "error C2440: 'initializing' : cannot convert from 'class std::rever
se_iterator<char *,char,char &,char *,int>' to 'class std::reverse_iterator<char
 const *,char,char const &,char const *,int>'"

Make it either

const std::string test("test");

or

std::string::reverse_iterator backward = test.rbegin();

and it'll work...

0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 6326837
But you can still generate a constant interator.  Use

std::string test("test");

std::string::const_reverse_iterator backward (&(*test.rbegin()));
0
 
LVL 22

Expert Comment

by:nietod
ID: 6326851
Note that oen way in which pointers differs from interators is that there is not always a convertion from iterator to const_iterator  (and same for reverse.).

For this reason and because virtually all of the STL uses interators, not const_iterators, it is often recommended that you do not use const_iterator (or reverse) except for very specific cases.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:proskig
ID: 6326858
OK, I knew about jkr's solution, so I would prefer nietod's.

But can anyone explain why it happens or why the following works:

std::string::const_iterator forward = test.begin();
0
 
LVL 5

Author Comment

by:proskig
ID: 6326878
Sorry, because string::iterator is typedef for char*, so that's why it works.

Sorry, again.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6326897
>>OK, I knew about jkr's solution

Then, why do you ask

>>why the following code does not work for me?

Because that's what I answered...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6327059
Note that the conversion I suggested is not necessarily safe when the string has 0 length.  i..e it doesn't necessarily work on an empty container.

As I think you understand now, a string classes' iterator is just typedefed as a char * and a constant iterator is typedefed as a "const char *"   So there is a conversion between iterator and const_iterator for this class, but the existance of this conversion is not guaranteed.  i.e. its not true for all containers and its not true for all implimentations.  (Someome could  write a string class where this wasn't true.)
0
 
LVL 5

Author Comment

by:proskig
ID: 6327187
jkr:
because I had function, which accepted non-const string and I did not want to create another temporary object.

So my real question was coming from the point: why for begin() C++ compiler is able to construct const_iterator from iterator and it cannot do that for rbegin.

However, I posted points for you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6327379
Thanx, proskig!
0
 
LVL 30

Expert Comment

by:Axter
ID: 6327838
proskig,
Are you trying to reverse a string.
If so, you can do so using the following method:
#include <algorithm>

std::string test("A test");
std::reverse(test.begin(),test.end());
0
 
LVL 5

Author Comment

by:proskig
ID: 6333487
Axter: Almost, but mainly I was playing around with some samples
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question