gpt4 book ai didi

c# - 什么是 C++ 中拼接的 C# 替代方案

转载 作者:行者123 更新时间:2023-11-30 02:25:39 25 4
gpt4 key购买 nike

我有一个 C++ 代码并尝试用 C# 编写

我想不出在 C# 中拼接的最佳替代方案

C++ 也有一个“查找”可以在 map 上工作,我想在字典上用 C# 实现它

最佳答案

在您的 C++ 示例中,您显示:

statement_tokens.splice(statement_tokens.begin(), tokens, tokens.begin(), next_sc);

根据我的理解(documentation),这个重载需要一个插入位置、一个列表(相同类型)以及一个范围的第一个(包括)和最后一个(不包括)索引以拼接到插入位置, 然后将此范围插入到原始列表中。

更新:并且它从另一个列表中删除项目。我刚刚添加了该功能。

如果这是正确的,那么下面的扩展方法应该可以工作:

列表扩展方法 (检查此答案的末尾以了解此方法的其他重载)

public static class ListExtensions
{
public static void Splice<T>(this List<T> list, int insertAtIndex, List<T> items,
int first, int last)
{
if (items == null) return;
insertAtIndex = Math.Min(list.Count, Math.Max(0, insertAtIndex));
first = Math.Min(items.Count - 1, Math.Max(0, first));
last = Math.Min(items.Count, Math.Max(1, last));
if (first >= last) return;

list.InsertRange(insertAtIndex, items.GetRange(first, last - first));
items.RemoveRange(first, last - first);
}
}

更新 2:现在,您似乎缺少 std::find_if 的另一个扩展方法,它返回指定范围内列表项的索引,基于如果项目满足某些条件则返回 true 的方法。因此,让我们将以下方法添加到上面的 ListExtensions 类中:

public static int FindIf<T>(this List<T> list, int start, int end, Func<T, bool> method)
{
if (method == null) return end;
if (!list.Any(method)) return end;
start = Math.Min(list.Count - 1, Math.Max(0, start));
end = Math.Min(list.Count, Math.Max(1, end));
if (start >= end) return end;
var range = list.GetRange(start, end - start);
var index = range.IndexOf(list.First(method));
if (index < start) return end;
return index;
}

请注意,此方法的一个参数是一个函数,该函数采用 T 类型的项目并返回 bool。这将是一个简单的方法,用于检查我们的 token 的字符串值是否为分号:

static bool TokenIsSemicolon(EvlToken token)
{
return (token != null && token.Str == ";");
}

现在,您可能会注意到我引用了 token.Str。这来自 EvlToken 类,创建该类是为了模仿 C++ 结构:

class EvlToken
{
public enum TokenType { Name, Number, Single }
public TokenType Type { get; set; }
public string Str { get; set; }
public int LineNo { get; set; }
}

现在我们可以完成对原始方法的转换,调用我们的FindIfSplice 扩展方法:

static bool MoveTokensToStatement(List<EvlToken> statementTokens, List<EvlToken> tokens)
{
if (statementTokens == null || statementTokens.Count > 0) return false;
if (tokens == null || tokens.Count == 0) return false;

int nextSemiColon = tokens.FindIf(0, tokens.Count, TokenIsSemicolon);

if (nextSemiColon == tokens.Count)
{
Console.WriteLine("Looked for ';' but reached the end of the file.");
return false;
}

++nextSemiColon;
statementTokens.Splice(0, tokens, 0, nextSemiColon);
return true;
}

额外的重载

为了完整起见,这里是扩展类以及文档中提到的其他两个重载:

