海口建站模板,手机网站源码 html5,wordpress 反代,网站建设外包公司怎么样算法库 算法库提供大量用途的函数#xff08;例如查找、排序、计数、操作#xff09;#xff0c;它们在元素范围上操作。注意范围定义为 [first, last) #xff0c;其中 last 指代要查询或修改的最后元素的后一个元素。 类似 std::accumulate#xff0c;但不依序执行
std…算法库 算法库提供大量用途的函数例如查找、排序、计数、操作它们在元素范围上操作。注意范围定义为 [first, last) 其中 last 指代要查询或修改的最后元素的后一个元素。 类似 std::accumulate但不依序执行
std::reduce
templateclass InputIt typename std::iterator_traitsInputIt::value_type reduce( InputIt first, InputIt last);(1)(C17 起)templateclass ExecutionPolicy, class ForwardIt typename std::iterator_traitsForwardIt::value_type reduce( ExecutionPolicy policy, ForwardIt first, ForwardIt last);(2)(C17 起) templateclass InputIt, class T T reduce(InputIt first, InputIt last, T init); (3)(C17 起)templateclass ExecutionPolicy, class ForwardIt, class T T reduce(ExecutionPolicy policy, ForwardIt first, ForwardIt last, T init);(4)(C17 起) templateclass InputIt, class T, class BinaryOp T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op); (5)(C17 起)templateclass ExecutionPolicy, class ForwardIt, class T, class BinaryOp T reduce(ExecutionPolicy policy, ForwardIt first, ForwardIt last, T init, BinaryOp binary_op);(6)(C17 起)
1) 同 reduce(first, last, typename std::iterator_traitsInputIt::value_type{})
3) 同 reduce(first, last, init, std::plus())
5) 在 binary_op 上以初值 init 规约范围 [first; last) 可能以未指定方式排序聚合。
2,4,6) 同 (1,3,5) 但按照 policy 执行。此重载仅若std::is_execution_policy_vstd::decay_tExecutionPolicy 为 true才参与重载决议
若 binary_op 非结合或非交换则行为非确定。
若 binary_op 修改 [first; last] 中任何元素或非法化范围中任何迭代器含尾迭代器则行为未定义。
参数
first, last-要应用算法的元素范围init-广义和的初值policy-使用的执行策略。细节见执行策略。binary_op-将以未指定顺序应用于解引用输入迭代器结果、其他 binary_op 结果及 init 上的二元函数对象 (FunctionObject) 。类型要求- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。- ForwardIt 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。- T 必须满足可移动构造 (MoveConstructible) 的要求。而且 binary_op(init, *first) 、 binary_op(*first, init) 、 binary_op(init, init) 及 binary_op(*first, *first) 必须可转换到 T 。
返回值
init 及 *first 、 *(first1) 、…… *(last-1) 在 binary_op 上的广义和
其中广义和 GSUM(op, a 1, ..., a N) 定义如下
若 N1 则为 a 1若 N 1 则为 op(GSUM(op, b 1, ..., b K), GSUM(op, b M, ..., b N)) 其中
b 1, ..., b N 可以是任何 a1, ..., aN 的排列且1 K1 M ≤ N
换言之 reduce 表现类似 std::accumulate 除了范围中的元素可能以任意顺序分组并重排。
复杂度
O(last - first) 次应用 binary_op.
异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误
若作为算法一部分调用的函数的执行抛出异常且 ExecutionPolicy 为标准策略之一则调用 std::terminate 。对于任何其他 ExecutionPolicy 行为是实现定义的。若算法无法分配内存则抛出 std::bad_alloc 。
注意
若为空则返回不修改的 init 调用示例
#include iostream
#include string
#include iterator
#include algorithm
#include functional
#include time.h
#include random
#include vector
#include cassertstruct Cell
{int x;int y;Cell() default;Cell(int a, int b): x(a), y(b) {}Cell operator (const Cell cell){x cell.x;y cell.y;return *this;}Cell operator (const Cell cell){x cell.x;y cell.y;return *this;}Cell operator *(const Cell cell){x * cell.x;y * cell.y;return *this;}Cell operator (){x 1;y 1;return *this;}bool operator (const Cell cell) const{if (x cell.x){return y cell.y;}else{return x cell.x;}}bool operator (const Cell cell) const{if (x cell.x){return y cell.y;}else{return x cell.x;}}bool operator (const Cell cell) const{return x cell.x y cell.y;}friend Cell operator(const Cell lcell, const Cell rcell){Cell cell lcell;cell.x rcell.x;cell.y rcell.y;return cell;}friend Cell operator-(const Cell lcell, const Cell rcell){Cell cell lcell;cell.x - rcell.x;cell.y - rcell.y;return cell;}friend Cell operator*(const Cell lcell, const Cell rcell){Cell cell lcell;cell.x * rcell.x;cell.y * rcell.y;return cell;}friend Cell operator/(const Cell lcell, const Cell rcell){Cell cell lcell;cell.x / rcell.x;cell.y / rcell.y;return cell;}friend Cell operator%(const Cell lcell, const Cell rcell){Cell cell lcell;cell.x % rcell.x;cell.y % rcell.y;return cell;}
};std::ostream operator(std::ostream os, const Cell cell)
{os { cell.x , cell.y };return os;
}namespace std
{
template typename InputIt, typename T, typename BinaryOperation
T reduce(InputIt first, InputIt last, T init, BinaryOperation op)
{for (; first ! last; first){init op(std::move(init), *first);}return init;
}
}int main()
{std::cout std::boolalpha;std::mt19937 g{std::random_device{}()};srand((unsigned)time(NULL));auto generate [](){int n std::rand() % 10 110;Cell cell{n, n};return cell;};//3) 构造拥有 count 个有值 value 的元素的容器。std::vectorCell vector1(8, generate());std::generate(vector1.begin(), vector1.end(), generate);std::sort(vector1.begin(), vector1.end());std::cout vector1: ;std::copy(vector1.begin(), vector1.end(), std::ostream_iteratorCell(std::cout, ));std::cout std::endl;std::vectorCell vector2(vector1.size(), generate());std::generate(vector2.begin(), vector2.end(), generate);std::sort(vector2.begin(), vector2.end());std::cout vector2: ;std::copy(vector2.begin(), vector2.end(), std::ostream_iteratorCell(std::cout, ));std::cout std::endl;for (size_t index 0; index vector1.size(); index){std::cout std::reduce(vector1.begin(), index , Cell{0, 0} std::plusCell() ): ;std::cout std::reduce(vector1.begin(), vector1.begin() index, Cell{0, 0}, std::plusCell());std::cout std::endl;}std::cout std::endl;for (size_t index 0; index vector1.size(); index){std::cout std::reduce(vector1.begin(), index , Cell{0, 0} std::minusCell() ): ;std::cout std::reduce(vector1.begin(), vector1.begin() index, Cell{0, 0}, std::minusCell());std::cout std::endl;}std::cout std::endl;for (size_t index 0; index vector2.size(); index){std::cout std::reduce(vector2.begin(), index , Cell{1, 1} std::multipliesCell() ): ;std::cout std::reduce(vector2.begin(), vector2.begin() index, Cell{1, 1}, std::multipliesCell());std::cout std::endl;}std::cout std::endl;for (size_t index 0; index vector2.size(); index){std::cout std::reduce(vector2.begin(), index , Cell{1024, 1024} std::dividesCell() ): ;std::cout std::reduce(vector2.begin(), vector2.begin() index, Cell{1024, 1024}, std::dividesCell());std::cout std::endl;}std::cout std::endl;for (size_t index 0; index vector2.size(); index){std::cout std::reduce(vector2.begin(), index , Cell{1024, 1024} std::modulusCell() ): ;std::cout std::reduce(vector2.begin(), vector2.begin() index, Cell{1024, 1024}, std::modulusCell());std::cout std::endl;}std::cout std::endl;return 0;
}输出
vector1: {111,111} {112,112} {113,113} {115,115} {116,116} {116,116} {117,117} {119,119}
vector2: {110,110} {112,112} {112,112} {114,114} {117,117} {117,117} {119,119} {119,119}
std::reduce(vector1.begin(), 0, Cell{0, 0} std::plusCell() ): {0,0}
std::reduce(vector1.begin(), 1, Cell{0, 0} std::plusCell() ): {111,111}
std::reduce(vector1.begin(), 2, Cell{0, 0} std::plusCell() ): {223,223}
std::reduce(vector1.begin(), 3, Cell{0, 0} std::plusCell() ): {336,336}
std::reduce(vector1.begin(), 4, Cell{0, 0} std::plusCell() ): {451,451}
std::reduce(vector1.begin(), 5, Cell{0, 0} std::plusCell() ): {567,567}
std::reduce(vector1.begin(), 6, Cell{0, 0} std::plusCell() ): {683,683}
std::reduce(vector1.begin(), 7, Cell{0, 0} std::plusCell() ): {800,800}std::reduce(vector1.begin(), 0, Cell{0, 0} std::minusCell() ): {0,0}
std::reduce(vector1.begin(), 1, Cell{0, 0} std::minusCell() ): {-111,-111}
std::reduce(vector1.begin(), 2, Cell{0, 0} std::minusCell() ): {-223,-223}
std::reduce(vector1.begin(), 3, Cell{0, 0} std::minusCell() ): {-336,-336}
std::reduce(vector1.begin(), 4, Cell{0, 0} std::minusCell() ): {-451,-451}
std::reduce(vector1.begin(), 5, Cell{0, 0} std::minusCell() ): {-567,-567}
std::reduce(vector1.begin(), 6, Cell{0, 0} std::minusCell() ): {-683,-683}
std::reduce(vector1.begin(), 7, Cell{0, 0} std::minusCell() ): {-800,-800}std::reduce(vector2.begin(), 0, Cell{1, 1} std::multipliesCell() ): {1,1}
std::reduce(vector2.begin(), 1, Cell{1, 1} std::multipliesCell() ): {110,110}
std::reduce(vector2.begin(), 2, Cell{1, 1} std::multipliesCell() ): {12320,12320}
std::reduce(vector2.begin(), 3, Cell{1, 1} std::multipliesCell() ): {1379840,1379840}
std::reduce(vector2.begin(), 4, Cell{1, 1} std::multipliesCell() ): {157301760,157301760}
std::reduce(vector2.begin(), 5, Cell{1, 1} std::multipliesCell() ): {1224436736,1224436736}
std::reduce(vector2.begin(), 6, Cell{1, 1} std::multipliesCell() ): {1525177344,1525177344}
std::reduce(vector2.begin(), 7, Cell{1, 1} std::multipliesCell() ): {1107477504,1107477504}std::reduce(vector2.begin(), 0, Cell{1024, 1024} std::dividesCell() ): {1024,1024}
std::reduce(vector2.begin(), 1, Cell{1024, 1024} std::dividesCell() ): {9,9}
std::reduce(vector2.begin(), 2, Cell{1024, 1024} std::dividesCell() ): {0,0}
std::reduce(vector2.begin(), 3, Cell{1024, 1024} std::dividesCell() ): {0,0}
std::reduce(vector2.begin(), 4, Cell{1024, 1024} std::dividesCell() ): {0,0}
std::reduce(vector2.begin(), 5, Cell{1024, 1024} std::dividesCell() ): {0,0}
std::reduce(vector2.begin(), 6, Cell{1024, 1024} std::dividesCell() ): {0,0}
std::reduce(vector2.begin(), 7, Cell{1024, 1024} std::dividesCell() ): {0,0}std::reduce(vector2.begin(), 0, Cell{1024, 1024} std::modulusCell() ): {1024,1024}
std::reduce(vector2.begin(), 1, Cell{1024, 1024} std::modulusCell() ): {34,34}
std::reduce(vector2.begin(), 2, Cell{1024, 1024} std::modulusCell() ): {34,34}
std::reduce(vector2.begin(), 3, Cell{1024, 1024} std::modulusCell() ): {34,34}
std::reduce(vector2.begin(), 4, Cell{1024, 1024} std::modulusCell() ): {34,34}
std::reduce(vector2.begin(), 5, Cell{1024, 1024} std::modulusCell() ): {34,34}
std::reduce(vector2.begin(), 6, Cell{1024, 1024} std::modulusCell() ): {34,34}
std::reduce(vector2.begin(), 7, Cell{1024, 1024} std::modulusCell() ): {34,34}
文章转载自: http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.ndyrb.com.gov.cn.ndyrb.com http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.rkyw.cn.gov.cn.rkyw.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.rpkg.cn.gov.cn.rpkg.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.lwgsk.cn.gov.cn.lwgsk.cn http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.nyzmm.cn.gov.cn.nyzmm.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.qnbgk.cn.gov.cn.qnbgk.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.pakistantractors.com.gov.cn.pakistantractors.com http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.txlxr.cn.gov.cn.txlxr.cn http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn http://www.morning.dycbp.cn.gov.cn.dycbp.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.dkqr.cn.gov.cn.dkqr.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.jspnx.cn.gov.cn.jspnx.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.vvdifactory.com.gov.cn.vvdifactory.com http://www.morning.rwzc.cn.gov.cn.rwzc.cn http://www.morning.ydzly.cn.gov.cn.ydzly.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.zsfooo.com.gov.cn.zsfooo.com http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.ljjph.cn.gov.cn.ljjph.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn