如何在词典末尾添加数据 [英] How do I add data at the end of the Dictionary

查看:95
本文介绍了如何在词典末尾添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

以下是我的代码片段。



Hi Friends,
below is my code snippet.

#include "stdafx.h"

#using "system.dll"
using namespace System;
using namespace System::Collections::Generic;


int _tmain(int argc, _TCHAR* argv[])
{
	
	Dictionary<string^,string^>^ storeDir = gcnew  Dictionary<string^,>();	

	storeDir->Add("G02"	,"G01");
	storeDir->Add("G23"	,"G02");
	storeDir->Add("G45"	,"G03");
	storeDir->Add("G55"	,"G04");

	for each (KeyValuePair<string^,> p in storeDir)
	{
		Console::WriteLine("{0} ==== {1}\n",p.Key,p.Value);
	}

	String^ StringstrtobeSearch =  "G55";
	String^ strgatevalue;

	if (storeDir->TryGetValue(StringstrtobeSearch,strgatevalue))
	{
		Console::WriteLine(strgatevalue);
	}
	storeDir->Remove("G23");
	Console::WriteLine("Remove data from Store Directory \n");
	for each (KeyValuePair<string^,> p in storeDir)
	{
		Console::WriteLine("{0} ==== {1}\n",p.Key,p.Value);
	}
	storeDir->Add("G23"	,"G02");

	Console::WriteLine("After adding again\n");
	for each (KeyValuePair<string^,> p in storeDir)
	{
		Console::WriteLine("{0} ==== {1}\n",p.Key,p.Value);
	}
	return 0;
}





我有一个场景,我必须删除storeDir-> Add(G23,G02 );从词典然后我必须添加这样的方式

storeDir->添加(G23,G02);应该到最后。

但是在上面的示例中它总是对目录进行排序。

是否还有其他容器可以用来实现我的目标?



I have a scenario where I have to remove "storeDir->Add("G23" ,"G02");" from Dictionary and then I have to add in such a way that
"storeDir->Add("G23" ,"G02");" should come at the end.
but here in the above example it always sort the directory.
is there any other container which i can use to achive my objectice ?

推荐答案

字典是查找对象,它以某种方式内部排序存储数据。如果您想要特殊订单,最好使用数组。但它没有这样的查找功能,你必须迭代才能找到一个特定的值。
A dictionary is lookup object which stores the data somehow "internally" sorted. If you want to have a special order you better use an array. But it has not such lookup function and you must iterate to find a specific value.


你不能用字典来做,因为它没有订单的概念。相反,请使用 NameValueCollection Class [< a href =https://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]可以使用密钥或使用index.eg

You can't do it with dictionary as it has no notion of order. Instead, use NameValueCollection Class[^] wihch can be accessed either with the key or with the index.e.g.
using System;
using System.Collections.Specialized;

public class Program
{
    public static void Main()
    {
        NameValueCollection grade = new NameValueCollection();

        grade.Add("excellent", "80");
        grade.Add("good", "60");
        grade.Add("average", "50");
        grade.Add("fail", "40");

        Console.WriteLine("Before: " + grade[3]);

        grade.Remove("good");
        grade.Add("good","60");

        Console.WriteLine("After: " + grade[3]);
    }
}



结果:


The outcome:

Before: 40
After: 60


从概念上讲,字典没有开头和结尾 - 只是一个键和值。

添加一个新的键和值将添加到最后一个位置。
Conceptually, a dictionary does not have a beginning and an end - just a key and value.
Adding a new key and value will add at the last location.


这篇关于如何在词典末尾添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