Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Probably the closest Delphi analogue to <code>std::vector&lt;T&gt;</code> is <a href="http://docwiki.embarcadero.com/Libraries/en/System.Generics.Collections.TList" rel="noreferrer"><code>TList&lt;T&gt;</code></a>. You can iterate over the list with a <a href="http://docwiki.embarcadero.com/RADStudio/en/Declarations_and_Statements#Iteration_Over_Containers_Using_For_statements" rel="noreferrer"><code>for in</code></a> loop:</p> <pre><code>var Item: Integer; List: TList&lt;Integer&gt;; .... for Item in List do Writeln(Item); </code></pre> <p>If you have a dynamic array rather than a <code>TList&lt;T&gt;</code> then you can use <code>for in</code> to iterate over the elements. In fact, all the built in containers support <code>for in</code> and it is easy to add support for <code>for in</code> to your own classes.</p> <p>In C++ there is nothing like a <code>for in</code> loop and so the idiom is to use an STL algorithm. That's what drives you to using an anonymous function. In Delphi with the <code>for in</code> syntax you can express "iterate over all members of the container" in a natural way without resorting to anonymous methods.</p> <p>Generics were added to Delphi in Delphi 2009 and the <code>for in</code> loop was added in Delphi 2005, so all this is available to you in XE. For what it's worth, anonymous were also added in Delphi 2009.</p> <p>What you must realise is that Delphi generics are less powerful than C++ templates. Although you talk about a generic <code>foreach</code>, your code is not generic in the sense that it has specialised to <code>int</code>. You could write a generic version of your code in C++ but this would be much harder to do with Delphi generics due to the inherent limitations of generics when compared to templates. An attempt to write the above code in a generic way in Delphi would founder at the point where you tried to call <code>Writeln</code>. Whilst that would be trivial with C++ templates it is frustratingly out of reach for generics.</p> <p><strong>Update:</strong> In the comments you ask if there is a slick way to add the contents of one container to another. The <a href="http://docwiki.embarcadero.com/Libraries/en/System.Generics.Collections.TList.AddRange" rel="noreferrer"><code>AddRange</code></a> method does that. <code>TList&lt;T&gt;.AddRange()</code> has three overloaded variants that receive one of the following input parameters: <code>array of T</code>, <code>Collection: IEnumerable&lt;T&gt;</code> or <code>Collection: TEnumerable&lt;T&gt;</code>. All the standard generic containers follow a similar pattern.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload