473,781 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::set

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.
Apr 29 '07 #1
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 ?
Apr 29 '07 #2
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
Apr 29 '07 #3
"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;}
Apr 29 '07 #4
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<).
Apr 29 '07 #5
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)
Apr 29 '07 #6

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

Similar topics

7
2639
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;
11
3253
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...
5
8752
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)
10
7561
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
5088
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
8155
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.
7
2593
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
5770
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
3092
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
2
2797
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
9636
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
10306
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...
0
10139
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 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...
1
10075
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
8961
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, 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...
1
7485
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
6727
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
5373
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...
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.