Is there a stylistically preferred way to apply an algorithm to a vector of
vectors?
For example, suppose I have:
typedef vector< vector<int Array2D;
Array2D array2D;
I want to find the top-level element who's 2nd element is the maximum
(think searching on column 2). The obvious algorithm is max_element, but do
I use a custom iterator or a custom predicate?
Should I define a predicate like LessThan2ndElement<Tor an interator
adaptor that converts Array2D::iterator to an interator that, when
dereferenced, returns the 2nd element of the vector<intit points to? 5 2056
Kenneth Porter wrote:
Is there a stylistically preferred way to apply an algorithm to a vector of
vectors?
For example, suppose I have:
typedef vector< vector<int Array2D;
Array2D array2D;
I want to find the top-level element who's 2nd element is the maximum
(think searching on column 2). The obvious algorithm is max_element, but do
I use a custom iterator or a custom predicate?
Should I define a predicate like LessThan2ndElement<Tor an interator
adaptor that converts Array2D::iterator to an interator that, when
dereferenced, returns the 2nd element of the vector<intit points to?
In this case, I'd rather hand write a double loop to iterate
--
Best Regards
Barry
On May 27, 10:07 pm, Kenneth Porter <shiva.blackl...@sewingwitch.com>
wrote:
For example, suppose I have:
typedef vector< vector<int Array2D;
Array2D array2D;
I want to find the top-level element who's 2nd element is the maximum
(think searching on column 2). The obvious algorithm is max_element, but do
I use a custom iterator or a custom predicate?
I agree with Barry, something like (assuming your array size is at
least [1][2]):
int maxv = Array2D[0][1], maxi = 0;
for (int i = 1; i < Array2D.size(); ++ i)
if (Array2D[i][1] maxv) {
maxv = Array2D[i][1];
maxi = i;
}
cout << "max value " << maxv << " at " << maxi << endl;
It's simple, efficient, and clear.
Jason
I went with a custom predicate:
// compares two vectors on the nth element
template <class V, unsigned index>
class VectorElementLess : std::less<V>
{
public:
bool operator()(const V& _Left, const V& _Right) const
{
return std::less<V::value_type>()(_Left[index], _Right[index]);
}
};
typedef std::vector<std::vector<int Array2D;
Array2D samples; // passed in parameter, known to be non-empty
const unsigned sensorIndex = 1;
typedef VectorElementLess<Array2D::value_type, sensorIndexSensorLess;
// find the sample with the maximum sensor reading
std::vector<intmaxSensor = *std::max_element(samples.begin(), samples.end
(), SensorLess());
Kenneth Porter wrote:
I went with a custom predicate:
// compares two vectors on the nth element
template <class V, unsigned index>
class VectorElementLess : std::less<V>
{
public:
bool operator()(const V& _Left, const V& _Right) const
{
return std::less<V::value_type>()(_Left[index], _Right[index]);
Is there any magic using std::less<here?
why not just "return _Left[index] < _Right[index];" ?
name starting with underscore followed by a capital letter is
reserved for implementation usage.
}
};
why inherit from std::less? which introduces a hiding of operator() in
std::less
maybe std::binary_function<V, V, boolis better.
>
typedef std::vector<std::vector<int Array2D;
Array2D samples; // passed in parameter, known to be non-empty
const unsigned sensorIndex = 1;
typedef VectorElementLess<Array2D::value_type, sensorIndexSensorLess;
// find the sample with the maximum sensor reading
std::vector<intmaxSensor = *std::max_element(samples.begin(), samples.end
(), SensorLess());
maybe keep using an iterator as the index found is better
--
Best Regards
Barry
Barry <dh*****@gmail.comwrote in news:g1**********@news.cn99.com:
Is there any magic using std::less<here?
why not just "return _Left[index] < _Right[index];" ?
No, good catch. That would save a template expansion, since at base it's
going to look for operator< to be defined anyway.
name starting with underscore followed by a capital letter is
reserved for implementation usage.
Side effect of copying the compiler's std::less body to base from. I'll fix
that.
why inherit from std::less? which introduces a hiding of operator() in
std::less
maybe std::binary_function<V, V, boolis better.
Just to get the typedefs, but there's no strong reason to use
binary_function versus less, and I figured less might better document
intent. If the standard later extended less, this should allow this class
to inherit the extension. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Scott Smedley |
last post by:
Hi all,
I'm trying to write a special adaptor iterator for my program. I have
*almost* succeeded, though it fails under some circumstances.
See the for-loop in main().
Any pointers/help...
|
by: Prawit Chaivong |
last post by:
Hi, gurus
Is it safe to do this in function?
'return &(*iterator)';
And iterator is std::set<something>::iterator
Regards,
|
by: nick |
last post by:
Hi,
I need to manage a "layered" collection of objects, where each layer
grows independently, e.g,
o--+--+--+--+--+ 1st layer
|
o 2nd layer (empty)
|
o--+--+--+ 3rd...
|
by: eric |
last post by:
Hi,
I would like to build the inParams SQLJ sample procedure from the
Development Center.
if I declare a #sql public iterator SpIterat, the Development Center
complains this should be in a...
|
by: Mateusz Ćoskot |
last post by:
Hi,
I know iterator categories as presented
by many authors: Stroustrup, Josuttis and Koenig&Moo:
Input <---|
|<--- Forward <--- Bidirectional <--- Random
Output <---|
| |
by: samuel.y.l.cheung |
last post by:
Hi,
I am trying to convert a Java iterator loop to C++ STL? the loop looks
like this:
public static boolean func (List aList) {
int minX = 0
for (Iterator iter = aList.listIterator(1);...
|
by: dkmd_nielsen |
last post by:
I have two rather simple class methods coded in Ruby...my own each
iterator: The iterator is used internally within the class/namespace,
and be available externally. That way I can keep...
|
by: Max Odendahl |
last post by:
Hi,
my own declared class has a stl::list as a member variable. I now need
access to those from the outside. Is a custom iterator for my class the best
solution for this and how to do this?
I...
|
by: jehugaleahsa |
last post by:
Hello:
Yesterday, I encountered an interesting side-effect of iterators. Even
though the code looked fine, it actually was completely wrong. See if
you can figure out why this code doesn't do as...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |