•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,022 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,658 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3715 | Replies: 30
![]() |
From a help file:
•
•
•
•
Templated algorithm for sorting collections of entities.
Syntax
#include <algorithm>
template <class RandomAccessIterator>
void sort (RandomAccessIterator first,
RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first,
RandomAccessIterator last, Compare comp);
Description
The sort algorithm sorts the elements in the range [first, last) using either the less than (<) operator or the comparison operator comp. If the worst case behavior is important stable_sort or partial_sort should be used.
sort performs approximately NlogN, where N equals last - first, comparisons on the average.
Rogue Wave Standard C++ Library User's Guide and Tutorial
Hi guys,
Due to trying to understand what the operator< actually was ment to do, I was trying something like this:
Wich shows that the operator compares the object p with the object q and depending on the result will returns bool (0 or 1) to main.
Is that the reason why in the exercise there is written
But instead with two different objects, you use is as is written on the pairs by determining wether the first number is smaller then the second, if not then you use the 'sort' algorithm to put them in the correct order?
Sorry to ask this, but I'm still confused about the actual intention of this part of the exercise, I don't actually understand what the intention is from that :o
Am I also correct that I could write instead of that if statement, just simply this:
Due to trying to understand what the operator< actually was ment to do, I was trying something like this:
class point
{
private:
int x, y, z;
public:
point (int xx = 0, int yy = 0): x (xx), y (yy){}
vector<int> points;
void numbers()
{
while (cin>> x >> y, !cin.fail())
{
points.push_back(x);
points.push_back(y);
}
}
bool operator< (const point &b)
{
return x < b.x || (x == b.x && y < b.y);
}
void print()
{
int n = points.size();
for (z = 0; z < n; z++)
cout<< points[z] <<endl;
}
};
int main()
{
point p, q (10, 16);
int a;
cout<<"Type twee integere getallen in per keer: "<<endl;
p.numbers();
p.print();
if (p < q){a = 0;}
else{a = 1;}
cout<< a;
return 0;
}Wich shows that the operator compares the object p with the object q and depending on the result will returns bool (0 or 1) to main.
Is that the reason why in the exercise there is written
•
•
•
•
Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y by using the defined order operator <.
But instead with two different objects, you use is as is written on the pairs by determining wether the first number is smaller then the second, if not then you use the 'sort' algorithm to put them in the correct order?
Sorry to ask this, but I'm still confused about the actual intention of this part of the exercise, I don't actually understand what the intention is from that :o
Am I also correct that I could write instead of that if statement, just simply this:
cout<< (p<q) <<endl;
•
•
•
•
Originally Posted by JoBe
Is that the reason why in the exercise there is written
••••Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y by using the defined order operator <.
But instead with two different objects, you use is as is written on the pairs by determining wether the first number is smaller then the second, if not then you use the 'sort' algorithm to put them in the correct order?
Sorry to ask this, but I'm still confused about the actual intention of this part of the exercise, I don't actually understand what the intention is from that :o
•
•
•
•
Originally Posted by JoBe
Am I also correct that I could write instead of that if statement, just simply this:cout<< (p<q) <<endl;
cout<< !(p<q) <<endl;•
•
•
•
Originally Posted by Dave Sinkula
Actually it would becout<< !(p<q) <<endl;
Why has there got to be a '!' infront of it, I tried it out without it and it works aswell, or is this reversing the 0 and 1 if I don't write the '!' infront of it?
•
•
•
•
Originally Posted by JoBe
or is this reversing the 0 and 1 if I don't write the '!' infront of it?
•
•
•
•
Originally Posted by Dave Sinkula
I'm reading it like this: use the STL sort -- this requires that you only write an operator< [saving you from needing to write some sort algorithm of your own that will be far too much fun to debug -- all that for the price of one easy function, not bad].
But, do you think it's the intention of the exercise to add the values of q(10,16) to the values of p and sort them again

So that you would get something like this:
Values entered for p:
10 15
35 20
25 5
Sorted:
5
10
15
20
25
35
And then: q(10, 16) added to p would give sorted.
5
10
10
15
16
20
25
35
How do I do that

I'm almost certain that you can do this by combining the operator< and the sort algorithm, but I can't see how
I take the point of the exercise to mean something like this.
int main()
{
vector<point> s;
point u(10,15),v(35,20),w(25,5);
s.push_back(u);
s.push_back(v);
s.push_back(w);
print(s);
point x(10,16);
s.push_back(x);
print(s);
cin.get();
return 0;
}
/* my output
sorting...
10,15
25,5
35,20
sorting...
10,15
10,16
25,5
35,20
*/ >Also, the numbers are read from the keyboard, not const integers.
Yes. I tire of repeatedly entering the same information for such examples. The point is that somehow numbers are gathered, then they are sorted, then more are added, and the they are resorted.
>Problem is, the exercise mentions the combination of operator< and the sort algorithm.
You need operator< to use sort.
>I don't see how I have to combine those two to get them into one list.
I don't think they are intertwined like you appear to believe. I think the exercise is trying to be simple, you have have way overcomplicated it.
Yes. I tire of repeatedly entering the same information for such examples. The point is that somehow numbers are gathered, then they are sorted, then more are added, and the they are resorted.
>Problem is, the exercise mentions the combination of operator< and the sort algorithm.
You need operator< to use sort.
>I don't see how I have to combine those two to get them into one list.
I don't think they are intertwined like you appear to believe. I think the exercise is trying to be simple, you have have way overcomplicated it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- C++ derived class virtual functions (C++)
- Reading an input file as a class memeber function (C++)
- Class Vectorization requirements (C++)
- Error pushing pointers to a vector (C++)
- Is ArrayList Better than Vector (Java)
- 6 Line class -> Me pounding head into wall (C)
Other Threads in the C++ Forum
- Previous Thread: Draw a house into a window in C++ (?!)
- Next Thread: How bad is this?



Linear Mode