quote:Original post by Sneftel
hmm, yeah. Well, really what you should do is throw an exception, since there''s nothing you could reasonably return to indicate an error. However, if you want you could do:
return *((T*)0);
!
quote:Original post by Sneftel
hmm, yeah. Well, really what you should do is throw an exception, since there''s nothing you could reasonably return to indicate an error. However, if you want you could do:
return *((T*)0);
quote:Original post by aaron_dsYeah, I can tell. O(n) = O(n/2) = O(c*n) where c is a constant. Or even, O(n^2+n) = O(n^2).
I''m no big O notation whiz. Would this make change it from O(n) to O(n/2)?
for(int i=0; i<list.length; i++) v = list[i].value;
would then just have to find element 0 (pretty fast, i guess), and the caching-mechanism would make this loop almost just as fast asfor(element=list.first; element<=list.last; element=element.next) v = element.value;
quote:What if the first element of a linked list contains a pointer to the element with index = length/2? And that element contains a pointer to index = length/4 and index = length * 3/4? Wouldn''t it produce a much faster list[x]?
quote:And what if the list-structure had an easy caching mechanism? What if it had an integer with the last used index, and a pointer to that element? Wouldn''t finding list[x+1] be much faster?
quote:If the [] operator contains a static iterator, then it''s OK.
quote:True, but perhaps that''s because the STL isn''t being consistent. I mean, one of the first ''gotchas'' people learn when using the STL is "never use size() to test for an empty container". Why? Because, while size() SHOULD be a constant-time operation it CAN be a linear-time operation on SOME list implementations.
quote:So why make size() for a list but not operator[]?