Advertisement
jack06215

[c++] Remove vector by list of index

Jul 7th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. //std::vector<int> myList = {1, 44, 66, 88, 77, 553};
  2. //std::vector<int> removeIndex = {0, 1, 3, 4};
  3. //myList.erase(toggleIndex(myList, removeIn
  4.  
  5. std::vector<int>::iterator toggleIndex(std::vector<int> &src, std::vector<int> &position)
  6. {
  7.     position.push_back(-1);                         // to handle range from 0 to the first index to remove
  8.     position.push_back(src.size());                 // to handle range from the last index to remove and to the end of values
  9.     std::sort(position.begin(), position.end());
  10.     std::vector<int>::iterator last = src.begin();
  11.     for (size_t i = 1; i != position.size(); ++i)
  12.     {
  13.         size_t range_begin = position[i - 1] + 1;
  14.         size_t range_end = position[i];
  15.         //std::cout << range_begin << "\t" << range_end << "\t" << range_end - range_begin << std::endl;
  16.         std::copy(src.begin() + range_begin, src.begin() + range_end, last);
  17.         last += range_end - range_begin;
  18.         std::cout << *last << std::endl;
  19.     }
  20.     return last;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement