about end() usage

  • Thread starter zhangyefei.yefei
  • Start date
Z

zhangyefei.yefei

can someone tell me ,is the following about end() right ? the
printed result seems ok,but i am not sure if i can use end() such
way.
thanks.

#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;

it=mymap.lower_bound ('b'); // it points to b

for ( ; it != mymap.end(); it--)
cout << (*it).first << " => " << (*it).second << endl;

return 0;
}

the output is:
b => 40
a => 20
 
K

Kai-Uwe Bux

can someone tell me ,is the following about end() right ? the
printed result seems ok,but i am not sure if i can use end() such
way.

No, you cannot.
thanks.

#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;

it=mymap.lower_bound ('b'); // it points to b

for ( ; it != mymap.end(); it--)
cout << (*it).first << " => " << (*it).second << endl;

return 0;
}

the output is:
b => 40
a => 20

It appears that you hit upon an interesting implementation detail of your
STL. Your code has undefined behavior. The function end() yields a
past-last iterator not a pre-first iterator.

If you want to traverse backwards, you could use reverse iterators or
rewrite your loop.


Best

Kai-Uwe Bux
 
A

Andrew Koenig

can someone tell me ,is the following about end() right ? the
printed result seems ok,but i am not sure if i can use end() such
way.
it=mymap.lower_bound ('b'); // it points to b

for ( ; it != mymap.end(); it--)
cout << (*it).first << " => " << (*it).second << endl;

This isn't legitimate; if it happens to do what you want, it's an accident.

The following will work, and will produce the same result if 'b' is a key:

it = mymap.upper_bound('b');
while (it != mymap.begin()) {
--it;
cout << (*it).first << " => " << (*it).second << endl;
}

If 'b' is not a key, then this version will differ from yours in that the
first output will be the last key less than 'b' rather than the first key
greater than 'b'.
 
Z

zhangyefei.yefei

This isn't legitimate; if it happens to do what you want, it's an accident..

The following will work, and will produce the same result if 'b' is a key:

    it = mymap.upper_bound('b');
    while (it != mymap.begin()) {
        --it;
        cout << (*it).first << " => " << (*it).second << endl;
    }

If 'b' is not a key, then this version will differ from yours in that the
first output will be the last key less than 'b' rather than the first key
greater than 'b'.

the question is that if "it" happens to be the first element (that
is mymap.begin() ),then we can not access the first
element,nothing is outputed.
 
J

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
says...
the question is that if "it" happens to be the first element (that
is mymap.begin() ),then we can not access the first
element,nothing is outputed.

#include <map>
#include <iostream>
#include <algorithm>

typedef std::pair<char, int> mytype;

// technically not allowed...
namespace std {
ostream &operator<<(ostream &os, mytype const &v) {
return os << v.first << " => " << v.second;
}
}

int main() {

std::map<char, int> mymap;

for (int i=0; i<10; i++)
mymap['b'+i] = i+1;

std::cout << "searched key is first item in map:\n";
std::reverse_copy(mymap.begin(), mymap.upper_bound('b'),
std::eek:stream_iterator<mytype>(std::cout, "\n"));

mymap['a'] = 0;

std::cout << "\nSearched item is not first item in map:\n";
std::reverse_copy(mymap.begin(), mymap.upper_bound('b'),
std::eek:stream_iterator<mytype>(std::cout, "\n"));
return 0;
}
 
A

Andrew Koenig

the question is that if "it" happens to be the first element (that
is mymap.begin() ),then we can not access the first
element,nothing is outputed.

Indeed. But this will never happen if 'b' is a key in the map. Please note
the change from lower_bound in the original program to upper_bound in my
version. Note that upper_bound returns an iterator that refers to the first
element *after* the given key.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top