Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ STL

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.

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

Name: Anonymous 2009-08-28 13:20

Back to summer reading for CS101, please.

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.

Name: Haxus the Correct 2009-08-28 16:42

Name: Haxus the Correct 2009-08-28 16:47

Name: Haxus the Correct++ 2009-08-28 17:03

>>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)."

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?

Name: Anonymous 2009-08-28 17:22

>>8

Quoting myself. Actually both should end up as the same after optimizing.

Name: Haxus the Resigned 2009-08-28 17:36

Haxus the Resigned

Name: Sagesus the Unbumping 2009-08-28 18:26

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/!

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

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;
};

Name: Anonymous 2009-08-29 5:07

>>9

I

>>14

was right

>>13

motherfucker

Name: Anonymous 2009-08-29 5:09

Any properly optimized STL implementation will be written as you would in C without sepples garbage
fixd

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.

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.

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.

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.

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.

Name: Anonymous 2009-08-29 12:12

WE CONJURE THE SPIRIT OF THE COMPUTER WITH OUR SPELLS

Name: Anonymous 2009-08-29 12:55

>>22
True for all cases.

Name: Anonymous 2009-08-29 16:00

I CONJURE THE SPIRITS OF UNSATISFYING CASUAL BUTTSEX WITH MY ANUS

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.

Name: Anonymous 2009-08-29 16:17

>>25
This is why people use C++.

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.

Name: Anonymous 2009-08-29 17:04

Name: Anonymous 2009-08-29 17:05

Name: Anonymous 2009-08-29 18:53

>>25-26
/prog/ in a nutshell, ladies and gentlemen!

Name: Haxus the Unconditional Jump 2009-08-29 19:26

Haxus the Unconditional Jump

Name: Anonymous 2010-12-09 4:01


Don't change these.
Name: Email:
Entire Thread Thread List