STL vector iterator question

T

T. Crane

Hi all,

I have some data that I am using a vector<vector<double> > container
to hold. The data is n sets of (x,y,z,intensity) data points. I can
either group my data like this:

vector<vector<double> > v(n, vector<double>(4)); // method A

or like this:

vector<vector<double> > v(4, vector<double>(n)); // method B

This difference, of course, is that in the first method, I have n 4-
element vectors and in the second I have 4 n-element vectors.

Next, I want to find the (x,y,z) coordinates of the point that has the
highest intensity. Finding the highest intensity is easy when I use
method B to group the data.

All I have to do is:

vector<double>::iterator maxInt = max_element(v[3].begin(),
v[3].end());

The max intensity is found by dereferencing maxInt. However, at this
point I'm at a loss as to how to get the corresponding (x,y,z) values
for that max intensity.

Alternatively, if I were to group the data using method A, once I have
a max intensity, it's trivial to find the (x,y,z) values, but I don't
know a good (i.e. easy, elegant, whatever) way to find the max
intensity short of writing a max_element-like function.

Any suggestions?

thanks,
trevis
 
V

Victor Bazarov

T. Crane said:
Hi all,

I have some data that I am using a vector<vector<double> > container
to hold. The data is n sets of (x,y,z,intensity) data points. I can
either group my data like this:

vector<vector<double> > v(n, vector<double>(4)); // method A

or like this:

vector<vector<double> > v(4, vector<double>(n)); // method B

This difference, of course, is that in the first method, I have n 4-
element vectors and in the second I have 4 n-element vectors.

Next, I want to find the (x,y,z) coordinates of the point that has the
highest intensity. Finding the highest intensity is easy when I use
method B to group the data.

All I have to do is:

vector<double>::iterator maxInt = max_element(v[3].begin(),
v[3].end());

The max intensity is found by dereferencing maxInt. However, at this
point I'm at a loss as to how to get the corresponding (x,y,z) values
for that max intensity.

I believe you should be able to do

size_t indMax = maxInt - v[3].begin();
double x = v[0][indMax],
y = v[1][indMax],
z = v[2][indMax];
Alternatively, if I were to group the data using method A, once I have
a max intensity, it's trivial to find the (x,y,z) values, but I don't
know a good (i.e. easy, elegant, whatever) way to find the max
intensity short of writing a max_element-like function.

... = max_element(v.begin(), v.end(), my_compare());

where 'my_compare' is this:

struct my_compare {
bool operator()(vector<double> const& v1, vector<double const& v2)
{
return v1[3] < v2[3]; // compare intensities
}
};

V
 
T

T. Crane

T. Crane said:
I have some data that I am using a vector<vector<double> > container
to hold. The data is n sets of (x,y,z,intensity) data points. I can
either group my data like this:
vector<vector<double> > v(n, vector<double>(4)); // method A
or like this:
vector<vector<double> > v(4, vector<double>(n)); // method B
This difference, of course, is that in the first method, I have n 4-
element vectors and in the second I have 4 n-element vectors.
Next, I want to find the (x,y,z) coordinates of the point that has the
highest intensity. Finding the highest intensity is easy when I use
method B to group the data.
All I have to do is:
vector<double>::iterator maxInt = max_element(v[3].begin(),
v[3].end());
The max intensity is found by dereferencing maxInt. However, at this
point I'm at a loss as to how to get the corresponding (x,y,z) values
for that max intensity.

I believe you should be able to do

size_t indMax = maxInt - v[3].begin();
double x = v[0][indMax],
y = v[1][indMax],
z = v[2][indMax];
Alternatively, if I were to group the data using method A, once I have
a max intensity, it's trivial to find the (x,y,z) values, but I don't
know a good (i.e. easy, elegant, whatever) way to find the max
intensity short of writing a max_element-like function.

... = max_element(v.begin(), v.end(), my_compare());

where 'my_compare' is this:

