Re: vector problem
arnuld wrote:
:: what i want: i want to add 6 elements to vector, beginning at 10.
:: i mean vector needs to have 6 elements in this order: 10, 11, 12,
:: 13, 14, 15.
::
:: what i get: error :(
::
::
:: #include <iostream>
:: #include <vector>
::
:: int main()
:: {
:: const int v_size = 6;
:: std::vector<int> ivec(v_size); /* now, i can add any number of
:: elelemnts :) */
::
:: /* adding elelemnts to vector */
:: /* std::vector<int>::size_type begin_vector = 10; */
:: int begin_element = 10;
:: for(std::vector<int>::iterator iter=ivec.begin(); iter !=
:: ivec.end(); ++iter) {
:: *iter = ivec.push_back(begin_element++);
:: }
::
:: return 0;
:: }
::
I think you a trying too hard, or something. :-)
push_back is used to add an element, by increasing the size of the
vector by one and copying the parameter into this new object.
If you already have a vector of the proper size (as you do), you can
just assign new values to the existing elements:
*iter = begin_element++;
You can't do both at the same time!
The error message you get is about push_back returning void, which
cannot be assigned to *iter. That's true, but it doesn't tell us what
the real problem is.
Bo Persson