1

In the C++ Primer book, Chapter (3), there is the following for-loop that resets the elements in the vector to zero.

vector<int> ivec; //UPDATE: vector declaration
for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix)
ivec[ix] = 0;

Is the for-loop really assigning 0 values to the elements, or do we have to use the push_back function?

So, is the following valid?

ivec[ix] = ix;

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 1
    Have you tried it? Because that would have answered your first question. If you actually write small programs to try such things you will learn *much* faster. – Björn Pollex Jan 31 '11 at 11:05
  • The primer is showing you looping. In practical terms there are faster ways to assign all the elements to 0. – CashCow Jan 31 '11 at 11:38
  • The book mentions that `vector ivec;` is an empty vector and subscripts can only be used to *fetch existing elements* – Simplicity Jan 31 '11 at 17:50

5 Answers5

5

Is the for-loop really assigning 0 values to the elements? Or, we have to use the push_back finction?

ivec[ix] =0 updates the value of existing element in the vector, while push_back function adds new element to the vector!

So, is the following valid?
ivec[ix] = ix;

It is perfectly valid IF ix < ivec.size().

It would be even better if you use iterator, instead of index. Like this,

int ix = 0;
for(vector<int>::iterator it = ivec.begin() ; it != ivec.end(); ++it)
{ 
     *it = ix++; //or do whatever you want to do with "it" here!
}

Use of iterator with STL is idiomatic. Prefer iterator over index!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • The change you propose certainly is better style and makes the code safer, but the above code does work, as long you really only increment `ix` by one in each iteration. – Björn Pollex Jan 31 '11 at 11:04
  • 1
    ... and nothing else messes with the vector. Yes, `!=` works, but why play with fire? – Christian Severin Jan 31 '11 at 11:08
  • equally though if you change container type then < may not work. != is pretty idiomatic for coding dealing with iterators as the point is usually to seperate the algorithm from the data structure via iterators – jk. Jan 31 '11 at 11:26
  • @jk: *"!= is pretty idiomatic for coding dealing with iterators"*...if iterators. but the OP is using index, not iterator! – Nawaz Jan 31 '11 at 11:27
  • doh, yes - the whole snippet seemed a little strange as it would naturally be a std::fill but then it is an out of context pedagogic example – jk. Jan 31 '11 at 11:36
  • @Nawaz. But I think `ivec[ix] = ix;` is intented to add new values to the vector, and thus invalid, isn't it? Thanks – Simplicity Jan 31 '11 at 17:43
  • @user588855: No, `ivec[ix] = ix` doesn't add new value to the vector. It only updates the value at index `ix`. So yes, what you were thinking, is wrong. :-) – Nawaz Jan 31 '11 at 17:58
2

Yes, you can use the square brackets to retrieve and overwrite existing elements of a vector. Note, however, that you cannot use the square brackets to insert a new element into a vector, and in fact indexing past the end of a vector leads to undefined behavior, often crashing the program outright.

To grow the vector, you can use the push_back, insert, resize, or assign functions.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

Using the array brackets the vector object acts just like any other simple array. push_back() increases its length by one element and sets the new/last one to your passed value.

Mario
  • 35,726
  • 5
  • 62
  • 78
0

The purpose of this for loop is to iterate through the elements of the vector. Starting at element x (when ix is 0) up to the last element (when ix is ivec.size() -1).

On each iteration the current element of the vector is set to 9. This is what the statement

ivec[ix] = 0;

does. Putting

ivec[ix] = ix;

in the for loop would set all the elements of the vector to their position in the vector. i.e, the first element would have a value of zero (as vectors start indexing from 0), the second element would have a value of 1, and so on and so forth.

Varun Madiath
  • 3,152
  • 4
  • 30
  • 46
0

Yes, assuming ix is a valid index, most likely: you have a vector of int though and the index is size_type. Of course you may want to purposely store -1 sometimes to show an invalid index so the conversion of unsigned to signed would be appropriate but then I would suggest using a static_cast.

Doing what you are doing (setting each value in the vector to its index) is a way to create indexes of other collections. You then rearrange your vector sorting based on a predicte of the other collection.

Assuming that you never overflow (highly unlikely if your system is 32 bits or more) your conversion should work.

CashCow
  • 30,981
  • 5
  • 61
  • 92