struct my_compare {
bool operator()(vector<double> const& v1, vector<double const& v2)
{
return v1[3] < v2[3]; // compare intensities
}
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

Cool -- I was thinking of something along the lines of the latter
suggestion, but I hadn't thought of the former.

thanks for your help,
trevis
 
P

Pete Becker

The max intensity is found by dereferencing maxInt. However, at this
point I'm at a loss as to how to get the corresponding (x,y,z) values
for that max intensity.

Alternatively, if I were to group the data using method A, once I have
a max intensity, it's trivial to find the (x,y,z) values, but I don't
know a good (i.e. easy, elegant, whatever) way to find the max
intensity short of writing a max_element-like function.

Back in the days of FORTRAN, we used parallel arrays to store data
values because FORTRAN didn't have structured data. In C++ we have
structured data, and parallel arrays are rarely appropriate. Is there a
good reason for using the old-fashioned (<g>) approach?

struct point_intensity
{
double x;
double y;
double z;
double intensity;
};

vector<point_intensity> v;

struct compare_intensity
{
bool operator()(const point_intensity& p1, const point_intensity& p2)
{ return p1.intensity < p2.intensity; }
};

vector<point_intensity>::const_iterator loc = max_element(v.begin(),
v.end(), compare_intensity());
 
J

James Kanze

I have some data that I am using a vector<vector<double> > container
to hold. The data is n sets of (x,y,z,intensity) data points. I can
either group my data like this:
vector<vector<double> > v(n, vector<double>(4)); // method A
or like this:
vector<vector<double> > v(4, vector<double>(n)); // method B
This difference, of course, is that in the first method, I have n 4-
element vectors and in the second I have 4 n-element vectors.

Off hand, something like:

struct DataPoint
{
double x ;
double y ;
double z ;
// or maybe: double pos[3] ;
// instead of x, y and z.
double intensity ;
} ;

std::vector< DataPoint > v( n ) ;

would seem more natural. If you can name the individual
elements in a semantically significant manner, it's usually best
to do so.

For small arrays which *must* have a fixed size for the program
to work, C style arrays are often preferable to std::vector.
But I would never use them for this outside of a wrapping struct
or class, since they don't have correct value semantics (which
you definitely need here, and would logically expect elsewhere).
The fact that intensity has a very distinct semantic from the
other elements (which I suppose are a position) means that it
should in no case be an element of the array, however.
Next, I want to find the (x,y,z) coordinates of the point that has the
highest intensity. Finding the highest intensity is easy when I use
method B to group the data.
All I have to do is:
vector<double>::iterator maxInt = max_element(v[3].begin(),
v[3].end());
The max intensity is found by dereferencing maxInt. However, at this
point I'm at a loss as to how to get the corresponding (x,y,z) values
for that max intensity.
Alternatively, if I were to group the data using method A, once I have
a max intensity, it's trivial to find the (x,y,z) values, but I don't
know a good (i.e. easy, elegant, whatever) way to find the max
intensity short of writing a max_element-like function.

What's wrong with the above structure, using something like:

struct CompareIntensities
{
bool operator()(
DataPoint const& lhs,
DataPoint const& rhs ) const
{
return lhs.intensity < rhs.intensity ;
}
} ;

then

std::vector< DataPoint >::iterator
maxInt
= std::max_element( v.begin(), v.end(),
CompareIntensities() ) ;

If you never compare anything but the intensities, you can also
define an operator< for DataPoint which only considers the
intensities. (I rather prefer the named comparison, however, as
it also documents exactly which criterion is being used for the
comparison.)
 
T

T. Crane

I have some data that I am using a vector<vector<double> > container
to hold. The data is n sets of (x,y,z,intensity) data points. I can
either group my data like this:
vector<vector<double> > v(n, vector<double>(4)); // method A
or like this:
vector<vector<double> > v(4, vector<double>(n)); // method B
This difference, of course, is that in the first method, I have n 4-
element vectors and in the second I have 4 n-element vectors.

Off hand, something like:

struct DataPoint
{
double x ;
double y ;
double z ;
// or maybe: double pos[3] ;
// instead of x, y and z.
double intensity ;
} ;

std::vector< DataPoint > v( n ) ;

would seem more natural. If you can name the individual
elements in a semantically significant manner, it's usually best
to do so.

For small arrays which *must* have a fixed size for the program
to work, C style arrays are often preferable to std::vector.
But I would never use them for this outside of a wrapping struct
or class, since they don't have correct value semantics (which
you definitely need here, and would logically expect elsewhere).
The fact that intensity has a very distinct semantic from the
other elements (which I suppose are a position) means that it
should in no case be an element of the array, however.
Next, I want to find the (x,y,z) coordinates of the point that has the
highest intensity. Finding the highest intensity is easy when I use
method B to group the data.
All I have to do is:
vector<double>::iterator maxInt = max_element(v[3].begin(),
v[3].end());
The max intensity is found by dereferencing maxInt. However, at this
point I'm at a loss as to how to get the corresponding (x,y,z) values
for that max intensity.
Alternatively, if I were to group the data using method A, once I have
a max intensity, it's trivial to find the (x,y,z) values, but I don't
know a good (i.e. easy, elegant, whatever) way to find the max
intensity short of writing a max_element-like function.

What's wrong with the above structure, using something like:

struct CompareIntensities
{
bool operator()(
DataPoint const& lhs,
DataPoint const& rhs ) const
{
return lhs.intensity < rhs.intensity ;
}
} ;

then

std::vector< DataPoint >::iterator
maxInt
= std::max_element( v.begin(), v.end(),
CompareIntensities() ) ;

If you never compare anything but the intensities, you can also
define an operator< for DataPoint which only considers the
intensities. (I rather prefer the named comparison, however, as
it also documents exactly which criterion is being used for the
comparison.)

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Well, as the consensus is to define the "data point" as a structure
and use a comparison function, I think I'll go with that. This does
tidy things up a bit.

thanks for everyone's comments/suggestions,
trevis
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top