Skip to content

Instantly share code, notes, and snippets.

@brycelelbach
Last active June 8, 2017 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycelelbach/137f1e45b737d615134e228ec0c84f3a to your computer and use it in GitHub Desktop.
Save brycelelbach/137f1e45b737d615134e228ec0c84f3a to your computer and use it in GitHub Desktop.
#include <numeric>
#include <array>
#include <vector>
#include <string>
#include <cassert>
int main()
{
std::array<std::string, 3> ia{{ "a", "b", "c" }};
std::vector<std::string> v0;
std::exclusive_scan(ia.begin(), ia.end(), std::back_inserter(v0), std::string("_"), std::plus<>());
// 1st sum: *(output) = init;
// 2nd sum: *(output+1) = init + *first;
// 3rd sum: *(output+2) = init + *first + *(first+1);
// ...
// Nth sum: *(output+(last-first)) = init + *first + *(first+1) + ... + *(first+(last-first)-1);
assert(v0[0] == "_");
assert(v0[1] == "_a");
assert(v0[2] == "_ab");
std::vector<std::string> v1;
std::inclusive_scan(ia.begin(), ia.end(), std::back_inserter(v1), std::plus<>(), std::string("_"));
// 1st sum: *(output) = init + *first;
// 2nd sum: *(output+1) = init + *first + *(first+1);
// 3rd sum: *(output+2) = init + *first + *(first+1) + *(first+2);
// ...
// Nth sum: *(output+(last-first)) = init + *first + *(first+1) + ... + *(first+(last-first));
assert(v0[0] == "_a");
assert(v0[1] == "_ab");
assert(v0[2] == "_abc");
std::vector<std::string> v2;
std::inclusive_scan(ia.begin(), ia.end(), std::back_inserter(v2), std::plus<>());
// 1st sum: *(output) = *first;
// 2nd sum: *(output+1) = *first + *(first+1);
// 3rd sum: *(output+2) = *first + *(first+1) + *(first+2);
// ...
// Nth sum: *(output+(last-first)) = *first + *(first+1) + ... + *(first+(last-first));
assert(v0[0] == "a");
assert(v0[1] == "ab");
assert(v0[2] == "abc");
std::vector<std::string> v3;
std::exclusive_scan(ia.begin(), ia.begin(), std::back_inserter(v3), std::string("_"), std::plus<>());
assert(v3.empty());
std::vector<std::string> v4;
std::inclusive_scan(ia.begin(), ia.begin(), std::back_inserter(v4), std::plus<>(), std::string("_"));
assert(v4.empty());
std::vector<std::string> v5;
std::inclusive_scan(ia.begin(), ia.begin(), std::back_inserter(v5), std::plus<>());
assert(v5.empty());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment