Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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