public static class ListExtensions
{
/// <summary>
/// Transfers all elements from 'items' into 'this' at the specified index
/// </summary>
/// <typeparam name="T">The type of items in the list</typeparam>
/// <param name="list">'this' instance</param>
/// <param name="insertAtIndex">The index to insert the items</param>
/// <param name="items">The list to transfer the items from</param>
public static void Splice<T>(this List<T> list, int insertAtIndex,
List<T> items)
{
if (items == null) return;
list.Splice(insertAtIndex, items, 0, items.Count);
}

/// <summary>
/// Transfers the element at 'itemIndex' from 'items'
/// into 'this' at the specified index
/// </summary>
/// <typeparam name="T">The type of items in the list</typeparam>
/// <param name="list">'this' instance</param>
/// <param name="insertAtIndex">The index to insert the item</param>
/// <param name="items">The list to transfer the item from</param>
/// <param name="itemIndex">The index of the item to transfer</param>
public static void Splice<T>(this List<T> list, int insertAtIndex,
List<T> items, int itemIndex)
{
list.Splice(insertAtIndex, items, itemIndex, itemIndex + 1);
}

/// <summary>
/// Transfers the specified range of elements from 'items'
/// into 'this' at the specified index
/// </summary>
/// <typeparam name="T">The type of items in the list</typeparam>
/// <param name="list">'this' instance</param>
/// <param name="insertAtIndex">The index to insert the item</param>
/// <param name="items">The list to transfer the item from</param>
/// <param name="first">The index of the first item in the range</param>
/// <param name="last">The exclusive index of the last item in the range</param>
public static void Splice<T>(this List<T> list, int insertAtIndex, List<T> items,
int first, int last)
{
if (items == null) return;
insertAtIndex = Math.Min(list.Count, Math.Max(0, insertAtIndex));
first = Math.Min(items.Count - 1, Math.Max(0, first));
last = Math.Min(items.Count, Math.Max(1, last));
if (first >= last) return;

list.InsertRange(insertAtIndex, items.GetRange(first, last - first));
items.RemoveRange(first, last - first);
}

/// <summary>
/// Searches for the first item in the specified range that "method" returns true for
/// </summary>
/// <typeparam name="T">The type of items in the list</typeparam>
/// <param name="list">'this' instance</param>
/// <param name="start">The index of the first item in the range</param>
/// <param name="end">The exclusive index of the last item in the range</param>
/// <param name="method">A method which takes type 'T' and returns a bool</param>
/// <returns>The index of the item, if found, otherwise 'end'</returns>
public static int FindIf<T>(this List<T> list, int start, int end, Func<T, bool> method)
{
if (method == null) return end;
if (!list.Any(method)) return end;
start = Math.Min(list.Count - 1, Math.Max(0, start));
end = Math.Min(list.Count, Math.Max(1, end));
if (start >= end) return end;
var range = list.GetRange(start, end - start);
var index = range.IndexOf(list.First(method));
if (index < start) return end;
return index;
}
}

示例用法

这是一个使用 EvlTokens 列表,然后调用 MoveTokensToStatement 两次的示例:

private static void Main()
{
var tokens = new List<EvlToken>
{
new EvlToken {LineNo = 3, Str = "int", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 3, Str = "x", Type = EvlToken.TokenType.Name},
new EvlToken {LineNo = 3, Str = "=", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 3, Str = "1", Type = EvlToken.TokenType.Number},
new EvlToken {LineNo = 3, Str = "+", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 3, Str = "5", Type = EvlToken.TokenType.Number},
new EvlToken {LineNo = 3, Str = ";", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 4, Str = "Console", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 4, Str = ".", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 4, Str = "WriteLine", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 4, Str = "(", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 4, Str = "Hello World", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 4, Str = ")", Type = EvlToken.TokenType.Single},
new EvlToken {LineNo = 4, Str = ";", Type = EvlToken.TokenType.Single}
};

var statementTokens = new List<EvlToken>();

MoveTokensToStatement(statementTokens, tokens);

Console.WriteLine("Here is the result of calling 'MoveTokensToStatement' the first time:");
Console.WriteLine(string.Join(" ", statementTokens.Select(t => t.Str)));
statementTokens.Clear();

MoveTokensToStatement(statementTokens, tokens);

Console.WriteLine("\nHere is the result of calling 'MoveTokensToStatement' the second time:");
Console.WriteLine(string.Join("", statementTokens.Select(t => t.Str)));
statementTokens.Clear();


Console.WriteLine("\nDone!\nPress any key to exit...");
Console.ReadKey();
}

输出

enter image description here

关于c# - 什么是 C++ 中拼接的 C# 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44012708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com