Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Quick q. on string::const_reverse_iterator

Posted on 2001-07-27
11
Medium Priority
?
302 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.
Suggested Courses

972 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