Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中的std::partial#u和等价物是什么? < C >中,C++的最佳等价物是什么?_C#_C++_Std - Fatal编程技术网

C# C中的std::partial#u和等价物是什么? < C >中,C++的最佳等价物是什么?

C# C中的std::partial#u和等价物是什么? < C >中,C++的最佳等价物是什么?,c#,c++,std,C#,C++,Std,试试: int[] values = new[] { 1, 2, 3, 4 }; var result = values.Select ((x, index) => values.Take (index + 1).Sum ()); 结果:1,3,6,10 但如果您关心性能,最好编写自己的方法 编辑: public static IEnumerable<T> MyAggregate<T> (this IEnumerable<T> items, Func

试试:

int[] values = new[] { 1, 2, 3, 4 };

var result = values.Select ((x, index) => values.Take (index + 1).Sum ());
结果:
1,3,6,10

但如果您关心性能,最好编写自己的方法

编辑:

public static IEnumerable<T> MyAggregate<T> (this IEnumerable<T> items, Func<T, T, T> mapper)
{
    bool processingFirstElement = true;

    T accumulator = default (T);

    foreach (T item in items)
    {
        if (processingFirstElement)
        {
            processingFirstElement = false;
            accumulator = item;
        }
        else
        {
            accumulator = mapper (accumulator, item);
        }

        yield return accumulator;
    }
}

该操作似乎是组合map(又名Select)和reduce(又名Aggregate)操作的弱版本,仅限于二进制操作。我们可以做得更好

public static IEnumerable<R> MyAggregate<T, R>(
  this IEnumerable<T> items,
  R seed,
  Func<T, R, R> mapper) 
{
  R current = seed;      
  foreach(T item in items)
  {
    current = mapper(item, current);
    yield return current;
  }
}

不,请尝试LINQ
Take
Sum
在这种情况下,您与OutputIterator和InputIterator的等价物是什么?@对std库进行编码是C++的一部分,对于
std::partial\u Sum
,第一个结果元素始终是第一个源元素。但是对于每一个可能的映射器函数,都不一定有一个seed,其中
mapper(items.First(),seed)=items.First()
。因此,我认为第一个result元素需要它自己的
收益率返回。当然,在
(i,s)=>s+i
的情况下,0的种子是有效的。@Eric Lippert:为什么你需要
R
?@apocapose:
cities.MyAggregate(0,(city,sum)=>city.Population+sum)
——没有要求总和类型与总和类型相同。@Eric Lippert:OK,现在就明白:)@apocalype:或者假设您正在对null值求和,并且希望null值为零
myNullableInts.MyAggregate(0,(i,s)=>(i??0)+s)
所以现在sum类型和summand类型又不同了。
public static IEnumerable<R> MyAggregate<T, R>(
  this IEnumerable<T> items,
  R seed,
  Func<T, R, R> mapper) 
{
  R current = seed;      
  foreach(T item in items)
  {
    current = mapper(item, current);
    yield return current;
  }
}
static IEnumerable<int> PartialSum(this IEnumerable<int> items) =>
  items.MyAggregate(0, (i, s) => s + i); 
public static IEnumerable<T> PartialSum<T>(
  this IEnumerable<T> items,
  Func<T, T, T> sum)
{
  bool first = true;
  T current = default(T);
  foreach(T item in items) 
  {
    current = first ? item : sum(current, item);
    first = false;
    yield return current;
  }
}
myints.PartialSum((i, s) => i + s);