Skip to content

Instantly share code, notes, and snippets.

@TAK-EMI
Last active August 29, 2015 14:11
Show Gist options
  • Save TAK-EMI/bedeac3e1e799149fc74 to your computer and use it in GitHub Desktop.
Save TAK-EMI/bedeac3e1e799149fc74 to your computer and use it in GitHub Desktop.
C#のEnumeratorを、C++のstd::vectorなんかのイテレータっぽく使うサンプル。
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);
@harujoh
Copy link

harujoh commented Dec 17, 2014

少し気になったのですが、
List list = new List { 0, 1, 2, 3, 4 }; を
List list = new List { 4, 3, 2, 1, 0 };
とした場合
list.Remove(0); の挙動は望んだ挙動でしょうか?
list.RemoveAt(0);だったりしませんか?

@TAK-EMI
Copy link
Author

TAK-EMI commented Dec 21, 2014

あ、仰るとおりですね!
list.RemoveAt(0);が想定したものです。
ご指摘ありがとうございます!
修正しておきますね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment