mercredi 25 mai 2016

Removing specific chunks from std::vector using a lambda function

I have a vector composed by several vectors of the same size and i want to get rid of part of their end using remove if and a lambda function but there seems to be a displacement in the removal. Here is the code that I am running:

#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <typeinfo>


int main()
{
   int sizeOfSpectrum=10;
   int desiredSizeOfSpectrum=8;
   int count=-1;
   std::vector<int> v(100);
   std::iota (std::begin(v), std::end(v), 0);
   auto newend=std::remove_if(v.begin(),v.end(),[&](int i)->bool{
                   count++;
                   int temp=count%sizeOfSpectrum;
                   bool test=(temp) > desiredSizeOfSpectrum;
                   return ( test);
               });
   v.erase(newend, v.end());
   std::cout << "\n";
   std::cout << "v[i]: \n";
   for(int i : v)std::cout <<v[i]<<": ";
   std::cout << "\n";
   return 0;
}

Aucun commentaire:

Enregistrer un commentaire