Hello,

I'm trying to get the STL equal_range function to work on a data structure I've designed, but I may be trying to use the equal_range function in a non-standard way. Is there a way to get this to work?

Basically, I have a vector of vectors of structures (elements) which each contain, among other things, an array (fixed for now, later I'll make them dynamic).

Here's the element structure definition:

struct element {
  short uniqueID;
  short vf;
  short sortKey;
  short arr[3];
};

Basically, each of these structures contains important info in its "arr" array. I then copy this vector twice, and push all three vectors onto the main vector (call it masterList). Then, I sort (using STL algorithm) each of the 3 copies according to a given dimension, so that the first vector will have its elements sorted in increasing order based on arr[0], second vector based on arr[1], and so on. For a given vector, the sortKey of all the elements is set to the appropriate dimension (i.e., vector 0 has sortKey = 0 for all elements, etc.).

Now that the three vectors are sorted, I would like to use the equal_range function on a given vector. For example, I would like to know the number of elements having arr[0] equal to a short value I provide. Here's the call I'm using:

pair<vector<element>::iterator,vector<element>::iterator> bounds = equal_range(masterList[i].begin(), masterList[i].end(), value, equalRangeFunction);

along with the comparator function I have:

bool equalRangeFunction(element i, element j) {
	return (i.arr[i.sortKey] < j.arr[j.sortKey]); }

This approach does not work, of course, because the comparison value I provide is a short, while the comparator function takes as input two elements (these types need to match, no)?

So this leads to my problem. Basically, I want to use STL's equal_range function, but I'm not passing it a vector of comparable values, but rather a vector of structs which have comparable values.

Short of copying the arr[] values into a new vector, is there a way to get this to work, maybe by rewriting the comparator function? I really would like to take advantage of the STL equal_range function if possible...

Nevermind, I figured out the answer to my question :)

I needed to create a temporary element struct, and place the short value that I wanted to compare into it. Logical enough, don't know why I didn't see it before...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.