Multiple datatype in std::vector

Hi,

I have this struct which returns double value of x & y. I want one more datatype in return(with this double), suppose of type int.
How to get that?What modifications i should do in this struct ?

struct pointt
{
pointt(double _x, double _y) : x(_x), y(_y) {}
bool operator < (const pointt &p) const {
if (*this == p) {
return x < p.x;
}
return y < p.y;
}
bool operator > (const pointt &p) const {
if (*this == p) {
return x > p.x;
}
return y > p.y;
}
bool operator==(const pointt& p) const {
return (y == p.y);
}
double x, y;
};
Last edited on
What are you trying to accomplish?
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2)
I have this struct which returns double value of x & y.


Can you explain what you mean by this? A struct doesn't return anything. A struct is a type of object which contains data members, and methods. Functions and methods return things, not structs.

3) I'm really confused by the logic of your operators. In your equality operator:

1
2
3
bool operator==(const pointt& p) const {
  return (y == p.y);
}


You're only comparing the y members. This means that, even if the two objects have different values for x, then your operator will report that they are the same. Is that what you really want? How does that make sense?
Last edited on
This was my previous post

HI,
I have a struct defined as

std::vector<pointt> arr;
struct pointt
{
pointt(double _x, double _y) : x(_x), y(_y) {}
bool operator < (const pointt &p) const {return (p.y < y);}
bool operator > (const pointt &p) const {return (p.y > y);}
double x, y;
};
I am adding values in arr.push_back
which is coming from some function at runtime

I am sort this see below

std::sort(arr.begin(), arr.end());
std::reverse(arr.begin(), arr.end());

my list now shows(in increasing order of values of y)
eg:
(27.98,9.045)
(26.98,9.045)
(27.51,9.045)
(16.14,22.14)



Now My requirement is like this
for the same value of y, I have to arrange x in increasing order
from above list I want
(26.98,9.045)
(27.51,9.045)
(27.98,9.045)
(16.14,22.14)
only for the same value of y, I have to arrange x in increasing order
Thanks
i got the above list using below struct

struct pointt
{
pointt(double _x, double _y) : x(_x), y(_y) {}
bool operator < (const pointt &p) const {
if (*this == p) {
return x < p.x;
}
return y < p.y;
}
bool operator > (const pointt &p) const {
if (*this == p) {
return x > p.x;
}
return y > p.y;
}
bool operator==(const pointt& p) const {
return (y == p.y);
}
double x, y;
};

I am inserting values using loop
Eg: for(int ii=0; ii<something;ii++)
{
arr.push_back(pointt(something[ii]).X,something[ii]).y));
}

Now i want to insert another dayatype in this struct
for(int ii=0; ii<something;ii++)
{
arr.push_back(pointt(something[ii]).X,something[ii]).y,something[ii].string));
}
So, look, in my previous post, I asked you to use code tags, to make your code readable.

If you're not going to pay attention to what we're writing, why should we carry on posting more replies?

If you're not going to put any effort into making your posts readable, why should we put in effort trying to read it?
ok!will keep in mind :)
What's stopping you editing your posts in this thread to make them readable?

This is for your benefit - the clearer your posts are to read, the more likely it is that people here will make the effort to read them and help you.
I'd add a default constructor to pointt.

You don't need > and == operators. sort() only requires operator<
Now My requirement is like this
for the same value of y, I have to arrange x in increasing order

That means you have to sort by y value first, then by x value to break ties:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
#include <algorithm>

using std::vector;
using std::cin;
using std::cout;
using std::sort;

struct pointt {
    pointt() : x{0}, y{0} {}
    pointt(double x, double y):x(x), y(y)
    {}
    bool operator <(const pointt & p) const
    {
	if (y < p.y) {
	    return true;
	} else if (y > p.y) {
	    return false;
	} else {
	    return x < p.x;	// y values are equal.
	}			
    }
    double x, y;
};

vector<pointt> arr;

int main()
{
    pointt p;
    while (cin >> p.x >> p.y) {
	arr.push_back(p);
    }
    sort(arr.begin(), arr.end());
    for (auto &p : arr) {
	cout << p.x << ' ' << p.y << '\n';
    }
}

Ahh, a continuation for http://www.cplusplus.com/forum/general/197244/

Your new question:
I want one more datatype in return(with this double), suppose of type int. How to get that? What modifications i should do in this struct ?

In other words, you have a struct that already:
* has member x
* has member y
... and you don't know how to add a third member?

Why is it hard for me to understand how you have managed to add two members to the struct without being able to add more?
The types? Do you know that each member has its own type (despite the use of comma-separated list syntax for declaring x and y in one statement). The x is a double and the y is a double, but one member being a double does not force the other members to be doubles.


The std::sort() can be used for more than just the ascending order. It has a form that takes third argument -- "predicate" -- that will determine the ordering.
See http://www.cplusplus.com/reference/algorithm/sort/
Topic archived. No new replies allowed.