?
Solved

Quick q. on string::const_reverse_iterator

Posted on 2001-07-27
11
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

719 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