c++ - What is the C# equivalent to std::vector::iterators? -
the following code snippet shows iterator
std::vector
c++ lib. c# equivalent this? can loop through each item in vector considering not linked list? iterator doing here , how can equivalent in c#? full code here.
std::vector<kmeancluster>::iterator closest_cluster = clusters.begin(); // figure out cluster color closest in rgb space. (std::vector<kmeancluster>::iterator cluster = clusters.begin(); cluster != clusters.end(); ++cluster) { uint distance_sqr = cluster->getdistancesqr(r, g, b); if (distance_sqr < distance_sqr_to_closest_cluster) { distance_sqr_to_closest_cluster = distance_sqr; closest_cluster = cluster; } }
c++ standard library iterators defined in way resemble pointers walk through collection. in c# every collection implements ienumerable
can iterated in foreach
loop. apart can still similar c++ iterators in c# using enumerator
s (which makes things harder in cases):
ienumerable<int> mycollection = new list<int> { 1, 2, 3 }; var enumerator = mycollection.getenumerator(); while(enumerator.movenext()) console.writeline(enumerator.current);
actually above how foreach
loop iterates through collection under hood.
foreach(int num in mycollection) console.writeline(num);
so in terms of code exact (but hard code , understand) equivalent:
ienumerator<kmeancluster> closest_cluster = clusters.getenumerator(); while (closest_cluster.movenext()) { uint distance_sqr = closest_cluster.current.getdistancesqr(r, g, b); if (distance_sqr < distance_sqr_to_closest_cluster) { distance_sqr_to_closest_cluster = distance_sqr; closest_cluster = cluster; } }
and easiest equivalent:
foreach(kmeancluster closest_cluster in clusters) { uint distance_sqr = closest_cluster.getdistancesqr(r, g, b); if (distance_sqr < distance_sqr_to_closest_cluster) { distance_sqr_to_closest_cluster = distance_sqr; closest_cluster = cluster; } }
Comments
Post a Comment