Last active
August 29, 2015 14:11
-
-
Save TAK-EMI/bedeac3e1e799149fc74 to your computer and use it in GitHub Desktop.
C#のEnumeratorを、C++のstd::vectorなんかのイテレータっぽく使うサンプル。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<int> list = new List<int> { 0, 1, 2, 3, 4 }; | |
foreach(int i in list) | |
System.Console.Write(i + ", "); | |
System.Console.WriteLine(""); | |
int[] array = list.ToArray(); | |
foreach(int i in array) | |
System.Console.Write(i + ", "); | |
System.Console.WriteLine(""); | |
var e = list.GetEnumerator(); | |
for(int i = 0; i < 4; ++i) | |
e.MoveNext(); | |
List<int>.Enumerator enu = e; | |
System.Console.WriteLine(e.Current); | |
System.Console.WriteLine(enu.Current); | |
//list.Remove(0); | |
list.RemoveAt(0); | |
foreach(int i in list) | |
System.Console.Write(i + ", "); | |
System.Console.WriteLine(""); | |
System.Console.WriteLine(e.Current); | |
System.Console.WriteLine(enu.Current); |
あ、仰るとおりですね!
list.RemoveAt(0);が想定したものです。
ご指摘ありがとうございます!
修正しておきますね。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
少し気になったのですが、
List list = new List { 0, 1, 2, 3, 4 }; を
List list = new List { 4, 3, 2, 1, 0 };
とした場合
list.Remove(0); の挙動は望んだ挙動でしょうか?
list.RemoveAt(0);だったりしませんか?