Hello ladies and gents,

Wanted to make this part of an exercise in wich I enter each time two integers into an array for wich I have to use STL container vector.

Ive got this sofar, but getting an error message when I entered two numbers and the exe shuts down? I'm almost certain it's got to do with the

while (cin>> x >>" ">> y, !cin.fail())

But, not sure about it and even if, don't know how to solve it?

What Ive got sofar in total is this:

#include <iostream>
#include <vector>

using namespace std;

class point
{
private:
	int x, y, z;

public:
	point (int xx = 0, int yy = 0, int zz = 0): x (xx), y (yy), z (zz) {}

	vector<int> s, t;

	void numbers()
	{
		while (cin>> x >>" ">> y, !cin.fail())
		{
			s.push_back(x);
			s.push_back(y);
		}

	}

	void print()
	{
		int n = s.size();

		for (z = 0; z < n; z++)
			cout<< s[z] <<endl;
	}
};

int main()
{
	point u;

	cout<<"Type twee integere getallen in per keer: "<<endl;

	u.numbers();

	u.print();

	cin.get();

	return 0;
}

Could someone please point me in the right direction, thank you ;)

Found it, it's this wich was causing the problem

>>" ">>

Still, if someone might want to have a look at the part of code that I wrote allready and give some hints or suggestions to improve it, I'd very much appreciate it ;)

Hi Dave,
Ive allready changed the cin part :)

Input for example:

10 20
30 40
50 60
stop

Output should be and is now:
10
20
30
40
50
60

This is just a small part of the exersize tough, have to include the operators == and <.

But, the exersize also talkes about two point-objects?? I'm I correct in thinking that this is when a class object has two datamembers?

Something like this,

class point
{
point (int xx = 0, int yy = 0): x (xx), y (yy){} --> two datamembers x and y.
...
};

int main()
{
point u;--> one class object 
...
}

If you don't understand what I'm trying to say, then I'll translate the entire exersize.

By the way, have you heard anything from Narue, strange for her to stay away for so long :?:

Do you want a vector of points instead? That is,

#include <iostream>
#include <vector>

using namespace std;

class point
{
   int x, y, z;
public:
   point (int xx = 0, int yy = 0, int zz = 0): x (xx), y (yy), z (zz) {}
   bool get()
   {
      cout << "Type twee integere getallen in per keer: " << endl;
      return cin >> x >> y;
   }
   void print()
   {
      cout << x << "," << y << endl;
   }
};

class something
{
   vector<point> s;
public:
   something()
   {
      point u;
      while ( u.get() )
      {
         s.push_back(u);
      }
   }
   void print()
   {
      for ( int n = s.size(), z = 0; z < n; z++ )
         s[z].print();
   }
};

int main()
{
   something interesting;
   interesting.print();
   cin.get();
   return 0;
}

/* my output
Type twee integere getallen in per keer:
1 2 3 4 5 6 stop
Type twee integere getallen in per keer:
Type twee integere getallen in per keer:
Type twee integere getallen in per keer:
1,2
3,4
5,6
*/

>By the way, have you heard anything from Narue, strange for her to stay away for so long

[voice=Sgt. Shultz]I know nothing.[/voice]

>> while (cin>> x >>" ">> y, !cin.fail())
Since you already know what the problem was, I'll tell you why it's a problem, just to be thorough. cin >> is strictly for formatted input. It doesn't accept formatting strings and it already skips whitespace by default. If you want to skip precise strings then you can do this (nonportable):

#include <iostream>

namespace std {
  template <typename CharT, typename Traits>
  basic_istream<CharT, Traits>&
    operator>>(basic_istream<CharT, Traits>& in, const char *fmt)
  {
    while (in.good() && *fmt != '\0') {
      if (in.get() != *fmt++)
        in.setstate(ios::failbit);
    }

    return in;
  }
}

int main()
{
  int a, b;

  std::cout << "Enter two numbers separated by =-=: ";
  std::cin >> a >> "=-=" >> b;
  std::cout << "a: " << a << '\n' << "b: " << b << '\n';
}

It's not portable because you technically aren't allowed to add stuff to the std namespace. You can do it portably by writing a manipulator:

#include <iostream>

class skip {
  const char *_fmt;
public:
  skip(const char *fmt): _fmt(fmt) {}

