简体   繁体   English

将用 C++ 编写的程序代码迁移到 C# - 相当于 `set<ii> ::迭代器?</ii>

[英]Migrating program code written in C++ to C# - What's the equivalent of `set<ii> ::iterator`?

I have a program written in C++ that process To place students to departments in order of preference and by their scores.我有一个用 C++ 编写的程序,该程序按优先顺序和分数将学生分配到部门。 Then I want to migrate this code to C# language.然后我想将此代码迁移到 C# 语言。 I converted all but the PlaceStudents() function is not complete.我转换了除了PlaceStudents() function 之外的所有内容。 How can I find equivalent of set<ii>::iterator in C#?如何在 C# 中找到等效的set<ii>::iterator

using namespace std;
typedef pair<int, int>   ii;

const int studentCount = 200, departmentCount = 40, preferenceCount = 24, scoreKind = 18;

struct Student {
    string name;
    int order[scoreKind]; // order[i] => student i. scoreKind order (order of student in score kind)
    int preference[preferenceCount]; // student's preference list
    int applicationCount; // student's application count
} student[studentCount + 7];

struct Department {
    string name;
    int quota; // acceptable max student count by Department
    int scoreKind; // accepted score kind by Department
    set<ii> S; // temporary accepted student by Department. (order, index) of student
} department[departmentCount + 7];

void PlaceStudents() {
    queue<int> Q; // students queue
    fori(i, 0, studentCount) {
        Q.push(i); // in the begining add all students to queue
    }
    while (!Q.empty()) {
        int id = Q.front();
        Q.pop();
        Student *a = &student[id]; // kuyrugun onundeki ogrenci
        Department *b = &department[a->preference[a->applicationCount]]; // department that next preference order of this student 
        b->S.insert({ a->order[b->scoreKind],id }); // add student to department
        if (b->S.size() > b->quota) { // If student count more than the quota 
            set<ii> ::iterator it = b->S.end();
            it--; // last element of set
            int x = it->second; // index of worst scored student 
            b->S.erase(it); // delete worst scored student
            student[x].applicationCount++; // increment students applicationCount
            if (student[x].applicationCount < preference) // if student is not rejected from all preferences
                Q.push(x); // add student to queue for next proses
        }
    }
}

Converted PlaceStudents Function to C# is belowPlaceStudents Function 转换为 C# 如下

public static void PlaceStudents()
{
    Queue<int> Q = new Queue<int>(); // students queue
    for (int i = 0; i < studentCount; ++i)
    {
        Q.Enqueue(i); // in the begining add all students to queue
    }
    while (Q.Count > 0)
    {
        int id = Q.Peek();
        Q.Dequeue();
        Student a = student[id]; // the student in front of the queue
        Department b = department[a.preference[a.applicationCount]]; // department that next preference order of this student
        b.S.Add(new Tuple<int, int>(a.order[b.scoreKind], id)); // add student to department
        if (b.S.Count > b.quota)
        { // If student count more than the quota
            SortedSet<Tuple<int, int>> iterator it = b.S.end(); // I can't convert from here  until end.
            it--; // last element of set
            int x = it.second; // index of worst scored student
            b.S.Remove(it); // delete worst scored student
            student[x].applicationCount++; // increment students applicationCount
            if (student[x].applicationCount < preference) // if student is not rejected from all preferences
            {
                Q.Enqueue(x); // add student to queue for next loop
            }
        }
    }
}

I do not think you should be that literal when converting code我认为您在转换代码时不应该是那种文字

        set<ii> ::iterator it = b->S.end();
        it--; // last element of set
        int x = it->second; // index of worst scored student 
        b.S.Remove(it); // delete worst scored student

The goal of all this is apparently to extract and remove the last element of the list.所有这一切的目标显然是提取和删除列表的最后一个元素。 The equivalent to iterators in.Net is IEnumerator , but this is a bit simpler and more restrictive than c++ iterators.与.Net 中的迭代器等效的是IEnumerator ,但这比 c++ 迭代器更简单且更具限制性。 The closest equivalent would use Linq to get the last item:最接近的等效项将使用 Linq 来获取最后一项:

 var item = b.S.Last(); // Get last item, or throws if empty
 var b.S.Remove(item); // remove item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM