473,804 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find() of std::set

Hello Newsgroup

I have a question about the find function of std::set.
When I have a "std::set<int*> ", why can't I call the find() function with an
"const int*"? I know that my key type is different from the type of the
parameter I give to the find function but can't the find() function be
written in a way where this would work? Normally, "int*" can be compared
with "const int*" without problems...
And as a follow-up question: What would you do in a function like this:

void MyClass::myFunc (const LargeObject* input)
{
// ...
// m_LargeObjectSe t is a member variable of the class MyClass and has
the type "std::set<Large Object*>". How would you write the following line?
const_const? Copy the large object?
// m_LargeObjectSe t.find(input);
// ...
}

The function already exists and I would have to rewrite quite a lot of code
when I would remove the "const" of the parameter type.
Do you have any suggestions?
Thanks & greetings
Chris
Nov 10 '08 #1
7 5353
Christian Meier wrote:
Hello Newsgroup

I have a question about the find function of std::set.
When I have a "std::set<int*> ", why can't I call the find() function with
an "const int*"? I know that
Because:
my key type is different from the type of the parameter I give to the find
function but can't the find() function be written in a way where
this would work?
I guess it could in theory. But it would mean that the class std::set would
have to be specialized just for this specific case.
Normally, "int*" can be compared with "const int*" without problems...
And as a follow-up question: What would you do in a function like this:

void MyClass::myFunc (const LargeObject* input)
{
// ...
// m_LargeObjectSe t is a member variable of the class MyClass and has
the type "std::set<Large Object*>". How would you write the following line?
const_const? Copy the large object?
// m_LargeObjectSe t.find(input);
m_LargeObjectSe t.find(const_ca st<LargeObject* >(input));
// ...
}
Nov 10 '08 #2

Rolf Magnus wrote:
Christian Meier wrote:
>Hello Newsgroup

I have a question about the find function of std::set.
When I have a "std::set<int*> ", why can't I call the find() function with
an "const int*"? I know that

Because:
>my key type is different from the type of the parameter I give to the
find
function but can't the find() function be written in a way where
this would work?

I guess it could in theory. But it would mean that the class std::set
would
have to be specialized just for this specific case.
I think in general, a "const T*" can be compaired with a "T*", no?

Unfortuneately, the way the language works "const T&" with T=int* is not the
same
as const int*&.

What is the reasoning behind that?
Nov 10 '08 #3
Joe Smith wrote:
Rolf Magnus wrote:
>Christian Meier wrote:
>>Hello Newsgroup

I have a question about the find function of std::set.
When I have a "std::set<int*> ", why can't I call the find()
function with an "const int*"? I know that

Because:
>>my key type is different from the type of the parameter I give to
the find
function but can't the find() function be written in a way where
this would work?

I guess it could in theory. But it would mean that the class
std::set would
have to be specialized just for this specific case.

I think in general, a "const T*" can be compaired with a "T*", no?

Unfortuneately, the way the language works "const T&" with T=int*
is not the same
as const int*&.

What is the reasoning behind that?
The reason is that "const T" should mean the same as "T const". So you
get "int* const&".

The type system works differently than #define macros - it is not a
straight text substitution.
Bo Persson
Nov 10 '08 #4

"Bo Persson" <bo*@gmb.dkwrot e in message
news:6n******** ***@mid.individ ual.net...
Joe Smith wrote:
>Rolf Magnus wrote:
>>Christian Meier wrote:

Hello Newsgroup

I have a question about the find function of std::set.
When I have a "std::set<int*> ", why can't I call the find()
function with an "const int*"? I know that

Because:

my key type is different from the type of the parameter I give to
the find
function but can't the find() function be written in a way where
this would work?

I guess it could in theory. But it would mean that the class
std::set would
have to be specialized just for this specific case.

I think in general, a "const T*" can be compaired with a "T*", no?

Unfortuneately , the way the language works "const T&" with T=int*
is not the same
as const int*&.

What is the reasoning behind that?

The reason is that "const T" should mean the same as "T const". So you get
"int* const&".
Right. Doh!

Obviously a "reference to a constant pointer to an integer" (quite the mouth
full) is the right way for the compiler to interpret that.

