variable-assignment 相关问题

设置或重新设置存储在由变量名称表示的存储位置中的值的过程。

数量增加到2、3和4

我编写了以下简单的 C++ 程序来输出用户输入的数字的幂到索引 2、3 和 4: 整数a = 0; 辛 >> 一个; int a2 = a * a; 计算<< "\n"...

回答 1 投票 0

为什么 bash 在评估我的变量时插入了不需要的“'”?

我有这个脚本: #!/bin/bash TOKEN='faketokendgfhjkdhf' KEYNAME="test_$(日期+'%m-%Y')" 回显$KEYNAME KEYPATH="$HOME/.ssh/${KEYNAME}" 回显$KEYPATH SSHPHRASE="测试短语...

回答 1 投票 0

你能编写一个同时处理复制构造函数和复制赋值运算符的通用函数吗?

由于复制构造函数 MyClass(const MyClass&); 和 = 运算符重载 MyClass& 运算符 = (const MyClass&); 有几乎相同的代码,相同的参数,只有不同......

回答 3 投票 0

Verilog 中的作业

为什么 Verilog 模拟器以不同的方式处理这些情况? 1) 总是#1时钟=!时钟; 最初的 开始 @(posege时钟) 开始<= 1; end initial begin clk <= 1; start <= 1; end The literature

回答 1 投票 0

如何从 shell 脚本获取变量并通过管道传输脚本的输出?

我有一个脚本test.sh: #!/bin/bash 导出 VAR1=abc123 回显$VAR1 我希望在运行此脚本的 shell 中设置 test.sh 中设置的变量,因此我使用源: $ 源 test.sh ...

回答 1 投票 0

替换对象列表中的对象

在 C# 中,如果我有一个 List,并且有一个 T 类型的对象,如何用 T 类型的对象替换 List 中的特定项? 这是我尝试过的: 列表 在 C# 中,如果我有一个 List<T>,并且有一个 T 类型的对象,如何将 List<T> 中的特定项目替换为 T 类型的对象? 这是我尝试过的: List<CustomListItem> customListItems = new List<CustomListItem>(); CustomListItem customListItem1 = new CustomListItem() { name = "Item 1", date = DateTime.MinValue}; CustomListItem customListItem2 = new CustomListItem() { name = "Item 2", date = DateTime.MinValue }; CustomListItem customListItem3 = new CustomListItem() { name = "Item 3", date = DateTime.MinValue }; customListItems.Add(customListItem1); customListItems.Add(customListItem2); customListItems.Add(customListItem3); CustomListItem newCustomListItem = new CustomListItem() { name = "Item 4", date = DateTime.Now }; customListItem2 = customListItems.Where(i=> i.name == "Item 2").First(); customListItem2 = newCustomListItem; 在上面的代码中,我想用 customListItem2 替换 newCustomListItem。 我是否必须删除列表中的项目,然后插入新项目?我可以不做简单的customListItem2 = newCustomListItem作业吗? 用另一个项目替换列表中的项目的最有效方法是什么? 你必须更换的是物品,而不是customListItem2的值。只需替换以下内容: customListItem2 = customListItems.Where(i=> i.name == "Item 2").First(); customListItem2 = newCustomListItem; 有了这个: customListItem2 = customListItems.Where(i=> i.name == "Item 2").First(); var index = customListItems.IndexOf(customListItem2); if(index != -1) customListItems[index] = newCustomListItem; 编辑: 正如 Roman R. 在评论中所述,您可以用简单的 .Where(predicate).First() 替换 First(predicate): customListItem2 = customListItems.First(i=> i.name == "Item 2"); var customListItems = new List<CustomListItem>(); var customListItem1 = new CustomListItem() { name = "Item 1", date = DateTime.MinValue }; var customListItem2 = new CustomListItem() { name = "Item 2", date = DateTime.MinValue }; var customListItem3 = new CustomListItem() { name = "Item 3", date = DateTime.MinValue }; customListItems.Add(customListItem1); customListItems.Add(customListItem2); customListItems.Add(customListItem3); var newCustomListItem = new CustomListItem() { name = "Item 4", date = DateTime.Now }; customListItems[customListItems.FindIndex(x => x.name == "Item 2")] = newCustomListItem; 或 public static class ListExtensions { public static void Replace<T>(this List<T> list, Predicate<T> oldItemSelector , T newItem) { //check for different situations here and throw exception //if list contains multiple items that match the predicate //or check for nullability of list and etc ... var oldItemIndex = list.FindIndex(oldItemSelector); list[oldItemIndex] = newItem; } } 然后 customListItems.Replace(x => x.name == "Item 2", newCustomListItem); 如果列表的顺序对你来说不重要 你可以试试这个 CustomListItem newCustomListItem = new CustomListItem() { name = "Item 4", date = DateTime.Now }; customListItem2 = customListItems.Where(i=> i.name == "Item 2").First(); customListItems.Remove(customListItem2); customListItems.Add(newCustomListItem ); 您可以使用选择来构建一个新的 iEnumerable 并替换项目: customListItems.Select(x => x.name == "Item 2" ? newItem : x).ToList(); 我没有测试性能,但我发现这个解决方案非常简洁和明确。 我通常有一些定义关键字段的基本接口,以便我可以更轻松地使用泛型。因此继承 IKey 的类将有一个字符串 Key 字段,它是记录的唯一标识符。 因此,在哈米德的示例之上构建,但具有“添加或替换”概念: public static void AddOrReplace<T>(this List<T> items, T newItem) where T : class, IKey { items.AddOrReplace(x => x.Key == newItem.Key, newItem); } public static void AddOrReplace<T>(this List<T> items, Predicate<T> oldItemSelector, T newItem) { var index = items.FindIndex(oldItemSelector); if (index < 0) { items.Add(newItem); } else { items[index] = newItem; } } 然后为了实现这些扩展,我可以使用与 list.Add() 类似的语法: list.AddOrReplace(newItem); 另一种方法是使用 DynamicData 包,列表中还有很多其他设施: 快速解答 确保已安装该软件包: Install-Package DynamicData -Version 7.4.3 然后 using DynamicData; DynamicData.ListEx.Replace<CustomListItem>(customListItems, customListItem2, newCustomListItem); 以下是完整的工作解决方案。 工作示例 我的自定义项目类别 public class CustomListItem { public string name; public DateTime date; public override string ToString() { return name + ", " + date.ToString(); } } 在主要尝试中,请确保在替换之前注释掉分配。 static void Main(string[] args) { List<CustomListItem> customListItems = new List<CustomListItem>(); CustomListItem customListItem1 = new CustomListItem() { name = "Item 1", date = DateTime.MinValue }; CustomListItem customListItem2 = new CustomListItem() { name = "Item 2", date = DateTime.MinValue }; CustomListItem customListItem3 = new CustomListItem() { name = "Item 3", date = DateTime.MinValue }; customListItems.Add(customListItem1); customListItems.Add(customListItem2); customListItems.Add(customListItem3); CustomListItem newCustomListItem = new CustomListItem() { name = "Item 4", date = DateTime.Now }; customListItem2 = customListItems.Where(i => i.name == "Item 2").First(); //customListItem2 = newCustomListItem; DynamicData.ListEx.Replace<CustomListItem>(customListItems, customListItem2, newCustomListItem); foreach(var customListItem in customListItems) { Debug.Print("\n" + customListItem.ToString()); } } references: https://github.com/reactivemarbles/DynamicData. https://dynamic-data.org

