Home c# Replacement Dictionary C #

Replacement Dictionary C #

Author

Date

Category

I use Dictionary in C # to quickly check the existence of an element in an array by key, on the fact that I don’t need the key value, it looks like this:

public void update_object_select_contour (MyObject MYOBJECT)
  {
    List & lt; vector2i & gt; list_position_tmp = myobject.list_position;
    Dictionary & Lt; String, Byte & GT; dictionary_position_tmp = New Dictionary & LT; String, Byte & GT; ();
    For (int i = 0; i & lt; list_position_tmp.count; i ++)
    {
      dictionary_position_tmp.add (list_position_tmp [i] .tostring (), 0);
    }
    if (dictionary_position.containskey (new vector2i (5, 5) .tostring ()))
    {
      // search for key element
    }
  }

That is, I throw the coordinates of the points in the Dictionary and then check whether they are there on the search you need and I have 2 questions:

1) What storage structure can be used for this? (I tried to find trees in C #, I didn’t find it) I would like it to look like this:

dictionary & lt; string & gt;

2) What better to use the key to store the locations of the coordinates? How I know converting pairs to the text and store how the text is not the best idea


Answer 1, Authority 100%

You use a dictionary. The dictionary is when you have a key and there is a value associated with the key.

What you need is called Hashset & Lt; T & GT; .

If you want to store HASHSET & LT; T & GT; coordinates, then the coordinates should be desirable to be integers, since fractional numbers may not be equal even if they were counted on the same formula just Because of the presence of errors.

But with integers easier. But here you need to keep in mind, because if you store the coordinates in hashset & lt; T & GT; , which uses a hash function for storing and hash function and equivalence function to search for elements, you need to overload methods in the coordinate class Equals and Gethashcode . Here is an example:

public class coordinatees2d
{
  Public int x {get; }
  Public int Y {Get; }
  Public Coordinates2D (INT X, Int Y)
  {
    X = x;
    Y = y;
  }
  PROTECTED BOOL EQUALS (Coordinates2D Other)
  {
    Return X == Other.x & amp; & amp; Y == Other.y;
  }
  Public Override Bool Equals (Object OBJ)
  {
    if (referenceAequals (NULL, OBJ)) RETURN FALSE;
    if (referenceEQUALS (this, obj)) Return True;
    if (obj.gettype ()! = this.gettype ()) Return False;
    RETURN EQUALS ((COORDINATES2D) OBJ);
  }
  Public Override int Gethashcode ()
  {
    Unchecked.
    {
      Return (x * 397) ^ y;
    }
  }
}

Now this class can be used in Hashset & Lt; Coordinates2D & GT;

var set = new hashset & lt; coordinatees2d & gt; ();
set.add (new coordinatees2d (10, 25));
Console.Writeline (set.contains (New CoordinateS2D (1, 1))); // False
Console.WriteLine (set.contains (New Coordinates2D (10, 25))); // True.

The output is obvious

Previous articlelinux. PostgreSQL
Next articlenavgationdrawer and tabs

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions