我的代码我有问题要拿出值,这是通过使用键自己的单词的单词数 那是我的代码,
template <typename T>
void BST<T>::clear(BTNode *subtree)
{
if (subtree != nullptr)
{
clear(subtree->m_Left);
clear(subtree->m_Right);
delete subtree;
}
}
拿出钥匙有问题。
答案 0 :(得分:3)
我不知道你究竟有什么问题,但我有个主意。
一个问题可能是您提供的单词不在字典中。这可能会导致KeyNotFoundException
。一个简单的解决方案就是:
if(Dict.ContainsKey(word)){
Console.WriteLine(Dict[word]);
} else {
Console.WriteLine(0); //Or whatever you deem appropriate
}
您可能遇到的另一个问题是foreach(var item in Dict)
。字典迭代元素对。变量item
的推断类型为KeyValuePair<string,int>
,而Console.WriteLine(item);
可能无法打印您所期望的内容。尝试将Console.WriteLine(item)
替换为Console.WriteLine(item.Key + " " +item.Value);
答案 1 :(得分:1)
试试这个:
string str = "Alameer Ashraf Hassan Alameer ashraf,elnagar.";
string[] CorpusResult = str.Split(' ', ',', '.');
//Creating the Dictionary to hold up each word as key & its occurance as Value ......!
Dictionary<string, int> Dict = new Dictionary<string, int>();
//loopnig over the corpus and fill the dictionary in .........!
foreach (string item in CorpusResult)
{
if (string.IsNullOrEmpty(item)) continue;
if (Dict.ContainsKey(item))
{
Dict[item]++;
}
else
{
Dict.Add(item, 1);
}
}
Console.WriteLine("Method 1: ");
foreach (var item in Dict)
{
Console.WriteLine(item.Value);
}
Console.WriteLine("Method 2: ");
foreach(var k in Dict.Keys)
{
Console.WriteLine(Dict[k]);
}
.NET小提琴:https://dotnetfiddle.net/JZ9Eid