By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

VS 2005 should allows this, why?

P: n/a
#include <set>

using namespace std;

int main()
{

typedef set<intINTSET;

INTSET coll;

INTSET::iterator pos = coll.begin();

*pos = 9; // VS 2005 should allowes this, why?

}

the set will contain two same values, it violates the rule that a set
at most contains one of each key value.

Jul 8 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Lighter wrote:
#include <set>

using namespace std;

int main()
{

typedef set<intINTSET;
All caps is usually used form macros.
INTSET coll;

INTSET::iterator pos = coll.begin();

*pos = 9; // VS 2005 should allowes this, why?
Why should it and why VS 2005 in particular?

--
Ian Collins.
Jul 8 '06 #2

P: n/a
Lighter wrote:
#include <set>

using namespace std;

int main()
{

typedef set<intINTSET;

INTSET coll;

INTSET::iterator pos = coll.begin();

*pos = 9; // VS 2005 should allowes this, why?
No C++ compiler will allow this.

At the point of the above statement pos has the value
of coll.end(). You can't dereference it.

I think you have the concept of iterators and inserters
confused.
Jul 8 '06 #3

P: n/a
Lighter wrote:
#include <set>

using namespace std;

int main()
{

typedef set<intINTSET;

INTSET coll;

INTSET::iterator pos = coll.begin();

*pos = 9; // VS 2005 should allowes this, why?
At this point, you have undefined behavior, and ANYTHING can happen.
}
Jul 8 '06 #4

P: n/a
red floyd wrote:

At this point, you have undefined behavior, and ANYTHING can happen.
=============

I'm sorry for my careless. I made a typying mistake. The actual code
fragment is as follows:

#include <set>

using namespace std;
int main()
{

typedef set<intINTSET;

INTSET coll;

coll.insert(9); // This line was omitted before

INTSET::iterator pos = coll.begin();
*pos = 9; // VS 2005 should allowes this, why?

}

Jul 8 '06 #5

P: n/a
Lighter wrote:
red floyd wrote:

At this point, you have undefined behavior, and ANYTHING can happen.
=============

I'm sorry for my careless. I made a typying mistake. The actual code
fragment is as follows:
That's why you should always copy and paste, not retype.
#include <set>

using namespace std;
int main()
{

typedef set<intINTSET;

INTSET coll;

coll.insert(9); // This line was omitted before

INTSET::iterator pos = coll.begin();
*pos = 9; // VS 2005 should allowes this, why?
Again, I ask why VS 2005 and why should it allow it?

--
Ian Collins.
Jul 8 '06 #6

P: n/a

"Ian Collins" <ia******@hotmail.comskrev i meddelandet
news:4h**************@individual.net...
Lighter wrote:
>red floyd wrote:

At this point, you have undefined behavior, and ANYTHING can
happen.
=============

I'm sorry for my careless. I made a typying mistake. The actual
code
fragment is as follows:
That's why you should always copy and paste, not retype.
>#include <set>

using namespace std;
int main()
{

typedef set<intINTSET;

INTSET coll;

coll.insert(9); // This line was omitted before

INTSET::iterator pos = coll.begin();
*pos = 9; // VS 2005 should allowes this, why?
Again, I ask why VS 2005 and why should it allow it?
I think the OP might be referring to this defect report for C++

http://www.open-std.org/jtc1/sc22/wg...fects.html#103

which asks if you are allowed to change the value of the map elements.

The original standard is obviously a bit unclear on this, which
explains why implementations behave inconsistently.
So, my attempt to answer the question is:

It might (right now) be ok to do *pos=9, if pos is a valid iterator,
and assigning the value 9 doesn't change the order of the elements in
the set.

It seems like the next revision of the standard will contain the
sentence "Keys in an associative container are immutable.", which will
change the rule.
Bo Persson
Jul 9 '06 #7

P: n/a
Bo Persson wrote:
"Ian Collins" <ia******@hotmail.comskrev i meddelandet
news:4h**************@individual.net...
>>Lighter wrote:
>>>*pos = 9; // VS 2005 should allowes this, why?

Again, I ask why VS 2005 and why should it allow it?


I think the OP might be referring to this defect report for C++

http://www.open-std.org/jtc1/sc22/wg...fects.html#103

which asks if you are allowed to change the value of the map elements.

The original standard is obviously a bit unclear on this, which
explains why implementations behave inconsistently.
So, my attempt to answer the question is:

It might (right now) be ok to do *pos=9, if pos is a valid iterator,
and assigning the value 9 doesn't change the order of the elements in
the set.

It seems like the next revision of the standard will contain the
sentence "Keys in an associative container are immutable.", which will
change the rule.
That makes sense. Both the compilers I use enforce this.

--
Ian Collins.
Jul 9 '06 #8

P: n/a
I think, as a solution, the c++ standard should cancel the interface
iterator of set intead of the only interface const_iterator that users
can get under any circumstance.

for exmaple, the following code should not be allowed:

set<int>::iterator pos; // error, set doesn't provide a type/interface
named iterator

in contrast, the following is allowed

set<int>::const_iterator pos; // This should be the only way to declare
a iterator of set

Jul 9 '06 #9

P: n/a
In message <11**********************@h48g2000cwc.googlegroups .com>,
Lighter <cq****@gmail.comwrites
>I think, as a solution, the c++ standard should cancel the interface
iterator of set intead of the only interface const_iterator that users
can get under any circumstance.

for exmaple, the following code should not be allowed:

set<int>::iterator pos; // error, set doesn't provide a type/interface
named iterator

in contrast, the following is allowed

set<int>::const_iterator pos; // This should be the only way to declare
a iterator of set
If you did that, you'd have to remove the overloads of erase() that take
iterators. Not such a good idea.

--
Richard Herring
Jul 10 '06 #10

P: n/a
Lighter <cq****@gmail.comwrites
I think, as a solution, the c++ standard should cancel the interface
iterator of set intead of the only interface const_iterator that users
can get under any circumstance.

for exmaple, the following code should not be allowed:

set<int>::iterator pos; // error, set doesn't provide a type/interface
named iterator

in contrast, the following is allowed

set<int>::const_iterator pos; // This should be the only way to declare
a iterator of set
Interestingly I just read Item 22 in "Effective STL" by Scott Meyers
which deals with this issue. In short, iterators are needed to change
the "non-key" parts of a set element. In your case, the element is just
an int so it is not relevant.

Jul 10 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.