473,883 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

which type should "std::set::begi n() const" return?

On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as
iterator set::begin() const.
However, why should a const object returns a non-const iterator?
Then, I found, in this book, the semantic of set::iterator is defined as same as set::const_iter ator. Both of them must be const!
I tried to read the source of GNU STL(version 3.4.1).They were using a red-black tree to implant it (std::set has a _RBtree.) .Both set::iterator and set::const_iter ator are simply defined as _RBtree::const_ iterator.
But, in VC++, it is deferent.
There is a base class named xtree which also a red-black tree. Then the class map and set inherit from xtree (which implantation is better? is-a? has-a?).They don't defined iterator and const_iterator theirself,just inherit them from the base class xtree,and the member function begin() is defined as :
const_iterator set::begin() const;
iterator set::begin().
So ,we can initialize a mutable set, get the begin of it, then try to modify it!
The following code has been compiled successful under VC++2003
int a[4]={1,2,3,4};
std::set<int> s(a,a+4);
std::set<int>:: iterator i=s.begin();
(*i) = 9;
std::cout<<"Now s = ";
std::copy(s.beg in(),s.end(),st d::ostream_iter ator<int>(std:: cout,","));
std::cout<<std: :endl;
std::set<int>:: const_iterator p=s.find(9);
if(p != s.end() )
std::cout<<(*p) ;
else std::cout<<"Can not find the element special"<<std:: endl;

A set must be a sorted container, but now it is not! So anything terrible things it would happen.
Is this a bug of VC++?

A bug of VC++.net STL
--
---------------snnn-------------
---http://snnn.blogone.ne t-----
Jul 22 '05 #1
11 3262
On Thu, 06 Jan 2005 01:18:22 +0800, snnn
<sn*****@gmail. com.haha.remove me> wrote:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as
iterator set::begin() const.
However, why should a const object returns a non-const iterator?
Then, I found, in this book, the semantic of set::iterator is defined as same as set::const_iter ator. Both of them must be const!
I tried to read the source of GNU STL(version 3.4.1).They were using a red-black tree to implant it (std::set has a _RBtree.) .Both set::iterator and set::const_iter ator are simply defined as _RBtree::const_ iterator.
But, in VC++, it is deferent.
There is a base class named xtree which also a red-black tree. Then the class map and set inherit from xtree (which implantation is better? is-a? has-a?).They don't defined iterator and const_iterator theirself,just inherit them from the base class xtree,and the member function begin() is defined as :
const_iterat or set::begin() const;
iterator set::begin().
So ,we can initialize a mutable set, get the begin of it, then try to modify it!
The following code has been compiled successful under VC++2003
int a[4]={1,2,3,4};
std::set<int> s(a,a+4);
Shouldn't this be:
std::set<int> s(a,a+3);
??

Note that s is non-const...
std::set<int>:: iterator i=s.begin();
(*i) = 9;
std::cout<<"Now s = ";
std::copy(s.beg in(),s.end(),st d::ostream_iter ator<int>(std:: cout,","));
std::cout<<std: :endl;
std::set<int>:: const_iterator p=s.find(9);
if(p != s.end() )
std::cout<<(*p) ;
else std::cout<<"Can not find the element special"<<std:: endl;

A set must be a sorted container, but now it is not! So anything terrible things it would happen.
Is this a bug of VC++? [or] A bug of VC++.net STL


You are mistaken... this is the declaration of begin() (actually two
declarations) for std::set as delivered with the Microsoft VCToolkit:

<quote>
iterator begin()
{ // return iterator for beginning of mutable
sequence
return (_TREE_ITERATOR (_Lmost()));
}

const_iterator begin() const
{ // return iterator for beginning of nonmutable
sequence
return (_TREE_CONST_IT ERATOR(_Lmost() ));
}
</quote>

As you can see, the "begin()" which returns a const_iterator is also
const, therefore you CANNOT modify the set if the set itself is const.

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #2
Bob Hairgrove wrote:
On Thu, 06 Jan 2005 01:18:22 +0800, snnn
<sn*****@gmail. com.haha.remove me> wrote:
int a[4]={1,2,3,4};
std::set<int> s(a,a+4);

Shouldn't this be:
std::set<int> s(a,a+3);
??


That would only consume 3 elements of 's'.
Note that s is non-const...
What difference would that make? Values (r-values) used to
initialise the elements of the set<> cannot transfer their
const-ness, can they?
[...]

Jul 22 '05 #3
On Wed, 05 Jan 2005 15:36:30 -0500, Victor Bazarov
<v.********@com Acast.net> wrote:
Note that s is non-const...


What difference would that make? Values (r-values) used to
initialise the elements of the set<> cannot transfer their
const-ness, can they?


The subject line says it all:
"which type should "std::set::begi n() const" return?"

