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 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));
// ...
}
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?
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
"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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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;
|
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...
|
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).
|
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.
| |
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,
|
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?
|
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()
{
|
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(){
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |