Search code examples
c#c++language-construct

C# equivalent of C++ std::vector<int>.end()


I was trying to rewrite some C++ code I found on the internet in C# for my hexagon sphere project, but I ran into the following code:

if((((*ti)->m_hexA) != tileNdx) && (find(nbrs. begin(), nbrs.end(), ((*ti)->m_hexA)) == nbrs.end()))
{
    nbrs.push_back(((*ti)->m_hexA));
}

I have it mostly converted to C#. I even built my own Find<T>(List<T> list, T value) method that achieves the same basic functionality as std::find.

I am still unsure about the docs for std::vector<T>::end() however. The docs say it returns an iterator pointing to a "place holder" slot at the end of the iterator, and that attempting to access it will result in "undefined behaviour". Does this:

1.) Mean that in my C#, when dealing with regular objects performing comparisons with an end() element, I should just compare the object to null?

2.) With integers and other primitives, should I just compare against a sentinel value such as -1?

In the above c++ source code, nbrs is a std::vector, m_hexA is an int, and ti is a struct.


Solution

  • The whole construction:

    (find(nbrs. begin(), nbrs.end(), ((*ti)->m_hexA)) == nbrs.end())
    

    could be written in C# using LinQ:

    nbrs.FirstOrDefault(i => i == ti.m_hexA) == null
    

    or

    !nbrs.Any(i => i == ti.m_hexA)
    

    where ((*ti)->m_hexA) is equivalent of ti.m_hexA.