8

I'm currently learning STL and I got some uncertainities about find and const iterators. Let's say I have a find function:

some_stl_container::const_iterator found = myContainer.find(value);

After that should I check what I got for found against another const_iterator, or is it valid to make a check against simply an iterator. Basically would there be any difference between doing this:

if(found!=myContainer.cend())

and this:

if(found!=myContainer.end())

The first looks more accurate(at least to me), but the second should work fine too, right?

lightxbulb
  • 1,251
  • 12
  • 29
  • possible duplicate of [const to non-const iterator comparisons, are they valid](http://stackoverflow.com/questions/16900498/const-to-non-const-iterator-comparisons-are-they-valid) – juanchopanza Jun 04 '13 at 20:58
  • Considering that `cend` didn't exist until C++11, there had to be *some* way to do it. – Mark Ransom Jun 04 '13 at 22:38

2 Answers2

10

All standard library containers satisfy the requirement that Container::iterator is convertible to Container::const_iterator. So both comparisons are valid and will yield the same result.

From §23.2.1 - Table 96

X::iterator ... any iterator category that meets the forward iterator requirements. convertible to X::const_iterator.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • Funny thing is that I just tried to find confirmation that `x.cend() == X::const_iterator(x.end())` in a standard. And didn't find one (may be I'm missing some obvious logical conclusion). By definition `x.cend() == const_cast(x).end()` but I don't see how this proves the first assertion. – Serge Dundich Jun 05 '13 at 05:42
  • @SergeDundich I'm not sure I understand your question. What I cited clearly states that, when it comes to containers, `iterator` is convertible to `const_iterator`. If you're asking what allows us to use `operator==` to compare 2 iterators, refer to [this answer](http://stackoverflow.com/a/16901637/241631) of mine. – Praetorian Jun 05 '13 at 13:35
1

Checking if your iterator is different from myContainer.end() is fine. cendand cbegin methods are only here to explicitely obtain const iterators, so that makes no difference in your case.

Note that you could do auto found = myContainer.find(value) in c++11 to infer the iterator type, and that some people will argue that Standard library is the correct name (not STL).

  • I mean http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library which is in fact different from the original STL (see http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about) but that's a bearded-purist argument :p – teh internets is made of catz Jun 04 '13 at 21:18
  • @lightxbulb: As the article you link to describes, the STL is an ancient library which inspired parts of the modern standard library. Your question is specifically about the C++11 Standard Library, not the STL (since STL containers didn't have `cend` functions). – Mike Seymour Jun 04 '13 at 22:53
  • I don't really think there's much use arguing semantics, but if your read the article you wouldn't say that the STL an ancient library that has inspired part of the modern std. STL is part of std. You can check the wiki page I linked and look left, right at the beginning. You can also check these: http://en.wikipedia.org/wiki/Standard_Template_Library#External_links (C/C++ STL reference, includes C++11 features), http://cpprocks.com/wp-content/uploads/c++11-stl-additions.pdf , http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx (search STL on the page). – lightxbulb Jun 05 '13 at 09:04
  • As I already said I find no meaning in arguing semantics, but I do not think that it is right to go so strong about iterators not being part of the STL and only part of std. They are part of the STL too. Here's a link that should help you: cpprocks.com/wp-content/uploads/c++11-stl-additions.pdf – lightxbulb Jun 05 '13 at 09:10
  • Here's part of the summary of this book too: http://www.amazon.com/Standard-Library-Tutorial-Reference-ebook/dp/B0085MNPQ6/ref=sr_1_1?ie=UTF8&qid=1370423491&sr=8-1&keywords=STL : "The book focuses in particular on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms." – lightxbulb Jun 05 '13 at 09:13
  • My remark was just an advice to avoid the exact type of argument you are doing ... I won't bother to mention it the next time. – teh internets is made of catz Jun 07 '13 at 13:16
  • Discussions help with clearing things up, I am sorry if you perceived anything I mentioned as offensive - I just want to know whether something is correct or not. I provided all these arguments in the hope that somebody would maybe give a counter-argument why it shouldn't be called STL. – lightxbulb Jun 08 '13 at 17:29
  • I mean plenty of books and articles use STL as the standard template library in std(even the book I was learning from), so should I stick with it or just stop addressing this part of the std as STL? The best argument that I saw on the other side is that people call sometimes the whole std STL, I don't do this, but maybe I shouldn't use STL at all if it causes misunderstanding. However why do so many people us it? – lightxbulb Jun 08 '13 at 17:40
  • Well, I think everyone understands what we mean by "STL" so that's not really a problem. But it seems that it is a recurrent topic on SO. – teh internets is made of catz Jun 08 '13 at 19:53