How to change value of an element of a list [duplicate] - list

This question already has answers here:
Change values while iterating
(4 answers)
Range references instead values
(8 answers)
How to return changed values of slice from function? [duplicate]
(2 answers)
increment a struct var in a range loop [duplicate]
(1 answer)
Why can't I change the values in a range of type structure?
(2 answers)
Closed 4 days ago.
I want to change value of an element in a list :
package main
import (
"container/list"
"fmt"
)
type TestStruct struct {
Data int
}
func main() {
testList := list.New()
testList.PushBack(TestStruct{Data: 1})
testList.PushBack(TestStruct{Data: 2})
testList.PushBack(TestStruct{Data: 3})
e := testList.Front().Value.(TestStruct)
e.Data = 100
for entry := testList.Front(); entry != nil; entry = entry.Next() {
testElement := entry.Value.(TestStruct)
fmt.Println("entry : ", testElement.Data)
}
}
https://go.dev/play/p/jBhz-TFZVma
It doesn't work, value isn't changed.
I try to cast e but it is an invalid operation :
e := &testList.Front().Value
e.(*TestStruct).Data = 100
Or :
ptr := &(testList.Front().Value.(TestStruct).Data)
*ptr = 100
How can I do ? I need to cast *any to *TestStruct.
I don't understand, my post has been closed. I have this message :
This question already has answers here:
Change values while iterating (4 answers)
Range references instead values (8 answers)
increment a struct var in a range loop [duplicate] (1 answer)
How to return changed values of slice from function? [duplicate] (2 answers)
Why can't I change the values in a range of type structure? (2 answers)
I use list and not slice !!!

Related

Unity - How to check if items within a list are the same value? [duplicate]

This question already has answers here:
C# Determine Duplicate in List [duplicate]
(11 answers)
Closed 2 years ago.
I have a list of type Vector3Int that contains the end location of my agents movement, I want to compare these values to see if they are equal,
pseudo code;
if (2 or more items within the list are the same value) {
create a new end of path location for one or more of the agents
}
Thanks in advance for you help
There are a few ways to do it.
You can run a loop that uses a LINQ query. It would check each item in the list and see if there are more than 1 of that item in the list. LINQ's version of Count() allows you to compare values.
bool HasDuplicates = false;
foreach (var item in MyList) {
if (MyList.Count(i => i.x == item.x && i.y == item.y) > 1) {
HasDuplicates = true;
break;
}
}
Or you can use Distinct() to make a second list that only has 1 of every value. Then compare the count of both lists. If the distinct one has a lower count, then there must be duplicates in the list.
var HasDuplicates = (MyList.Distinct().Count < MyList.Count)

How to fix rand()/RAND_MAX in a method that always produces 0.0000000? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Integer division always zero [duplicate]
(1 answer)
Random number c++ in some range [duplicate]
(6 answers)
Closed 3 years ago.
I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.
I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.
void cPortfolio::randomize(cProblem &portfolioProblem) {
int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
int test;
for (int i = 0; i < assetCount; i++) {
double num = rand() / RAND_MAX; //this always produces 0.0000
int test = num * (portfolioProblem.assetNum); } `} //cannot format these correctly please ignore the brackets

What is the internal functioning of the following code? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.

Efficient way to get key from std::map Where the value is duplicate [duplicate]

This question already has answers here:
efficient way to get key from std::map value
(4 answers)
Reverse map lookup
(8 answers)
Closed 4 years ago.
std::map< std::string ,std::string> mObj;
mObj["one"] = "hello";
mObj["two"] = "hello";
mObj["three"] ="hello;
How to get key when input is hello(this is dup )
Ex 1 : when refer to mObj["one"] = "hello";
input : hello
output: one
Ex 2: when refer to mObj["two"] = "hello";
input : hello
output: two
Ex 3 :when refer to mObj["three"] ="hello;
input : hello
output : three
Is it good to make explicitly unique value to all the duplicate value during insert time. when input hello is received, i get the corresponding
unique value (which was done internally) and get the correct key.
Please help to address this problem.

How to limit input length in JFX textField? [duplicate]

This question already has answers here:
JavaFX TextField Array max length of text value
(1 answer)
How to restrict TextField so that it can contain only one '.' character? JavaFX
(3 answers)
Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number
(1 answer)
Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator
(4 answers)
Closed 5 years ago.
I need to limit input length to 4 digits in JFX textField (field can be empty or have value 0-9999). Below solution works only partially - I can input only digits , but as many as i want - limit do not work. Even if I remove {0,4} from regex and
change IF condition to:
if(newText.matches("\d") && newText.length()>=0 && newText.length()<5)
it doesn't work too. Where is the error?
JFXtextField.textProperty().addListener((obs, oldText, newText) ->
{
if(newText.matches("\\d{0,4}"))
{
newText = newText;
}
else
{
newText = oldText;
}
});