C++ STL
1
Name:
Anonymous
2009-08-28 12:46
Is it faster to iterate through stl vector with the [] operator or an iterator?
I would think that the iterator is faster but I'm too lazy to test.
2
Name:
Anonymous
2009-08-28 13:05
Accessing individual elements by their position index = constant time
Iterating over the elements in any order = linear time
3
Name:
Anonymous
2009-08-28 13:20
Back to summer reading for CS101 , please .
4
Name:
Anonymous
2009-08-28 14:27
>>2
DOUBLE EXPERT SEPPLES PROGRAMMER AND /PROG/ POSTER
If this is the statistical
/prog/ -rider's knowledge of Sepples then no wonder they hate it.
>>5 is the winner
[1]
References:
1. http://www.cplusplus.com/reference/stl/vector/
"Accessing individual elements by their position index (constant time)."
"Iterating over the elements in any order (linear time)."
8
Name:
Anonymous
2009-08-28 17:19
He said iterate, not access. Learn to read. I would say [] is faster because it can be optimized better?
9
Name:
Anonymous
2009-08-28 17:22
>>8
Quoting myself. Actually both should end up as the same after optimizing.
12
Name:
Anonymous
2009-08-28 19:43
>>5,2
Excellent
!
Now that I know this
, I can sort my
vectors in
O(N) time (probably
faster )
! Thank you
, /YHBT/ !
13
Name:
Anonymous
2009-08-28 19:49
>>1
[] operator should be faster because it skips bounds checking and doesn't throw exceptions (but don't take my word for it).
>>2-12
You lose.
1
[1]
IHBT
14
Name:
Anonymous
2009-08-28 20:21
Any properly optimized STL implementation will have iterators directly map to pointers and the [] operator also be a pointer operation:
template <class _T>
class vector {
...
typedef _T *iterator;
...
iterator begin() { return vec; }
iterator end() { return vec+used; }
_T operator[](size_t offs) { return *(vec+offs); }
...
size_t capacity, used;
_T *vec;
};
15
Name:
Anonymous
2009-08-29 5:07
>>9
I
>>14
was right
>>13
motherfucker
16
Name:
Anonymous
2009-08-29 5:09
Any properly optimized STL implementation will be written as you would in C without sepples garbage
fixd
17
Name:
Anonymous
2009-08-29 9:22
>>14
True but not for all cases. Use a
deque instead of a
vector and see where that gets you.
18
Name:
Anonymous
2009-08-29 10:30
>>17
True but not for all cases. Use a
vector instead of a
deque and see where that gets you.
19
Name:
Anonymous
2009-08-29 10:30
>>18
True but not for all cases. Use a
map instead of a
vector and see where that gets you.
20
Name:
Anonymous
2009-08-29 10:34
>>19
True but not for all cases. Use a
java.util.LinkedHashSet instead of a
vector and see where that gets you.
21
Name:
Anonymous
2009-08-29 12:02
>>20
True but not for all cases. Use a
[[Char]] instead of a
String[] and see where that gets you.
22
Name:
Anonymous
2009-08-29 12:12
WE CONJURE THE SPIRIT OF THE COMPUTER WITH OUR SPELLS
23
Name:
Anonymous
2009-08-29 12:55
24
Name:
Anonymous
2009-08-29 16:00
I CONJURE THE SPIRITS OF UNSATISFYING CASUAL BUTTSEX WITH MY ANUS
25
Name:
Anonymous
2009-08-29 16:10
If you keep your asshole clean enough and have good hygiene, it smells like a cunt. And tastes like one, too.
26
Name:
Anonymous
2009-08-29 16:17
>>25
This is why people use C++.
27
Name:
Anonymous
2009-08-29 17:03
case statements shouldn't require constants. const should create compile time constants. a real type system, so things like
int string = "string"; or
sin("string"); are not possible. No goto statement.
28
Name:
Anonymous
2009-08-29 17:04
29
Name:
Anonymous
2009-08-29 17:05
30
Name:
Anonymous
2009-08-29 18:53
>>25-26
/prog/ in a nutshell,
ladies and gentlemen!
Haxus the Unconditional Jump
33
Name:
Anonymous
2010-12-09 4:01