  template <typename CharT, typename Traits>
  friend std::basic_istream<CharT, Traits>&
    operator>>(std::basic_istream<CharT, Traits>& in, const skip& sk)
  {
    const char *tmp = sk._fmt;

    while (in.good() && *tmp != '\0') {
      if (in.get() != *tmp++)
        in.setstate(std::ios::failbit);
    }

    return in;
  }
};

int main()
{
  int a, b;

  std::cout << "Enter two numbers separated by =-=: ";
  std::cin >> a >> skip("=-=") >> b;
  std::cout << "a: " << a << '\n' << "b: " << b << '\n';
}

But that's not as clean, so unless somebody is looking, go for the first option. ;)

>Do you want a vector of points instead?
Euh, no, don't think I need that, but thanks for the example, why would you use this?

The translation of the exersize is:

Define a class "point" with two datamembers, x and y. Use the STL-container vector to stack a row of point-objects. Enter them by using the keyboard by two(pairs), x and y. It's not known how many (pairs) there will be entered. Use a non numeric character to stop the input of the (pairs) x and y.

(<-- so far so good, no problem)

But this part of the exersize, I don't really understand what I'm supposed to do:

Define the operators == and < for two point-objects in wich p<q has the following meaning: p.x < q.x || (p.x == q.x && p.y < q.y)
Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y by using the defined order operator <.

I understand that those operators "== and <" are linked towards this: "p.x < q.x || (p.x == q.x && p.y < q.y)" But, don't know how this should get incorporated into a piece of code?

>[voice=Sgt. Shultz]I know nothing.[/voice]
LOL :lol:

[voice=Homer].....DOH,.. ssstupid policeman[/voice]

Hey Dogtree,

Thanks for the explanation man ;)

But euh, ... if you don't mind, I'm going to skip it in this exersize since it's not being asked to implement it, I'm having a hard time allready getting it to do what it should without incorporating some more code :cheesy:

But, greatly appreciated man :!:

>> Define a class "point" with two datamembers, x and y.

struct point {
  int x, y;
};

>> Use the STL-container vector to stack a row of point-objects.

#include <vector>

std::vector<point> points;

>> Enter them by using the keyboard by two(pairs), x and y.
>> Use a non numeric character to stop the input of the (pairs) x and y.

while (std::cin.peek() != 'q') {
  point item;
  int x, y;

  if (!(std::cin >> x >> y))
    break;

  item.x = x;
  item.y = y;

  points.push_back(item);
}

>> Define the operators == and < for two point-objects

bool operator==(const point& a, const point& b)
{
  return a.x == b.x && a.y == b.y;
}

bool operator<(const point& a, const point& b)
{
  return a.x < b.x || (a.x == b.x && a.y < b.y);
}

>> Use the STL-algorithm 'sort' to sort the previous entered (pairs) x and y
>> by using the defined order operator <.
std::sort uses operator< by default, so you can simply do this:

std::sort(points.begin(), points.end());

Why do people like you, Narue, Dave,... make it all look so simple, pfff, life isn't fair you know LOL :lol:

Thanks Dogtree, though, have one favor to ask you, if possible, could you give me some hints and clues in the future or even examples wich are related to the exersize instead of the solution because, that way, I'll learn much more then by simply getting the solution on a silver platter.

Please, don't missunderstand me, I really appreciate your help, but, I'd like to try the exersize first, if I really am not able to solve it, after getting tips, hints, examples, from you, Dave, Narue, Vegaseat, Asif, 1OOOBHP,... I'll ask you to help me out with the solution ;)

Anyway, thanks for the solution, this way, I'll get to the next chapter much faster :cheesy:

There's really no way I could have hinted how to define the operators and call sort that would be useful without giving code. Don't worry, if possible I try not to solve other people's problems. ;) I also didn't give it to you on a silver platter. There are multiple ways to do something, some better than others, and you would still have to piece it all together even if you did use the exact code I gave.

>> Why do people like you, Narue, Dave,... make it all look so simple
We make it look simple because we worked really hard to learn it, just like you're doing now.

Why do people like you, Narue, Dave,... make it all look so simple, pfff, life isn't fair you know LOL :lol:

Practice, practice, [failure, failure, failure << me at least>> ], practice...

Please, don't missunderstand me, I really appreciate your help, but, I'd like to try the exersize first, if I really am not able to solve it, after getting tips, hints, examples, from you, Dave, Narue, Vegaseat, Asif, 1OOOBHP,... I'll ask you to help me out with the solution ;)

I generally don't try to provide complete solutions to exact questions, especially homework. But sometimes it's just easier for me to try to explain what I'm thinking with code. Or I feel that posting a correct method of whatever is much better than "STFW for a (likely bad) example". And since the web is awash with bad examples, I try to make sure I'm posting only a correct bit of compilable code that demonstrates some portion of an answer; or a different approach to doing the same thing.

It's sort of a "the more good code that people can see, the more likely they will be to become good coders" type of thing -- and in the process of trying to tell someone else how to do something, you often end up learning a heckuva lot more than than you expect to.

Lather, rinse, repeat... and keep trying to get better.

Hi guys,

Why am I able to write this:

#include <iostream>
#include <vector>
#include <algorithm>

class point
{
...
public:
	...
	void sort()
	{
		std::sort(s.begin(), s.end());
	}
	
	...
};

int main()
{
	point u;

	...
	u.sort();

	...
}

But not this:

class point
{
...
};

int main()
{
	...
             vector<point>points; 

	std::sort(points.begin(), points.end());

	...
}

It would be much easier to answer if you didn't snip it to ... all over the place. I don't really care to guess at what goes into all the ...'s and attempt to get something to compile.

Sorry Dave,

Tought you wouldn't need the total code and therefore posted only those parts. Don' worry about the operators '==' '<', haven't gotten them in working order yet ;)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class point
{
private:
	int x, y, z;

public:
	point (int xx = 0, int yy = 0): x (xx), y (yy){}

	vector<int> s;

	void numbers()
	{
		while (cin>> x >> y, !cin.fail())
		{
			s.push_back(x);
			s.push_back(y);
		}
	}

	/*void sort()
	{
		std::sort(s.begin(), s.end());
	}*/
	
	/*bool operator==(const point &a, const point &b)
	{
		return a.x == b.x && a.y == b.y;
	}

	bool operator< (const point &a, const point &b)
	{
		return a.x < b.x || (a.x == b.x && a.y < b.y);
	}*/

	void print()
	{
		int n = s.size();

		for (z = 0; z < n; z++)
			cout<< s[z] <<endl;
	}
};

int main()
{
	point u;

	vector<point>points; 

	cout<<"Type twee integere getallen in per keer: "<<endl;

	u.numbers();

	u.print();

	//u.sort();

	std::sort(points.begin(), points.end());

	u.print();

	cin.get();

	return 0;
}

I get an error message (trimmed):

'operator<' not implemented in type 'point'

With that and your previous reply, in the first case you have a vector of ints? And the second you have a vector of points? I believe the reason might be that operator< is implemented for ints.

[edit]

Tought you wouldn't need the total code and therefore posted only those parts.

Little things like vector<int> vs vector<point> often lurk in ..., but many times that is where the answer is.

Wait Dave,

What's that got to do with

std::sort(points.begin(), points.end());

As I said in the previous post, I'm not working with the operator functions just yet.

What's that got to do with

std::sort(points.begin(), points.end());

As I said in the previous post, I'm not working with the operator functions just yet.

I believe std::sort uses operator< to do the sorting.

class point
{
   // ... (sorry I mean these are just the couple changes to what you have)
   bool operator< (const point &b)
   {
      return x < b.x || (x == b.x && y < b.y);
   }

   friend bool operator< (const point &a, const point &b);
};

bool operator< (const point &a, const point &b)
{
   return a.x < b.x || (a.x == b.x && a.y < b.y);
}

Suddenly it compiles.

[Reminder: C++ is not my native language.]

Sorry DAve,

But let me get this straight, you're saying that

std::sort(points.begin(), points.end());

Is related to this part

class point
{
   bool operator< (const point &b)
   {
      return x < b.x || (x == b.x && y < b.y);
   }

   friend bool operator< (const point &a, const point &b);
};

bool operator< (const point &a, const point &b)
{
   return a.x < b.x || (a.x == b.x && a.y < b.y);
}

Now I'm really confused :confused:

Is this like you are determinating one bool operator first and that solution is given to the friend operator as one of the parameters??

How would you perform a sort if you didn't have a comparison function?
How would the compiler know if widget a is supposed to be before widget b?

Ok, thanks for the explanation Dave, I'll give it a test run to see how it works!

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:

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;

:?:

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].

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;

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?

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".

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 :confused:

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
*/

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.

>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.

This question has already been answered. Start a new discussion instead.