回答 6 投票 0

使用另一个对象构造一个对象的不同方法

前几天我读到了这篇文章: int 类型的构造函数 但我想我必须先澄清一件事: 假设我有一个类 Myclass 并且我已经创建了一个对象 obj1。 有区别吗

回答 1 投票 0

Scala 3:在赋值之前使用变量时意外成功编译

我希望这会引发错误,因为我在分配 x 之前尝试使用它。这是 Scala 3.3.1 REPL: scala> val x: Int = |值 y: Int = x + 10 | y | 值 x:整数 = 10 ...

回答 1 投票 0

变量是名称、值还是内存位置?

我学Python有几个月了,对C也知之甚少,我想知道是否有人可以为我解答这个疑惑: 变量是名称、值还是内存位置? ...

回答 5 投票 0

Python 3 中使用链表进行三个(或更多)不同变量赋值的操作顺序

我正在编写简单的代码来反转链表,并意识到可以在一行上完成分配,我发现这很酷: def 反向(头): 上一个节点 = 无 curr_node = h...

回答 1 投票 0

Python 3 个变量并行赋值

我的问题来自一个流行的编码测试。 给定一个定义如下的链: 类列表节点: def __init__(自身, x): self.val = x self.next = 无 如果我们想恢复ch...

回答 1 投票 0

无法将从服务器接收到的单个 json 对象转换为所需类型,以便剖析数据

如果我问了太愚蠢的问题,我提前道歉,但我对 Angular 真的很陌生,我不明白如何处理来自服务器的 JSON 对象并将该对象转换为 c...

回答 1 投票 0

为什么赋值运算符 (=) 在 foreach 循环中无效?

为什么赋值运算符 (=) 在 foreach 循环中无效?我使用的是 C#,但我假设该参数对于支持 foreach 的其他语言(例如 PHP)是相同的。例如,如果我这样做

回答 11 投票 0

将变量转换为结构体中的半字节

我面临以下问题。我有一个这样的结构: 类型定义结构 { uint8 半字节_1 : 4; uint8 半字节_2 : 4; uint8 data_1; uint8 data_2; uint8 data_3; NibbleStruct; 这是 32...

回答 1 投票 0

使用for循环动态创建变量并将它们添加到python中的列表中

我想使用for循环动态命名变量,为变量赋值,然后将变量添加到列表中。 这是 Tadeck 关于如何在 for l 中动态创建变量的答案...

回答 1 投票 0

CODESYS:我可以分配并数组到另一个数组吗?

我可以在codesys上执行以下操作吗? 宣言 VAR abVariable_A 字节数组[0...15] := [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; abVariable_B ARRAY[1...16] OF BYTE := [0,0,0,...

回答 1 投票 0

打印时,变量将之前的值与 C 中的新值相加

我正在编写这个程序来打印多个测试用例的两个给定数字之间的连续奇数之和。它将首先获取测试用例的输入并循环运行,然后它将...

回答 2 投票 0

如何在随机填充的二维数组中找到最高值

我正在制作一个程序,插入一个带有随机整数的二维数组,并找到最高的值来显示它及其在数组中的坐标。 我正在使用三个文件: 实用工具.h 实用程序....

回答 3 投票 0

如何在c中相互比较字符串字母以使字母不重复(不区分大小写)?它总是显示一些指针错误

我是编码(和这个网站)的新手,正在学习 cs50 课程。 #包括 #包括 #包括 #包括 #包括 #包括...

回答 2 投票 0

检查返回值是否不为空,如果是,则通过一个方法调用在一行中分配它

Java 中充斥着这样的语句: if(cage.getChicken() != null) { 晚餐=笼子.getChicken(); } 别的 { 晚餐= getFreeRangeChicken(); } 在...之前需要两次调用 getChicken()

回答 10 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.