Re: some combination of std::distance/std::max_element ( ? )
Carl Barron wrote:
[.....]
Where For1 and For2 are forward iterators [faster if For1 is
actually a random access iterator] and Out is an output iterator.
and For1::value_type has an operator < .
This will work with any STL sequence containers.
Awesome. Often desirable for what I'm doing.
std::vector<int> sizes{3,3};
std::vector<double> data;
// place the six values in data.
std::vector<double> max_of_zone;
max_zones(data.begin(),data.end(),sizes.begin(),sizes.end(),
std::ostream_iterator<double>(std::cout,",'));
will print the max's as a comma separated list.
Isn't that shorter and easier to read??
Incredible.
How would my approach below rank in the shorter and easier to read
category?
At issue:
Compare the keys @ element and element+ 1 in a map against some
threshold. If condition is met. Compare the values. Remove the
lower..
typedef std::map< int , double > MMAP;
void run_it ( MMAP& m, int num )
{
MMAP::iterator it1 = m.begin();
MMAP::iterator it2 = m.begin();
if (it2 != m.end()) ++it2;
while (it1 != m.end() && it2 != m.end())
{
bool success ( false );
// if difference between Key and Key + 1 < num
//std::cout << it1->first << " " ;
//std::cout << it2->first << std::endl;
if ( ( it2->first - it1->first ) < num )
{
if ( it1->second < it2->second )
{
m.erase(it1);
it1 = ++it2;
}
else
{
m.erase(it2++);
it1 = it2;
if (it2 != m.end()) ++it2;
}
success = true;
}
// increment -
if (it2 != m.end()) ++it2;
if (!success )
{
++it1;
}
}
}
int main()
{
MMAP mp;
mp.insert( std::make_pair ( 0 , 3.2 ) );
mp.insert( std::make_pair ( 1 , 9.2 ) );
mp.insert( std::make_pair ( 3 , 2.8 ) );
mp.insert( std::make_pair ( 4 , 4.2 ) );
int num ( 3 ) ;
run_it ( mp, num ) ;
MMAP::iterator end = mp.end();
for ( MMAP::iterator it = mp.begin(); it != end; ++it )
std::cout << " ( " << it->first << ", "
<< it->second << " ) "
<< std::endl;
}
Curious to see ( more importantly learn ) how you'd approach this.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]