本文整理汇总了C++中ranges::adjacent_difference方法的典型用法代码示例。如果您正苦于以下问题:C++ ranges::adjacent_difference方法的具体用法?C++ ranges::adjacent_difference怎么用?C++ ranges::adjacent_difference使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ranges
的用法示例。
在下文中一共展示了ranges::adjacent_difference方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test
template<class InIter, class OutIter, class InSent = InIter> void test()
{
using ranges::adjacent_difference;
using ranges::make_subrange;
{ // iterator
int ia[] = {15, 10, 6, 3, 1};
int ir[] = {15, -5, -4, -3, -2};
const unsigned s = sizeof(ia) / sizeof(ia[0]);
int ib[s] = {0};
auto r = adjacent_difference(InIter(ia), InSent(ia + s), OutIter(ib));
CHECK(base(r.in) == ia + s);
CHECK(base(r.out) == ib + s);
for(unsigned i = 0; i < s; ++i)
{
CHECK(ib[i] == ir[i]);
}
}
{ // range + output iterator
int ia[] = {15, 10, 6, 3, 1};
int ir[] = {15, -5, -4, -3, -2};
const unsigned s = sizeof(ia) / sizeof(ia[0]);
int ib[s] = {0};
auto rng = make_subrange(InIter(ia), InSent(ia + s));
auto r = adjacent_difference(rng, OutIter(ib));
CHECK(base(r.in) == ia + s);
CHECK(base(r.out) == ib + s);
for(unsigned i = 0; i < s; ++i)
{
CHECK(ib[i] == ir[i]);
}
}
{ // range + output range
int ia[] = {15, 10, 6, 3, 1};
int ir[] = {15, -5, -4, -3, -2};
const unsigned s = sizeof(ia) / sizeof(ia[0]);
int ib[s] = {0};
auto rng = make_subrange(InIter(ia), InSent(ia + s));
auto orng = make_subrange(OutIter(ib), OutIter(ib + s));
auto r = adjacent_difference(rng, orng);
CHECK(base(r.in) == ia + s);
CHECK(base(r.out) == ib + s);
for(unsigned i = 0; i < s; ++i)
{
CHECK(ib[i] == ir[i]);
}
}
{
int ia[] = {15, 10, 6, 3, 1};
int ir[] = {15, 25, 16, 9, 4};
const unsigned s = sizeof(ia) / sizeof(ia[0]);
int ib[s] = {0};
auto rng = make_subrange(InIter(ia), InSent(ia + s));
auto orng = make_subrange(OutIter(ib), OutIter(ib + s));
auto r = adjacent_difference(rng, orng, std::plus<int>());
CHECK(base(r.in) == ia + s);
CHECK(base(r.out) == ib + s);
for(unsigned i = 0; i < s; ++i)
{
CHECK(ib[i] == ir[i]);
}
}
}
示例2: main
int main()
{
test<input_iterator<const int *>, input_iterator<int *>>();
test<input_iterator<const int *>, forward_iterator<int *>>();
test<input_iterator<const int *>, bidirectional_iterator<int *>>();
test<input_iterator<const int *>, random_access_iterator<int *>>();
test<input_iterator<const int *>, int *>();
test<forward_iterator<const int *>, input_iterator<int *>>();
test<forward_iterator<const int *>, forward_iterator<int *>>();
test<forward_iterator<const int *>, bidirectional_iterator<int *>>();
test<forward_iterator<const int *>, random_access_iterator<int *>>();
test<forward_iterator<const int *>, int *>();
test<bidirectional_iterator<const int *>, input_iterator<int *>>();
test<bidirectional_iterator<const int *>, forward_iterator<int *>>();
test<bidirectional_iterator<const int *>, bidirectional_iterator<int *>>();
test<bidirectional_iterator<const int *>, random_access_iterator<int *>>();
test<bidirectional_iterator<const int *>, int *>();
test<random_access_iterator<const int *>, input_iterator<int *>>();
test<random_access_iterator<const int *>, forward_iterator<int *>>();
test<random_access_iterator<const int *>, bidirectional_iterator<int *>>();
test<random_access_iterator<const int *>, random_access_iterator<int *>>();
test<random_access_iterator<const int *>, int *>();
test<const int *, input_iterator<int *>>();
test<const int *, forward_iterator<int *>>();
test<const int *, bidirectional_iterator<int *>>();
test<const int *, random_access_iterator<int *>>();
test<const int *, int *>();
using ranges::adjacent_difference;
{ // Test projections
S ia[] = {{15}, {10}, {6}, {3}, {1}};
int ir[] = {15, -5, -4, -3, -2};
const unsigned s = sizeof(ir) / sizeof(ir[0]);
int ib[s] = {0};
auto r = adjacent_difference(ranges::begin(ia), ranges::begin(ia) + s,
ranges::begin(ib), std::minus<int>(), &S::i);
CHECK(base(r.in) == ia + s);
CHECK(base(r.out) == ib + s);
for(unsigned i = 0; i < s; ++i)
{
CHECK(ib[i] == ir[i]);
}
}
{ // Test BinaryOp
int ia[] = {15, 10, 6, 3, 1};
int ir[] = {15, 25, 16, 9, 4};
const unsigned s = sizeof(ir) / sizeof(ir[0]);
int ib[s] = {0};
auto r = adjacent_difference(ia, ranges::begin(ib), std::plus<int>());
CHECK(base(r.in) == ia + s);
CHECK(base(r.out) == ib + s);
for(unsigned i = 0; i < s; ++i)
{
CHECK(ib[i] == ir[i]);
}
}
{ // Test calling it with an array
int ia[] = {15, 10, 6, 3, 1};
int ir[] = {15, 25, 16, 9, 4};
const unsigned s = sizeof(ir) / sizeof(ir[0]);
int ib[s] = {0};
auto r = adjacent_difference(ia, ib, std::plus<int>());
CHECK(base(r.in) == ia + s);
CHECK(base(r.out) == ib + s);
for(unsigned i = 0; i < s; ++i)
{
CHECK(ib[i] == ir[i]);
}
}
return ::test_result();
}