Here is my program:
class Test {
private:
int _num;
};
int main()
{
set<TestaSet;
Test aTest;
aSet.insert(aTe st);
return 0;
}
when compiling, vs2003 complain about operator <. If I create a class which
will be used in set, I have to implement or overload operator <? Thanks in
advance. 5 8193
Austin wrote:
Here is my program:
class Test {
private:
int _num;
};
int main()
{
set<TestaSet;
Test aTest;
aSet.insert(aTe st);
return 0;
}
when compiling, vs2003 complain about operator <. If I create a class
which
will be used in set, I have to implement or overload operator <? Thanks
in advance.
Why use Set in the first place ?
On 2007-04-29 16:28, Austin wrote:
Here is my program:
class Test {
private:
int _num;
Try not to use names which start with _, in certain scopes they are
reserved.
};
int main()
{
set<TestaSet;
Test aTest;
aSet.insert(aTe st);
return 0;
}
when compiling, vs2003 complain about operator <. If I create a class which
will be used in set, I have to implement or overload operator <? Thanks in
advance.
No you can specify another comparator to use instead, something like
this (untested):
class Test {
int num_
friend bool cmp(const Test&, const Test&);
};
bool cmp(const Test& a, const Test& b)
return a.num_ < b.num_
}
int main()
{
std::set<Test, cmps;
}
But it's usually much easier to just implement operator<.
--
Erik Wikström
"Austin" wrote:
Here is my program:
class Test {
private:
int _num;
};
int main()
{
set<TestaSet;
Test aTest;
aSet.insert(aTe st);
return 0;
}
when compiling, vs2003 complain about operator <. If I create a class
which
will be used in set, I have to implement or overload operator <? Thanks
in
advance.
If the set consists of fundamental types, such as int, I would expect there
would be no glitch. But you have hidden your int in a class and the
compiler has no knowledge about the innards of that class. Except for the
fear factor, it should not be a big deal. Something like (untested)
bool operator<(const Test arg) {if(_num < arg._num) return
_num<arg._num;}
Austin wrote:
>
when compiling, vs2003 complain about operator <. If I create a class which
will be used in set, I have to implement or overload operator <? Thanks in
advance.
Yes, set needs a "less" relation to work. This means you must either
implement operator< or you must provide a function object to the set
to accomplish that (this by default is std::less which uses the objects
operator<).
osmium wrote:
>
bool operator<(const Test arg) {if(_num < arg._num) return
_num<arg._num;}
And what does this function return if _num is not less than arg._num? <g>
For the humor-impaired, it's obviously a typo.
--
-- Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." ( www.petebecker.com/tr1book) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ma740988 |
last post by:
The position returned via the STL std::set container never made much
sense to me. When you insert elements within the container, the
position returned - via find - does not reflect the actual position of
the value within the container. std::string - for instance - does.
How then do I get the actual position of the value when using
std::set?
# include <iostream>
using std::cout;
using std::cin;
|
by: snnn |
last post by:
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_iterator. 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...
|
by: Peter Jansson |
last post by:
Hello,
I have the following code:
std::map<int,std::set<std::string> > k;
k="1234567890";
k="2345678901";
//...
std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)
throw(std::runtime_error)
|
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: 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: mathieu |
last post by:
hi there,
I would like to know if the following piece of code is garantee to
work. I am afraid that the cstring address I am using in the std::map
found from a request in std::set is not garantee to remain the same as
the std::set grows...
thanks
-Mathieu
|
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: 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...
|
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: 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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: 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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |