Solved

Quick q. on string::const_reverse_iterator

Posted on 2001-07-27
11
292 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Load and store *.pnm image file 1 86
ASP.net build a IF/Then Walkthrough Guide 1 211
FMX StringGrid1->Canvas->FillRect Problem 3 153
convert char array to number in c 5 85
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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