I have implemented a red-black tree which is used for std::set.
In the C++ standard 3 different insert methods are specified for the
associative container. But as i see it insert adds an object to the set
based on a key. But where does the key come from?
If I make my own object I don't specify any unique key that insert uses
to place the object.
Does the insert call generate some unique key that it uses each time an
object is inserted? 16 3078
desktop wrote:
I have implemented a red-black tree which is used for std::set.
In the C++ standard 3 different insert methods are specified for the
associative container. But as i see it insert adds an object to the
set based on a key. But where does the key come from?
It's the same as the value. Doesn't the book you're reading about
'std::set' tell you that?
If I make my own object I don't specify any unique key that insert
uses to place the object.
You do. By supplying an object with a value.
Does the insert call generate some unique key that it uses each time
an object is inserted?
Nope.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov wrote:
desktop wrote:
>I have implemented a red-black tree which is used for std::set.
In the C++ standard 3 different insert methods are specified for the associative container. But as i see it insert adds an object to the set based on a key. But where does the key come from?
It's the same as the value. Doesn't the book you're reading about
'std::set' tell you that?
>If I make my own object I don't specify any unique key that insert uses to place the object.
You do. By supplying an object with a value.
>Does the insert call generate some unique key that it uses each time an object is inserted?
Nope.
V
It seems that I don't understand set. Inserting into a vector works fine:
class test {
public:
int getpp(){return pp;}
void setpp(int i){pp = i;}
private:
int pp;
};
int main() {
std::vector<tes thh;
test t1;
hh.push_back(t1 ); // Works fine
std::set<testmy _set;
const test& tref = t1; // see *
my_set.insert(t ref); // fails with error: no match for
‘operator<’ in ‘__x < __y’
}
Can I only insert into std::set if my class 'test' define '<' and
properly some of the other operators?
I still don't see how insert gets the key from 'test' so it can put it
the right place in the tree.
* According to http://www.cppreference.com/cppset/insert.html
desktop wrote:
Victor Bazarov wrote:
>desktop wrote:
>>I have implemented a red-black tree which is used for std::set.
In the C++ standard 3 different insert methods are specified for the associative container. But as i see it insert adds an object to the set based on a key. But where does the key come from?
It's the same as the value. Doesn't the book you're reading about 'std::set' tell you that?
>>If I make my own object I don't specify any unique key that insert uses to place the object.
You do. By supplying an object with a value.
>>Does the insert call generate some unique key that it uses each time an object is inserted?
Nope.
V
It seems that I don't understand set.
Hmm... Yes. Do you understand std::map? std::set is very similar to
std::map. Essentially 'std::set<T>' is just like 'std::map<const T,T>'.
Inserting into a vector works
fine:
class test {
public:
int getpp(){return pp;}
void setpp(int i){pp = i;}
private:
int pp;
};
int main() {
std::vector<tes thh;
test t1;
hh.push_back(t1 ); // Works fine
std::set<testmy _set;
const test& tref = t1; // see *
my_set.insert(t ref); // fails with error: no match for
‘operator<’ in ‘__x < __y’
}
Can I only insert into std::set if my class 'test' define '<' and
properly some of the other operators?
"properly"? Yes, to use the default sorting mechanism your class
needs to have operator< defined for it. You can make it a member or
you can make it a stand-alone function.
You don't have to have operator< defined if you use custom sorting
functor in your set.
I still don't see how insert gets the key from 'test' so it can put it
the right place in the tree.
What book are you reading that doesn't explain how sorting of
objects works?
* According to http://www.cppreference.com/cppset/insert.html
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Jun 8, 6:41 am, desktop <f...@sss.comwr ote:
Can I only insert into std::set if my class 'test' define '<' and
properly some of the other operators?
Yes. If you read a reference (or the C++ standard)
you will see exactly what operators must be defined.
desktop wrote:
Victor Bazarov wrote:
>desktop wrote:
>>I have implemented a red-black tree which is used for std::set.
In the C++ standard 3 different insert methods are specified for the associative container. But as i see it insert adds an object to the set based on a key. But where does the key come from?
It's the same as the value. Doesn't the book you're reading about 'std::set' tell you that?
>>If I make my own object I don't specify any unique key that insert uses to place the object.
You do. By supplying an object with a value.
>>Does the insert call generate some unique key that it uses each time an object is inserted?
Nope.
V
It seems that I don't understand set. Inserting into a vector works fine:
class test {
public:
int getpp(){return pp;}
void setpp(int i){pp = i;}
private:
int pp;
};
int main() {
std::vector<tes thh;
test t1;
hh.push_back(t1 ); // Works fine
std::set<testmy _set;
const test& tref = t1; // see *
my_set.insert(t ref); // fails with error: no match for
‘operator<’ in ‘__x < __y’
}
Can I only insert into std::set if my class 'test' define '<' and
properly some of the other operators?
I still don't see how insert gets the key from 'test' so it can put it
the right place in the tree.
* According to http://www.cppreference.com/cppset/insert.html
If you've created a red-black tree implementation, then your
implementation must have some way of deciding what order the objects go
in the tree. This is the same concept required for set. You have to
have some way of deciding what order objects go in. The way std::set
(and all the containers that need an ordering) decide this is by an
expression that can determine whether one object is "less" than another.
What it means for an object to be "less" depends on the type.
For types that implement operator<, std::set can just use that. If you
don't want to (or can't) add operator< to a class, you can create a
separate comparison functor and tell std::set to use that. Example:
struct compare_test
{
// If getpp was const we could pass by const reference.
// But it isn't, so we can't.
bool operator()(test test1, test test2)
{
return test1.getpp() < test2.getpp();
}
};
std::set<test, compare_testmy_ set;
--
Alan Johnson
Old Wolf wrote:
On Jun 8, 6:41 am, desktop <f...@sss.comwr ote:
>Can I only insert into std::set if my class 'test' define '<' and properly some of the other operators?
Yes. If you read a reference (or the C++ standard)
you will see exactly what operators must be defined.
I can see operators that must be defined for std::set in 23.3.3, but I
can't find any requirements for the objects that I would like to insert.
This is my object that I would like to insert into a std::set:
class test {
public:
int getpp(){return pp;}
void setpp(int i){pp = i;}
int operator<(int a) const
{
return 22;
}
private:
int pp;
};
But I still get the error:
error: no match for ‘operator<’ in ‘__x < __y
where can I find an interface for the objects to insert?
Johs wrote:
[..]
I can see operators that must be defined for std::set in 23.3.3, but I
can't find any requirements for the objects that I would like to
insert. This is my object that I would like to insert into a std::set:
class test {
public:
int getpp(){return pp;}
void setpp(int i){pp = i;}
int operator<(int a) const
{
return 22;
}
private:
int pp;
};
But I still get the error:
error: no match for ‘operator<’ in ‘__x < __y
where can I find an interface for the objects to insert?
You are supposed to implement a comparison between two objects of the
type you're going to store, not between an object and an int:
...
bool operator <(test const& t) const {
...
}
and the actual implementation is supposed to adhere to "strict weak
ordering" rules: if two objects ('a', 'b') are equivalent (for the
purporses of storing in that set), then 'a < b' and 'b < a' should
both return false.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov wrote:
Johs wrote:
>[..] I can see operators that must be defined for std::set in 23.3.3, but I can't find any requirements for the objects that I would like to insert. This is my object that I would like to insert into a std::set:
class test { public: int getpp(){return pp;} void setpp(int i){pp = i;}
int operator<(int a) const { return 22; }
private: int pp; };
But I still get the error:
error: no match for ‘operator<’ in ‘__x < __y
where can I find an interface for the objects to insert?
You are supposed to implement a comparison between two objects of the
type you're going to store, not between an object and an int:
...
bool operator <(test const& t) const {
...
}
and the actual implementation is supposed to adhere to "strict weak
ordering" rules: if two objects ('a', 'b') are equivalent (for the
purporses of storing in that set), then 'a < b' and 'b < a' should
both return false.
V
Where are such rules defined. Could not find them in the C++ Standard.
desktop wrote:
Victor Bazarov wrote:
>Johs wrote:
>>[..] I can see operators that must be defined for std::set in 23.3.3, but I can't find any requirements for the objects that I would like to insert. This is my object that I would like to insert into a std::set: class test { public: int getpp(){return pp;} void setpp(int i){pp = i;}
int operator<(int a) const { return 22; }
private: int pp; };
But I still get the error:
error: no match for ‘operator<’ in ‘__x < __y
where can I find an interface for the objects to insert?
You are supposed to implement a comparison between two objects of the type you're going to store, not between an object and an int:
... bool operator <(test const& t) const { ... }
and the actual implementation is supposed to adhere to "strict weak ordering" rules: if two objects ('a', 'b') are equivalent (for the purporses of storing in that set), then 'a < b' and 'b < a' should both return false.
V
Where are such rules defined. Could not find them in the C++ Standard.
The 'set' and 'map' templates have a template argument: the "Compare"
relation (see 23.1.2), which is by default 'std::less'. Now, look up
'std::less' and keep reading until you don't understand (and cannot
figure it out) by reading it again. Then ask more specific questions.
Again: what book are you reading that doesn't explain those things?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Svenn-Ivar Svendsen |
last post by:
I'm implementing a windows application with generic support for
scripting, and I'm using the microsoft script control (msscript).
For vbscript/jscript I'm happy, but with python I got problems
accessing COM objects that have been inserted in the script runtime by
the msscript->AddObject function. Have anybody had similar problems?
To be...
|
by: F. Da Costa |
last post by:
Hi,
Does anybody know why IE5+ does *not* honour array objects (like a table)
across a session?
Example:
Frame A contains a var tableVar which is set via form Frame B (on init)
using top.A.tableVar = document.getElementById("someTable");
As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
|
by: Aguilar, James |
last post by:
Hey guys. A new question:
I want to use an STL libarary to hold a bunch of objects I create.
Actually, it will hold references to the objects, but that's beside the
point, for the most part. Here's the question: I want to be able to change
the references (including deleting them). Is there any way to do that
besides using pointers rather...
|
by: Jon Slaughter |
last post by:
Are methods of a class created only once for all objects or for each object
as its own copy of its methods?
It seems that I can only access an object method's address using :: so if I
have a class with method func and I want its address I would do
&someClass::someFunc which seems to imply that there is only one function
per class(and hence...
|
by: Alex Maghen |
last post by:
I want to create an ASP.NET class which generates some client-side HTML
objects on a page with the following rules:
1. Only generate these objects from the class if they haven't already been
inserted by another instantiation of this class. So if the obejcts have
already been inserted, determine that this is the case and don't do it again.
...
| |
by: polocar |
last post by:
Hi,
I'm writing a program using Visual C# 2005 Professional Edition, and
I was trying to assign multiple MainMenu objects (one by one, of
course) to the same Form (let's suppose 2 MainMenu objects).
It is possible (and it's my case) that these 2 MainMenu objects use
some different MenuItem objects and some identical MenuItem objects.
For...
|
by: Nemisis |
last post by:
hi guys,
Currently converting an old classic asp system to a OOP asp.net
application. We are building the new application using a 3 tier
arcitecture and i was wondering about the following.
I have a parent class, that when inserted, inserts a few child classes
into the database, based on other classes. In the old system, this is
wrote...
|
by: Danny Liberty |
last post by:
I need some opionions on an issue here...
Suppose I want to keep a collection of objects, each need to be
uniquely identified by a number. This number has no meaning as long as
it's unique, so it should be automatically generated for each new
object. One more requirement is keeping the objects in the order in
which they were inserted, not...
|
by: walterbyrd |
last post by:
Python seems to have a log of ways to do collections of arbitrary
objects: lists, tuples, dictionaries. But what if I want a collection
of non-arbitrary objects? A list of records, or something like that?
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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. ...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |