sorting through a set of doubles

P

pauldepstein

Suppose x is of the type std::set<double>

I wanted to do a basic sort on x so that the elements were in
numerical order, in the sense that an iterator x.begin() would point
to the least element and so on.

My command std::sort(x.begin(), x.end()); generated compiler errors.
What should I have done?

Thank you,

Paul Epstein
 
I

Ioannis Gyftos

Suppose x is of the type std::set<double>

I wanted to do a basic sort on x so that the elements were in
numerical order, in the sense that an iterator  x.begin() would point
to the least element and so on.

My command std::sort(x.begin(), x.end());  generated compiler errors.
What should I have done?

Thank you,

Paul Epstein

Isn't an std::set already sorted?

What were the compiler errors?

http://www.cppreference.com/cppset/index.html
 
P

puzzlecracker

Suppose x is of the type std::set<double>

I wanted to do a basic sort on x so that the elements were in
numerical order, in the sense that an iterator  x.begin() would point
to the least element and so on.

My command std::sort(x.begin(), x.end());  generated compiler errors.
What should I have done?

Thank you,

Paul Epstein

std:set has a function value_comp, which returns a function used to
sort elements in the set. I presume there is a way to set this to a
different function. Would someone confirm that?
 
P

Pascal J. Bourguignon

Suppose x is of the type std::set<double>

I wanted to do a basic sort on x so that the elements were in
numerical order, in the sense that an iterator x.begin() would point
to the least element and so on.

My command std::sort(x.begin(), x.end()); generated compiler errors.
What should I have done?

std::set<double> x;
std::vector<double> y(x.size());
std::copy(x.begin(),x.end(),y.begin());
std::sort(y.begin(),y.end());

Now y is sorted, and *(y.begin()) is the least element.
 
K

Kai-Uwe Bux

It already does unless you have used a special comparison predicate upon
construction of x.

Nothing.


std:set has a function value_comp, which returns a function used to
sort elements in the set. I presume there is a way to set this to a
different function. Would someone confirm that?

You can only set this comparison function upon construction of the set.
Changing it later (causing a resort) is impossible.


Best

Kai-Uwe Bux
 
P

puzzlecracker

It already does unless you have used a special comparison predicate upon
construction of x.


You can only set this comparison function upon construction of the set.
Changing it later (causing a resort) is impossible.

Best

Kai-Uwe Bux

How do you set a comparison functions for set? via template
instantiation?
 
K

Kai-Uwe Bux

puzzlecracker said:
How do you set a comparison functions for set? via template
instantiation?

The answer has two parts:

a) std::set<> has a template parameter for the comparison function. It
defaults to std::less< value_type >. In order to provide your own, you have
to specify the template parameter.

b) In std::set< T, A, C >, the default constructor (and maybe some others)
takes arguments for the allocator and the comparison predicate. They
default to A() and C(). So, if the comparison template parameter has the
right default, you don't need to do anything. However, if you use something
like

std::function< bool( T const &, T const & ) >

for C, you will need to provide an appropriate functor.


Best

Kai-Uwe Bux
 
J

Juha Nieminen

Pascal said:
std::set<double> x;
std::vector<double> y(x.size());
std::copy(x.begin(),x.end(),y.begin());
std::sort(y.begin(),y.end());

Now y is sorted, and *(y.begin()) is the least element.

"Now sorted"? The data was *already* sorted to begin with. Why sort it
again?
 
P

Pascal J. Bourguignon

Pete Becker said:
Funny thing, though: the result is exactly the same if you comment out
the last line.

Yes, it's just me being paranoiac. However, we could add a different
comparator either in std::set<> or in std::sort().
 
J

James Kanze

"Iterator is not of random-access kind" would be my bet. And,
of course, no additional sorting of the set is needed, it's
already in the ascending order.

Iterator isn't random access, but also, you can't assign through
it (e.g. *iter = ... isn't legal).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top