c++ std::map::find 到 c# 字典<key,val>
c++ std::map::find to c# dictionary<key,val>
再次进行语言转换。
我有一个 c++ std::map 需要转换为 C#。我相信它相当于一本字典。
我真正需要了解的地方是在 C++ 代码中有 map::find - 我需要在 C# 中做类似的事情,但还没有完全弄明白。
C++代码块
map<int, CAG_StatementWriter*> m_Writer;
map<int, DWORD> m_Counter;
other program code ....
int nPhysicalPages = 1 + nTextPages / 2; //Statement.m_nTotalPages
map<int, CAG_StatementWriter*>::iterator iter = m_Writer.find(nPhysicalPages);
if (iter == m_Writer.end() ||
(nPhysicalPages == 1 && m_Counter[1] % 2000 == 0) ||
(nPhysicalPages == 2 && m_Counter[2] % 1000 == 0)
)
{
// working code here
}
C#代码块
public static Dictionary<int, Writer> sWriter = new Dictionary<int, Writer>();
public static Dictionary<int, int> Counter { get; set; }
other program code...
int physicalPages = 1 + textPages / 2;
Dictionary<int, Writer>.ValueCollection iter = sWriter.<>(physicalPages);
if (iter == sWriter.<> ||
(physicalPages == 1 && Counter[1] % 2000 == 0) ||
(physicalPages == 2 && Counter[2] % 1000 == 0))
{
// working code here
}
我想弄清楚用什么来代替 c++ ::iterator。我知道 ValueCollection 不正确;当我决定 post 这个问题时,这就是我正在尝试的。
Dictionary 与 find 的等价物是 TryGetValue。与 find which returns 迭代器 TryGetValue returns 布尔值不同,如果它在集合中,则采用接收值的输出参数。
C# 没有像 c++ 那样的迭代器
查找字典中可能存在或不存在的值
Writer w = null;
if(sWriter.TryGetValue(physicalPage, out w))
{
// found and w has the found Writer object
}
如果你知道它在那里(或者不介意扔掉它)
Writer w = sWriter[physicalPage];
再次进行语言转换。
我有一个 c++ std::map 需要转换为 C#。我相信它相当于一本字典。
我真正需要了解的地方是在 C++ 代码中有 map::find - 我需要在 C# 中做类似的事情,但还没有完全弄明白。
C++代码块
map<int, CAG_StatementWriter*> m_Writer;
map<int, DWORD> m_Counter;
other program code ....
int nPhysicalPages = 1 + nTextPages / 2; //Statement.m_nTotalPages
map<int, CAG_StatementWriter*>::iterator iter = m_Writer.find(nPhysicalPages);
if (iter == m_Writer.end() ||
(nPhysicalPages == 1 && m_Counter[1] % 2000 == 0) ||
(nPhysicalPages == 2 && m_Counter[2] % 1000 == 0)
)
{
// working code here
}
C#代码块
public static Dictionary<int, Writer> sWriter = new Dictionary<int, Writer>();
public static Dictionary<int, int> Counter { get; set; }
other program code...
int physicalPages = 1 + textPages / 2;
Dictionary<int, Writer>.ValueCollection iter = sWriter.<>(physicalPages);
if (iter == sWriter.<> ||
(physicalPages == 1 && Counter[1] % 2000 == 0) ||
(physicalPages == 2 && Counter[2] % 1000 == 0))
{
// working code here
}
我想弄清楚用什么来代替 c++ ::iterator。我知道 ValueCollection 不正确;当我决定 post 这个问题时,这就是我正在尝试的。
Dictionary 与 find 的等价物是 TryGetValue。与 find which returns 迭代器 TryGetValue returns 布尔值不同,如果它在集合中,则采用接收值的输出参数。
C# 没有像 c++ 那样的迭代器
查找字典中可能存在或不存在的值
Writer w = null;
if(sWriter.TryGetValue(physicalPage, out w))
{
// found and w has the found Writer object
}
如果你知道它在那里(或者不介意扔掉它)
Writer w = sWriter[physicalPage];