比较两个字典键值

时间:2015-10-31 11:31:11

标签: vb.net

我有两个类型为Dictionary(of Integer, string)的字典对象。

我需要检查第一个字典中的键值,即dc1是否在字典dc2中重复,如果是,则获取重复键值的键。  以下代码有效,但有更好的方法进行比较

    Dim dc1 As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
    Dim dc2 As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)
    dc1.Add(1, "student1")
    dc1.Add(2, "student2")
    dc1.Add(3, "student3")
    dc1.Add(4, "student4")
    dc2.Add(1, "student1")
    dc2.Add(2, "student4")
    dc2.Add(3, "student2")
    dc2.Add(4, "student3")
    dc2.Add(10, "student1")
    dc2.Add(11, "student4")

    Dim lstcount1 As List(Of Integer) = New List(Of Integer)
    For Each kvp1 As KeyValuePair(Of Integer, String) In dc1
        Dim count As Integer = 0
        For Each kvp2 As KeyValuePair(Of Integer, String) In dc2
            If kvp1.Value = kvp2.Value Then
                count = count + 1
                If (count > 1) Then

                    lstcount1.Add(kvp2.Key)
                End If


            End If
        Next

    Next

1 个答案:

答案 0 :(得分:0)

我不知道它是否更好",无论如何它是不同的:

#include <iostream>
#include <string>

int main() {
    std::string str("I am some code, with \"A string here\", but not here\\\". 'This sentence \" should not end yet', now it should. There is also 'a string here' too.\n");
    std::string::iterator endVal = str.end(); // a kind of NULL pointer
    std::string::iterator type = endVal;      // either " or '
    bool ignore = false; // whether to ignore the current character or not
    for (std::string::iterator it=str.begin(); it!=str.end();)
    {
        // ignore escaped characters
        if ((*it) == '\\')
        {
            it += 2;
        }
        else
        {
            if ((*it) == '"' || (*it) == '\'')
            {
                if (ignore) // within a string
                {
                    if (type != endVal && (*it) == (*type))
                    {
                        // end of the string
                        ignore = false;
                        type = endVal;
                    }
                }
                else // outside of a string, so one must be starting.
                {
                    type = it;
                    ignore = true;
                }
                it++;
                //ignore ? ignore = false : ignore = true;
                //type = it;
            }
            else
            {
                if (!ignore)
                {
                    if ((*it) == ' ' || (*it) == '\n' || (*it) == '\t')
                    {
                        it = str.erase(it);
                    }
                    else
                    {
                        it++;
                    }
                }
                else
                {
                    it++;
                }
            }
        }
    }
    std::cout << "string now is: " << str << std::endl;
    return 0;
}

GroupBy 按学生姓名分组dc2密钥。 其中过滤包含重复项的组(2个键)。 ToDictionary 将结果转换为[重复的学生姓名,重复的学生姓名的密钥]的字典。