<small id='4BeXh'></small><noframes id='4BeXh'>

<legend id='4BeXh'><style id='4BeXh'><dir id='4BeXh'><q id='4BeXh'></q></dir></style></legend>
      <bdo id='4BeXh'></bdo><ul id='4BeXh'></ul>

    <i id='4BeXh'><tr id='4BeXh'><dt id='4BeXh'><q id='4BeXh'><span id='4BeXh'><b id='4BeXh'><form id='4BeXh'><ins id='4BeXh'></ins><ul id='4BeXh'></ul><sub id='4BeXh'></sub></form><legend id='4BeXh'></legend><bdo id='4BeXh'><pre id='4BeXh'><center id='4BeXh'></center></pre></bdo></b><th id='4BeXh'></th></span></q></dt></tr></i><div id='4BeXh'><tfoot id='4BeXh'></tfoot><dl id='4BeXh'><fieldset id='4BeXh'></fieldset></dl></div>
    <tfoot id='4BeXh'></tfoot>

      C# 或滑动窗口枚举器中的成对迭代

      时间:2023-08-28
    1. <tfoot id='DYjns'></tfoot>

        1. <small id='DYjns'></small><noframes id='DYjns'>

          <i id='DYjns'><tr id='DYjns'><dt id='DYjns'><q id='DYjns'><span id='DYjns'><b id='DYjns'><form id='DYjns'><ins id='DYjns'></ins><ul id='DYjns'></ul><sub id='DYjns'></sub></form><legend id='DYjns'></legend><bdo id='DYjns'><pre id='DYjns'><center id='DYjns'></center></pre></bdo></b><th id='DYjns'></th></span></q></dt></tr></i><div id='DYjns'><tfoot id='DYjns'></tfoot><dl id='DYjns'><fieldset id='DYjns'></fieldset></dl></div>

              • <bdo id='DYjns'></bdo><ul id='DYjns'></ul>
                  <tbody id='DYjns'></tbody>

                <legend id='DYjns'><style id='DYjns'><dir id='DYjns'><q id='DYjns'></q></dir></style></legend>
              • 本文介绍了C# 或滑动窗口枚举器中的成对迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                如果我有一个像这样的 IEnumerable:

                If I have an IEnumerable like:

                string[] items = new string[] { "a", "b", "c", "d" };
                

                我想循环遍历所有成对的连续项目(大小为 2 的滑动窗口).会是

                I would like to loop thru all the pairs of consecutive items (sliding window of size 2). Which would be

                ("a","b"), ("b", "c"), ("c", "d")
                

                我的解决方案是这样的

                    public static IEnumerable<Pair<T, T>> Pairs(IEnumerable<T> enumerable) {
                        IEnumerator<T> e = enumerable.GetEnumerator(); e.MoveNext();
                        T current = e.Current;
                        while ( e.MoveNext() ) {
                            T next = e.Current;
                            yield return new Pair<T, T>(current, next);
                            current = next;
                        }
                    }
                
                 // used like this :
                 foreach (Pair<String,String> pair in IterTools<String>.Pairs(items)) {
                    System.Out.PrintLine("{0}, {1}", pair.First, pair.Second)
                 }
                

                当我编写这段代码时,我想知道 .NET 框架中是否已经有函数可以做同样的事情,而且不仅适用于对,而且适用于任何大小的元组.恕我直言,应该有一种很好的方法来执行这种滑动窗口操作.

                When I wrote this code, I wondered if there are already functions in the .NET framework that do the same thing and do it not just for pairs but for any size tuples. IMHO there should be a nice way to do this kind of sliding window operations.

                我使用 C# 2.0,我可以想象使用 C# 3.0(w/LINQ)有更多(更好)的方法可以做到这一点,但我主要对 C# 2.0 解决方案感兴趣.不过,我也会欣赏 C# 3.0 解决方案.

                I use C# 2.0 and I can imagine that with C# 3.0 (w/ LINQ) there are more (and nicer) ways to do this, but I'm primarily interested in C# 2.0 solutions. Though, I will also appreciate C# 3.0 solutions.

                推荐答案

                在 .NET 4 中这变得更加容易:-

                In .NET 4 this becomes even easier:-

                var input = new[] { "a", "b", "c", "d", "e", "f" };
                var result = input.Zip(input.Skip(1), (a, b) => Tuple.Create(a, b));
                

                这篇关于C# 或滑动窗口枚举器中的成对迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 lambda 表达式中使用 foreach 循环的迭代器变量 下一篇:yield 语句实现

                相关文章

                最新文章

              • <i id='fAZdX'><tr id='fAZdX'><dt id='fAZdX'><q id='fAZdX'><span id='fAZdX'><b id='fAZdX'><form id='fAZdX'><ins id='fAZdX'></ins><ul id='fAZdX'></ul><sub id='fAZdX'></sub></form><legend id='fAZdX'></legend><bdo id='fAZdX'><pre id='fAZdX'><center id='fAZdX'></center></pre></bdo></b><th id='fAZdX'></th></span></q></dt></tr></i><div id='fAZdX'><tfoot id='fAZdX'></tfoot><dl id='fAZdX'><fieldset id='fAZdX'></fieldset></dl></div>

                1. <small id='fAZdX'></small><noframes id='fAZdX'>

                    <legend id='fAZdX'><style id='fAZdX'><dir id='fAZdX'><q id='fAZdX'></q></dir></style></legend>
                  1. <tfoot id='fAZdX'></tfoot>

                    • <bdo id='fAZdX'></bdo><ul id='fAZdX'></ul>