Question: I am able to setup a listview using custom adapter which shows correct output.each list item also has an gradient style see drawable.xml below how do I change the item background to some other gradient on tapping any of them. can I set different gradients for each of the items.? 

```
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
<gradient 
android:startColor="#FFFAFAFA" 
android:centerColor="#FFFFFFFF" 
android:endColor="#FFFAFAFA" 
android:angle="90"/>
</shape>

```

My files...

activity\_main.xml....Main xml file

MainActivity.java....Main activity

Item.java.... item selector method

ItemAdaptor.... custom adapter

list\_item.xml..... item (textView) styling

Answer: 
With a vector iterators do no offer any real advantage. The syntax is uglier, longer to type and harder to read.

Iterating over a vector using iterators is not faster and is not safer (actually if the vector is possibly resized during the iteration using iterators will put you in big troubles).

The idea of having a generic loop that works when you will change later the container type is also mostly nonsense in real cases. Unfortunately the dark side of a strictly typed language without serious typing inference (a bit better now with C++11, however) is that you need to say what is the type of everything at each step. If you change your mind later you will still need to go around and change everything. Moreover different containers have very different trade-offs and changing container type is not something that happens that often.

The only case in which iteration should be kept if possible generic is when writing template code, but that (I hope for you) is not the most frequent case.

The only problem present in your explicit index loop is that `size` returns an unsigned value (a design bug of C++) and comparison between signed and unsigned is dangerous and surprising, so better avoided. If you use a decent compiler with warnings enabled there should be a diagnostic on that.

Note that the solution is not to use an unsiged as the index, because arithmetic between unsigned values is also apparently illogical (it's modulo arithmetic, and `x-1` may be bigger than `x`). You instead should cast the size to an integer before using it.
It **may** make some sense to use unsigned sizes and indexes (paying a LOT of attention to every expression you write) only if you're working on a 16 bit C++ implementation ([16 bit was the reason for having unsigned values in sizes](https://stackoverflow.com/questions/10168079/why-is-size-t-unsigned)).

As a typical mistake that unsigned size may introduce consider:

```
void drawPolyline(const std::vector<P2d>& points)
{
    for (int i=0; i<points.size()-1; i++)
        drawLine(points[i], points[i+1]);
}

```

Here the bug is present because if you pass an empty `points` vector the value `points.size()-1` will be a huge positive number, making you looping into a segfault.
A working solution could be

```
for (int i=1; i<points.size(); i++)
    drawLine(points[i - 1], points[i]);

```

but I personally prefer to always remove `unsinged`-ness with `int(v.size())`.

PS: If you really don't want to think by to yourself to the implications and simply want an expert to tell you then consider that a quite a few world recognized C++ experts agree and expressed opinions on that [unsigned values are a bad idea except for bit manipulations](https://www.youtube.com/watch?v=Puio5dly9N8&t=2560).

Discovering the ugliness of using iterators in the case of iterating up to second-last is left as an exercise for the reader.
Iterators are first choice over `operator[]`. C++11 provides `std::begin()`, `std::end()` functions. 

As your code uses just `std::vector`, I can't say there is much difference in both codes, however, `operator []` may not operate as you intend to. For example if you use map, `operator[]` will insert an element if not found.

Also, by using `iterator` your code becomes more portable between containers. You can switch containers from `std::vector` to `std::list` or other container freely without changing much if you use iterator such rule doesn't apply to `operator[]`.