tags:

views:

161

answers:

4

While using the vector why do we sometime use the operator[] like homework[mid] but other times use homework.begin(). Also, homework.end() not homework[end], but that's like begin. Is it just accessing the elements differently? It's more confusing this way, don't you agree?

+2  A: 

vector::operator[] retrieves the Nth element of the vector. Such an operator is defined only for select STL container classes.

vector.end() is a method returning an iterator. Iterators are special entities for working with STL containers, vector included. vector::end() points onto the element immediately following the last element of the vector - it's often treated as a value to campare the iterator against to determine whether the whole container has been traversed.

sharptooth
+1  A: 

begin() and end() are member function of vector which return an iterator. This iterator can be dereferenced to get the value. Similarly, you have operator[] which takes an index and directly returns the value stored at that position.

Naveen
+2  A: 

It's the same analogy as with C arrays...

int a[length_a]

"begin/end" version:

int* begin = a;
int* end = a+length_a;
while (p<e) {
   printf("%d", *p);
   ++p;
}

Note how "end" points one element beyond the array! It's exactly same as vector::end (think about it as a stop condition).

"[]" version:

for (int i=0; i<length_a; ++i) {
   printf("%d", a[i]);
}

'[]' summary:

  • +compatible with C arrays, efficient syntax

  • -not available for non-random access containers ( for example: changing to "list" will be difficult)

my POV advice:

  • use [] for random access or as an 'easy' upgrade to c array

  • use begin/end when traversing whole content sequentially

+1, the anology is helpful. You might want to reword "It's exactly the same as vector::end", as it might be easy to confuse an iterator with a pointer. It's more accurate to say "It's exactly analogous to vector::end".
sheepsimulator
A: 

They do different things. operator[n] returns a reference a reference to the nth element. begin() and end() returns iterators pointing to the beginning and end of the container. If you need to iterate over the container, then using iterators is hardly "more confusing". It's much much simpler.

Let's say you have a vector v:

std::vector<int> v;
v.push_back(1);
v.push_back(7);
v.push_back(42);
v.push_back(3);

if you want to simply reference an element, you have these options:

v[2];
*v.begin()+2;

So here, operator[] is simpler. But if you want to iterate over the container, perhaps to print out the contents, you have these options:

for (int i = 0; i < v.size(); ++i){
  std::cout << v[i];
}

std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout));

Suddenly, the iterators saved us a lot of work. We no longer have to make a loop.

Or let's say you wanted to find the largest element:

int max = 0;
for (int i = 0; i < v.size(); ++i){
  if (v[i] > max) { max = v[i]; }
}

std::max_element(v.begin(), v.end());

or maybe you want to sort the vector. I won't even bother writing out my own sorting algorithm using operator[], but here's the iterator version:

std::sort(v.begin(), v.end());

That's why begin(), end() are useful.

jalf