Monday, February 26, 2018

Digest for comp.lang.c++@googlegroups.com - 7 updates in 2 topics

alessio211734 <alessio211734@yahoo.it>: Feb 26 12:28PM -0800

Hello,
many time happen to do a series of similar algorithms where loop through array with adjacent iterator. Just a example to better understand
 
std::vector<float> v;
auto l=v.begin();
auto c=v.begin();
c++;
while (c!=v.end())
{
// here I do something with (*l) and (*c)
l++;
c++
}
 
how can I avoid to do a sort of copy and paste on my code when should
loop through array with adjacent iterator and apply a generic algoritm on the value of this iterator?
I think something with template but I don't known how.
Daniel <danielaparker@gmail.com>: Feb 26 01:16PM -0800

On Monday, February 26, 2018 at 3:29:25 PM UTC-5, alessio211734 wrote:
 
> }
 
> how can I avoid to do a sort of copy and paste on my code when should
> loop through array with adjacent iterator and apply a generic algoritm on > the value of this iterator?
 
Perhaps something like
 
#include <vector>
#include <iostream>
 
template <class Iterator,class F>
void process(Iterator l, Iterator end, F f)
{
if (l != end)
{
auto c = l + 1;
while (c != end)
{
f(c, l);
c++;
l++;
}
}
}
 
int main()
{
std::vector<float> v = {1,2,3};
 
auto f = [](std::vector<float>::iterator c,
std::vector<float>::iterator l)
{
std::cout << "do something with " << *c << " and " << *l <<
std::endl;
};
 
process(v.begin(), v.end(), f);
}
Daniel <danielaparker@gmail.com>: Feb 26 01:22PM -0800

On Monday, February 26, 2018 at 4:16:39 PM UTC-5, Daniel wrote:
 
> Perhaps something like
 
or
 
#include <vector>
#include <iostream>
 
template <class Iterator,class F>
void process(Iterator l, Iterator end, F f)
{
if (l != end)
{
auto c = l + 1;
while (c != end)
{
f(*c, *l);
c++;
l++;
}
}
}
 
int main()
{
std::vector<float> v = {1,2,3};
 
auto f = [](float c, float l)
{std::cout << "do something with " << c << " and " << l << std::endl; };
process(v.begin(), v.end(), f);
}
Paavo Helde <myfirstname@osa.pri.ee>: Feb 26 11:47PM +0200

On 26.02.2018 22:28, alessio211734 wrote:
 
> how can I avoid to do a sort of copy and paste on my code when should
> loop through array with adjacent iterator and apply a generic algoritm on the value of this iterator?
> I think something with template but I don't known how.
 
My attempt without lambdas:
 
#include <iostream>
#include <vector>
#include <numeric>
#include <cassert>
 
template<class CONT>
class IterPair {
public:
using T = typename CONT::value_type;
using ITER = typename CONT::const_iterator;
 
IterPair(ITER it1, ITER it2) : it1_(it1), it2_(it2) {
}
std::pair<const T&, const T&> operator*() const {
return std::pair<const T&, const T&>(*it1_, *it2_);
}
bool operator!=(const IterPair& b) {
return it1_ != b.it1_ && it2_ != b.it2_;
}
void operator++() {
++it1_;
++it2_;
}
private:
ITER it1_, it2_;
};
 
template<class CONT>
class AdjacentIterWrapper {
public:
using const_iterator = IterPair<CONT>;
AdjacentIterWrapper(const CONT& c) : c_(c) {
assert(!c.empty());
}
const_iterator begin() {
return const_iterator(c_.begin(), c_.begin()+1);
}
const_iterator end() {
return const_iterator(c_.end(), c_.end());
}
private:
const CONT& c_;
};
 
template<class CONT>
AdjacentIterWrapper<CONT> IterateAdjacent(const CONT& c) {
return AdjacentIterWrapper<CONT>(c);
}
 
int main() {
std::vector<float> v(100);
std::iota(v.begin(), v.end(), 0.0f);
 
for (auto x: IterateAdjacent(v)) {
// here I do something with (*l) and (*c)
std::cout << "do something with " << x.first << " and " << x.second <<
"\n";
}
}
"Öö Tiib" <ootiib@hot.ee>: Feb 26 01:49PM -0800

On Monday, 26 February 2018 22:29:25 UTC+2, alessio211734 wrote:
 
> how can I avoid to do a sort of copy and paste on my code when should
> loop through array with adjacent iterator and apply a generic algoritm on the value of this iterator?
> I think something with template but I don't known how.
 
Vector has random access iterators so with those you can do
arithmetic and also since C++11 those have dereference operator [].
 
if (v.empty())
{
abort(); // or whatever is appropriate by your program logic
}
for (auto it = v.begin()+1; it!=v.end(); ++it)
{
// here do something with your it[-1] and it[0]
}
Daniel <danielaparker@gmail.com>: Feb 26 02:13PM -0800

On Monday, February 26, 2018 at 4:50:11 PM UTC-5, Öö Tiib wrote:
> {
> // here do something with your it[-1] and it[0]
> }
 
Hard to beat that :-)
Vir Campestris <vir.campestris@invalid.invalid>: Feb 26 09:56PM

On 23/02/2018 22:27, Alf P. Steinbach wrote:
 
> You can wrap it in a class that doesn't have default constructor.
 
> Static check better than dynamic. ;-)
 
That might do it. I'll have a play next time I have some free time.
 
Fixing core bugs in shared code isn't my real job - I'm working on a
specific product - but I do seem to have found a few over the months
I've been here.
 
Thanks Alft & Öö.
 
Andy
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: