如何将这个c#脚本转换为本地c++

How to convert this C# script to native C++?

本文关键字:转换 c++ 脚本      更新时间:2023-10-16

我是c++初学者。在我使用c#之前。Bellow是一个c#脚本。我如何在本地c++中做同样的事情?

我只需要:

  • 一个列表,或类似的,有int-int键值对
  • 可以按值自动排序。如果不是,它必须是按键排序的,并且它可以得到索引a值(我的每个值都是确定的)

我尝试了std::map,但它没有内置的按值排序或按值获取键。c++有类似c#中的sortedlist的东西吗?

非常感谢!

public static SortedList<int, int> sortedList1 = new SortedList<int, int>();
static void List_Add(int i) // 0 < i < 1000
{
    if (!sortedList1.ContainsValue(i))
        sortedList1[Environment.TickCount] = i;
}
static void List_Remove(int i) // 0 < i < 1000
{
    if (sortedList1.ContainsValue(i))
        sortedList1.RemoveAt(sortedList1.IndexOfValue(i));
}
static int List_toInt()
{
    int time = 0;
    int keys = 0;
    bool modifier = false;
    foreach (KeyValuePair<int, int> i in sortedList1)
    {
        if (i.Value > 90) modifier = true;
        if (i.Key - time > 200 | modifier | keys > 1000)
        {
            keys = keys * 1000 + i.Value;
            time = i.Key;
        }
    }
    return keys;
}

你似乎做错了,因为通常事情是用键排序的,查询是用键而不是用值。然而,std::map<int,int>似乎会在这里帮助你。只需将值用作映射的键,并将键用作值(以便可以使用值进行查询)。

这是一些转换器工具:
请访问以下链接:

    http://sourceforge.net/projects/convetercpptocs/
  • http://www.tangiblesoftwaresolutions.com/Product_Details/CSharp_to_CPlusPlus_Converter_Details.html
  • http://cscpp.codeplex.com/

就像这样:

#include <map>
#include "Winbase.h"
std::map<int, int> sortedList1;
void List_Add(int i) // 0 < i < 1000
{
    if (sortedList1.find(i) == sortedList1.end())
        sortedList1.insert(std::make_pair<int, int>(GetTickCount(), i));
}
void List_Remove(int i) // 0 < i < 1000
{
    if (sortedList1.find(i) != sortedList1.end())
        sortedList1.erase(sortedList1.find(i));
}
int List_toInt()
{
    int time = 0;
    int keys = 0;
    bool modifier = false;
    for (std::map<int, int>::const_iterator it = sortedList1.cbegin(); 
        it != sortedList1.cend(); it++)
    {
        if (it->second > 90) modifier = true;
        if (it->first - time > 200 || modifier || keys > 1000)
        {
            keys = keys * 1000 + it->second;
            time = it->first;
        }
    }
    return keys;
}