Nov 11 '08 #5
On Nov 10, 3:26*pm, "Christian Meier" <chris@no_spam. comwrote:
I have a question about the find function of std::set.
When I have a "std::set<int*> ", why can't I call the find()
function with an "const int*"? I know that my key type is
different from the type of the parameter I give to the find
function but can't the find() function be written in a way
where this would work? Normally, "int*" can be compared with
"const int*" without problems...
The problem is that std::set<>::fin d() takes the key by
reference, and a reference to an int* cannot be initialized with
an int const*&. (If you could, then you could modify a const
object without a const cast.)
And as a follow-up question: What would you do in a function
like this:
void MyClass::myFunc (const LargeObject* input)
{
* * // ...
* * // m_LargeObjectSe t is a member variable of the class MyClass andhas
the type "std::set<Large Object*>". How would you write the following line?
const_const? Copy the large object?
* * // m_LargeObjectSe t.find(input);
* * // ...
}
The function already exists and I would have to rewrite quite
a lot of code when I would remove the "const" of the parameter
type. Do you have any suggestions?
The real question is what your std::set should really contain.
Perhaps it would be more appropriate for it to contain MyType
const*, rather than just MyType*. Not knowing what the role of
the set is, I can't say. Most of the time, if the set contains
pointers, you want to specialize the comparison to be based on
the pointed to object. In which case, the pointer had better be
to const.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 11 '08 #6
On Nov 10, 4:08*pm, Rolf Magnus <ramag...@t-online.dewrote:
Christian Meier wrote:
I have a question about the find function of std::set. When
I have a "std::set<int*> ", why can't I call the find()
function with an "const int*"? I know that *
Because:
my key type is different from the type of the parameter I
give to the find function but can't the find() function be
written in a way where this would work?
I guess it could in theory. But it would mean that the class
std::set would have to be specialized just for this specific
case.
Not at all. All it would require is that set<>::find() take
its argument by value, and not by reference. Which would have
been the better design to begin with.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 11 '08 #7
On Nov 10, 7:48*pm, "Joe Smith" <unknown_kev_.. .@hotmail.comwr ote:
Rolf Magnus wrote:
Christian Meier wrote:
Unfortuneately, the way the language works "const T&" with
T=int* is not the same as const int*&.
What is the reasoning behind that?
Because it would break const:

void
f( int const* p )
{
int*& r = p ;
*r = 42 ;
}

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 11 '08 #8

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

Similar topics

8
11122
by: gipsy boy | last post by:
I've got a std::set with a custom comparator. When I add things, it puts them in the right place. When I change these objects (and I can tell they are effectively changed), the set doesn't 'auto-sort' with these changes. How can I do this? I store pointers in the set, but the comparator compares the contents of these pointers. -- - gipsy boy
1
2506
by: jimmy | last post by:
Hi, I am getting a Fatal signal with std::set::insert(). I'm really not sure what to try next: Here is my code: std::cout << "max size: " << _indices.max_size() << std::endl; std::cout << "size: " << _indices.size() << std::endl;
10
7562
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...
16
5091
by: Cory Nelson | last post by:
Does anyone know how std::set prevents duplicates using only std::less? I've tried looking through a couple of the STL implementations and their code is pretty unreadable (to allow for different compilers, I guess).
2
8157
by: shuisheng | last post by:
Dear All, std::set is sorted. So I am wondering is there any fast way to access (sucn as random access) to its elements just like std::vector. Assume I have a set std::set<inta; So I can easily get access to such as a.
26
7219
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm and was thus wondering whether I'd be better off tracking the size "manually" or whether that'd be pointless. Thanks,
7
2596
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?
7
5774
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me help, thank you. I am a freshman about the STL. The following is the code. #include <set> #include <iostream> #include <vector> int main() {
2
2799
by: Markus Dehmann | last post by:
I want to derive from std::set, like shown below. But when I try to declare an iterator over the contained elements I get an error, see the twp uncommented lines: #include <set> template<class T> class MySet : public std::set<T>{ public: MySet() : std::set<T>() {} void foo(){
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
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...
1
10341
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
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3
3001
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.