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,intmymap;
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 5 1190 zh**************@gmail.com wrote:
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,intmymap;
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
<zh**************@gmail.comwrote in message
news:de**********************************@u36g2000 prf.googlegroups.com...
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'.
On Jul 1, 10:43*pm, "Andrew Koenig" <a...@acm.orgwrote:
<zhangyefei.ye...@gmail.comwrote in message
news:de**********************************@u36g2000 prf.googlegroups.com...
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'.
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.
In article <94394196-cb35-4d7f-9754- ba**********@h1g2000prh.googlegroups.com>, zh**************@gmail.com
says...
On Jul 1, 10:43Â*pm, "Andrew Koenig" <a...@acm.orgwrote:
<zhangyefei.ye...@gmail.comwrote in message
news:de**********************************@u36g2000 prf.googlegroups.com....
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'.
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, intmytype;
// technically not allowed...
namespace std {
ostream &operator<<(ostream &os, mytype const &v) {
return os << v.first << " =" << v.second;
}
}
int main() {
std::map<char, intmymap;
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::ostream_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::ostream_iterator<mytype>(std::cout, "\n"));
return 0;
}
--
Later,
Jerry.
The universe is a figment of its own imagination.
<zh**************@gmail.comwrote in message
news:94**********************************@h1g2000p rh.googlegroups.com...
On Jul 1, 10:43 pm, "Andrew Koenig" <a...@acm.orgwrote:
>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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: David MacQuigg |
last post by:
I'm concerned that with all the focus on obj$func binding, &closures,
and other not-so-pretty details of Prothon, that we are missing what
is really good - the simplification of classes. There are...
|
by: Jarvis |
last post by:
I've made a testing program to test the memory usage of some Data Forms.
I create a MDI parent form with one single MDI child form, which is a Data
Form generated by .NET Data Form Wizard.
To...
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there...
|
by: glenn |
last post by:
I am use to programming in php and the way session and post vars are past
from fields on one page through to the post page automatically where I can
get to their values easily to write to a...
|
by: Cor |
last post by:
Hi,
I start a new thread about a discussion from yesterday (or for some of us
this morning). It was a not so nice discussion about dynamically removing
controls from a panel or what ever.
It...
|
by: Lalatendu Das |
last post by:
Dear friends,
I am getting a problem in the code while interacting with a nested
Do-while loop
It is skipping a scanf () function which it should not. I have written
the whole code below. Please...
|
by: andrew.jefferies |
last post by:
Hi,
I'm trying to write a simple log parsing program. I noticed that it
isn't reading my log file to the end.
My log is around 200,000 lines but it is stopping at line 26,428. I
checked that...
|
by: Frank Rizzo |
last post by:
I have an application (windows service) that in the beginning reads data
rapidly into a collection of large object trees. Each object tree in
the collection is about 100mb and typically there are...
|
by: raphfrk |
last post by:
Is this valid?
int a;
void *b;
b = (void *)a; // b points to a
b += 5*sizeof(*a); // b points to a
a = 100;
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |