Solved

Quick q. on string::const_reverse_iterator

Posted on 2001-07-27
11
290 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now