Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this program:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; using namespace std; //holds two different datatypes template &lt;typename A, typename B&gt; class data { public: A dataA; B dataB; data* _next; data(){_next = NULL;} data* add(A dataA, B dataB){ this-&gt;dataA = dataA; this-&gt;dataB = dataB; return this; } bool operator&lt;(data&amp; rhs) { if (this-&gt;dataB ==rhs.dataB); return true; return (this-&gt;dataB &lt;rhs.dataB); } data* operator++() { if(this == NULL) return NULL; return _next; } }; //stores an array of the data template &lt;typename A, typename B&gt; class container { data&lt;A, B&gt; **dataArray; int ammount; public: typedef data&lt;A, B&gt; * iterator; container(){ //create dynamic array of pointers dataArray = new data&lt;A, B&gt;*[5]; ammount = 0; } ~container(){ for (int i = 0; i &lt; ammount; i++) { delete dataArray[i]; } } void add(A dataA, B dataB){ data&lt;A, B&gt; *newItem; if (ammount &lt;= 5){ newItem = new data&lt;A, B&gt;(); dataArray[ammount] = newItem-&gt;add(dataA, dataB); if(ammount != 0) { dataArray[ammount -1]-&gt;_next = dataArray[ammount]; } ammount++; } } //basic iterators iterator begin(){ return *dataArray; } iterator end(){ return dataArray[4]; } }; int main(){ string somestuff[] = { "one", "two", "three", "four", "five" }; container&lt;string, int&gt; foo; for (int i = 0; i &lt; 5; i++){ foo.add(somestuff[i], i); } container&lt;string, int&gt;::iterator it = foo.begin(); // why cant i just iterate through each item in the array for (it; *it &lt; *foo.end(); it =++ *it) { if(it != NULL) cout &lt;&lt; "Data A : " &lt;&lt; it-&gt;dataA &lt;&lt; " Data B : " &lt;&lt; it-&gt;dataB &lt;&lt; "\n"; else return 0; } return 0; } </code></pre>
    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.
 

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