User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2004
Posts: 411
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 3
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #21  
May 25th, 2005
Ok, thanks for the explanation Dave, I'll give it a test run to see how it works!
Reply With Quote  
Join Date: Apr 2004
Posts: 3,626
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class with use of <vector>

  #22  
May 25th, 2005
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
Reply With Quote  
Join Date: Sep 2004
Posts: 411
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 3
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #23  
May 26th, 2005
Hi guys,

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;
Reply With Quote  
Join Date: Apr 2004
Posts: 3,626
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class with use of <vector>

  #24  
May 26th, 2005
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
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].

Originally Posted by JoBe
Am I also correct that I could write instead of that if statement, just simply this:
cout<< (p<q) <<endl;
Actually it would be
cout<< !(p<q) <<endl;
Reply With Quote  
Join Date: Sep 2004
Posts: 411
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 3
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #25  
May 26th, 2005
Originally Posted by Dave Sinkula
Actually it would be
cout<< !(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?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,626
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class with use of <vector>

  #26  
May 26th, 2005
Originally Posted by JoBe
or is this reversing the 0 and 1 if I don't write the '!' infront of it?
Yes. Reread your "if" version as, "if true, print false; if false, print true".
Reply With Quote  
Join Date: Sep 2004
Posts: 411
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 3
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #27  
Jun 5th, 2005
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
Reply With Quote  
Join Date: Apr 2004
Posts: 3,626
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class with use of <vector>

  #28  
Jun 5th, 2005
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
*/
Reply With Quote  
Join Date: Sep 2004
Posts: 411
Reputation: JoBe is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 3
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Class with use of <vector>

  #29  
Jun 5th, 2005
Hi DAve,

Problem is, the exercise mentions the combination of operator< and the sort algorithm. I don't see how I have to combine those two to get them into one list.

Also, the numbers are read from the keyboard, not const integers.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,626
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Class with use of <vector>

  #30  
Jun 5th, 2005
>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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:42 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC