473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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_LargeObjectSet is a member variable of the class MyClass and has
the type "std::set<LargeObject*>". How would you write the following line?
const_const? Copy the large object?
// m_LargeObjectSet.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 5330
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_LargeObjectSet is a member variable of the class MyClass and has
the type "std::set<LargeObject*>". How would you write the following line?
const_const? Copy the large object?
// m_LargeObjectSet.find(input);
m_LargeObjectSet.find(const_cast<LargeObject*>(inp ut));
// ...
}
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.dkwrote in message
news:6n***********@mid.individual.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<>::find() 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_LargeObjectSet is a member variable of the class MyClass andhas
the type "std::set<LargeObject*>". How would you write the following line?
const_const? Copy the large object?
* * // m_LargeObjectSet.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 objektorientierter Datenverarbeitung
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 objektorientierter Datenverarbeitung
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.comwrote:
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 objektorientierter Datenverarbeitung
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
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...
1
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 <<...
10
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...
16
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...
2
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...
26
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...
7
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...
7
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...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.