What are practical uses for STL's 'partial_sum'?

前端 未结 11 1702
耶瑟儿~
耶瑟儿~ 2021-02-04 03:47

What/where are the practical uses of the partial_sum algorithm in STL?

What are some other interesting/non-trivial examples or use-cases?

11条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 04:28

    Personal Use Case: intermediate step in counting sort from CLRS:

    COUNTING_SORT (A, B, k)
    
    for i ← 1 to k do
        c[i] ← 0
    for j ← 1 to n do
        c[A[j]] ← c[A[j]] + 1
    //c[i] now contains the number of elements equal to i
    
    
    // std::partial_sum here
    for i ← 2 to k do
        c[i] ← c[i] + c[i-1]
    
    
    // c[i] now contains the number of elements ≤ i
    for j ← n downto 1 do
        B[c[A[i]]] ← A[j]
        c[A[i]] ← c[A[j]] - 1
    

提交回复
热议问题