Last active
July 20, 2018 23:09
-
-
Save halflearned/d4a89ad1b28332c72037683a82d7838e to your computer and use it in GitHub Desktop.
Filling vector while skipping values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <set> | |
#include <vector> | |
void fill_skipping(std::vector<size_t>& result, | |
std::set<size_t>& skip) { | |
auto p_skip = skip.begin(); | |
auto p_result = result.begin(); | |
size_t value = 0; | |
while (p_result != result.end()) { | |
if (value != *p_skip) { | |
*p_result++ = value; | |
} else { | |
*p_skip++; | |
} | |
++value; | |
} | |
} | |
int main() { | |
size_t max = 30; | |
std::set<size_t> skip{9, 7, 5, 3, 1}; // This is ordered upon initialization | |
std::vector<size_t> result(max - skip.size()); | |
fill_skipping(result, skip); | |
std::cout << "\nValues: "; | |
for (auto&x : result) std::cout << x << " "; | |
std::cout << "\nSize: " << result.size() << "\n"; | |
// Output will be | |
// Values: 0 2 4 6 8 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
// Size: 25 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment