I'm beginner in C++. Before I work with C#. Bellow is a C# script. How I can do same these things in native C++?
All I need is:
I tried std::map
but it don't have built-in sort by value or get key by value. Is C++ have similar thing like sortedlist
in c#?
Thank you very much!
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;
}
You seem to be doing the wrong way round as usually things are sorted using keys and queries are done using the key not using the value. However, it seems a std::map<int,int>
will help you here. Simply use your value as a key of the map and your key as value(so that you can query using the value). Use multimap if duplicates are allowed.
This are some converter tools:
Please visit the following links:
Like that:
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.