How does std::find auto-magically knows the end of an array in C++? -


Why is there a nuclear variable in the code given below? - Because this is part of a larger code base where m_subscriptions are being updated with multiple threads. Please keep me on that one . My question is:

  #include & lt; Iostream & gt; # Include & lt; Atomic & gt; # Include & lt; Algorithm & gt; #include & lt; Iterator & gt; # Definitions MAX_SUBSCRIPTIONS 45 static stadium :: atomic & lt; Int & gt; NumberOfSubscriptions; Int main () {int m_subscriptions [MAX_SUBSCRIPTIONS] = {}; NumberOf Subscriptions Fetch_add (0, std :: memory_order_relaxed); M_subscriptions [number number membership] = 12; NumberOfSubscriptions.fetch_add (1); M_subscriptions [number number membership] = 13; NumberOfSubscriptions.fetch_add (1); M_subscriptions [Subscriptions to numbers] = 14; NumberOfSubscriptions.fetch_add (1); M_subscriptions [number number membership] = 15; NumberOfSubscriptions.fetch_add (1); M_subscriptions [Subscriptions to numbers] = 16; NumberOfSubscriptions.fetch_add (1); For (Int i = 0; I & lt; Number of subscriptions, i ++) std :: cout & lt; & Lt; M_subscriptions [i] & lt; & Lt; "\ T"; Std :: cout & lt; & Lt; Std :: endl; Auto results = std :: search (m_subscriptions, m_subscriptions + subscribe to numbers, 44); If (results == std :: end (m_subscriptions)) std :: cout & lt; & Lt; "Not Found" & lt; & Lt; Std :: endl; Else std :: cout & lt; & Lt; * Results & lt; & Lt; Std :: endl; }  

does not print when I do not give anything "which is not found" which is not present in the array.

If I change (result == std :: end (m_subscriptions)) with

if (result == (m_subscriptions) + NumberOf membership))) This works! how

how std :: find shows how many values ​​have been started in m_subscriptions ? Should not it go to the end, like someone with MU subscriptions [MAX_SUBSCRIPTIONS] would expect a dumb computer?

In other words, why is searching for search 44 to m_subscriptions + numberOf subscriptions ? m_subscriptions [MAX_SUBSCRIPTIONS] ?

Std :: find has no knowledge of the elements in the array or the elements that begin within it. You told where to stop with the second argument:

  std :: find (m_subscriptions, // start here m_subscriptions + subscribe to numbers, // stop here 44); // Try to find it.  

If you do not want to find what you are looking for, then it just returns the second argument, which is the end of the limit given by you.

If you have passed std :: end (m_subscriptions) as the second contract, then it will rotate with all the elements that you have not assigned, however, The anger that it is not drawn to, the price has been started. When you do the total initially with {} , any member of the value that starts with the sum not specified by you starts even though, even if they are unrefined, there is no way to tell it Is not it whether something has been started or not?


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -