473,386 Members | 1,804 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,386 software developers and data experts.

std::set

Here is my program:

class Test {
private:
int _num;
};

int main()
{
set<TestaSet;
Test aTest;
aSet.insert(aTest);
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 8157
Austin wrote:
Here is my program:

class Test {
private:
int _num;
};

int main()
{
set<TestaSet;
Test aTest;
aSet.insert(aTest);
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(aTest);
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(aTest);
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
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...
11
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?...
5
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)...
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...
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: 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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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,...
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...

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.