Answer:
std::set::const _iterator (which it does).

The OP seems to think it doesn't.

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #4
On Wed, 05 Jan 2005 15:36:30 -0500, Victor Bazarov
<v.********@com Acast.net> wrote:
Bob Hairgrove wrote:
On Thu, 06 Jan 2005 01:18:22 +0800, snnn
<sn*****@gmail. com.haha.remove me> wrote:
int a[4]={1,2,3,4};
std::set<int> s(a,a+4);

Shouldn't this be:
std::set<int> s(a,a+3);
??


That would only consume 3 elements of 's'.


Of course, you are right about this. I really need to RTFM some
more...

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #5
Bob Hairgrove wrote:
On Wed, 05 Jan 2005 15:36:30 -0500, Victor Bazarov
<v.********@com Acast.net> wrote:

Note that s is non-const...


What difference would that make? Values (r-values) used to
initialise the elements of the set<> cannot transfer their
const-ness, can they?

The subject line says it all:
"which type should "std::set::begi n() const" return?"

Answer:
std::set::const _iterator (which it does).

The OP seems to think it doesn't.


Well, yes. I just was curious on your 'notice that s is non-const'
comment placed right after another comment about the initialisation
of 's'. It shouldn't matter whether 's' is const or not when 's'
is initialised, no? I mean, it will be initialised just as well if
it were const...
Jul 22 '05 #6
The second param is a past-the-end iterator

Jul 22 '05 #7
Whether s is const or non-const,a set should never return a non-const
iterator.We can never modify a set by a iterator pointing to it's
element.Because a set must be a sorted container.You can insert and
remove element into/off it,but never manual modify it.

Jul 22 '05 #8
"snnn" <sn*****@gmail. com> wrote...
Whether s is const or non-const,a set should never return a non-const
iterator.We can never modify a set by a iterator pointing to it's
element.Because a set must be a sorted container.You can insert and
remove element into/off it,but never manual modify it.


I think you need to submit this as a proposal to comp.std.c++.
Jul 22 '05 #9

snnn wrote:
Whether s is const or non-const,a set should never return a non-const
iterator.We can never modify a set by a iterator pointing to it's
element.Because a set must be a sorted container.You can insert and
remove element into/off it,but never manual modify it.


Untrue.

class X {
int a;
int b;
public:
X(int a, int b) : a(a),b(b) {}
bool operator<( X const& rhs ) { return a<rhs.a; }
};
std::set<X> sox = foo();
sox.begin()->b=0; // safe

Regards,
Michiel Salters

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5766
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. The following class with be used as an example: class MyClass { public: // member function would go here. private: int data_;
4
5584
by: CupOfWater | last post by:
Hi, I'm getting some errors using remove_if that I can not fix at all. Let me paste my code and also the error here. I went on IRC and people were mostly no help. The compiler is g++ 3.2.3. Thanks in advance. /usr/include/c++/3.2.3/bits/stl_algo.h: In function `_OutputIter std::remove_copy_if(_InputIter, _InputIter, _OutputIter, _Predicate) ': /usr/include/c++/3.2.3/bits/stl_algo.h:1085: instantiated from `_ForwardIter...
26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
3
3600
by: Markus Dehmann | last post by:
I have a two different value types with which I want to do similar things: store them in the same vector, stack, etc. Also, I want an << operator for each of them. class Value{}; // this would be "public interface Value{}" in Java! class IntValue : public Value{ private: int _value; public:
1
5077
by: Joe Gottman | last post by:
What are the advantages and disadvantages of the tree-based std::set versus the hash-based tr1::unordered_set? set advantages: 1) iterators remain valid after insert and erase (except for iterators to the erased element). 2) Sets can be compared using operator== or operator<. The STL set functions, (set_union, set_intersection, etc), easily work on sets. 3) Can do searches based on ordering (for instance, given a set of strings, it is...
4
2758
by: chrisstankevitz | last post by:
This code does not compile on gcc 3.4.4. Should it? Thanks for your help, Chris //================ #include <set> int main()
10
7572
by: danibe | last post by:
I never had any problems storing pointers in STL containers such std::vector and std::map. The benefit of storing pointers instead of the objects themselves is mainly saving memory resources and performance (STL containers hold *copies* of what's passed to them via the insert() method). However, I am not sure how to accomplish that using std::set. For various reasons, I cannot use vector or map in my application. But I would like to...
5
8203
by: Austin | last post by:
Here is my program: class Test { private: int _num; }; int main() { set<TestaSet;
7
2599
by: desktop | last post by:
In the C++ standard page 472 it says that you can construct a std::set in linear time if the constructor gets a sorted sequence of elements. But how is this possible when insert takes logarithmic time? Should the time not be nlgn where n is the number of elements?
0
11114
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10835
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5787
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.