vector problem

A

arnuld

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

======= OUTPUT ===========
/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra test.cpp
test.cpp: In function ‘int main()’:
test.cpp:14: error: void value not ignored as it ought to be
/home/arnuld/programming/cpp $
 
P

Paul

arnuld said:
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.

*iter = ivec.push_back(begin_element++);

The push_back() returns void. Why are you trying to assign a void to an
int? Regardless, that is the error.

Paul
 
P

peter koch

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 :) */

No. You now have a vector with six elements, all being zero.
/* 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++);

push_back returns void. Read the manual.
Instead, I'd simply use a simple loop, pushing back the desired values
(although generate_n is also an option).
}

return 0;

}

======= OUTPUT ===========
/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra test.cpp
test.cpp: In function 'int main()':
test.cpp:14: error: void value not ignored as it ought to be

Please annotate the source code with the error message so we won't
have to count lines (which can be difficult when we need to guess e.g.
the word-wrap that occured).
 
A

Alp Mestan

Paul said:
*iter = ivec.push_back(begin_element++);

The push_back() returns void. Why are you trying to assign a void to an
int? Regardless, that is the error.

Paul
Are you trying to iterate over the vector while filling it ?
push_back automatically pushes ... back.

Maybe you would have liked to write that :

for(/* your stuff */)
{
*iter = begin_element++;
}

?

Alp
 
B

Bo Persson

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
 
J

Jim Langston

arnuld said:
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;
}

======= OUTPUT ===========
/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra test.cpp
test.cpp: In function 'int main()':
test.cpp:14: error: void value not ignored as it ought to be
/home/arnuld/programming/cpp $

You're donig things a bit wrong.

If you want to size your vector first, you don't use push_back, push_back
inserts a new element, increasing the size.

Change the inner workins of your loop to this:
for(std::vector<int>::iterator iter=ivec.begin(); iter != ivec.end();
++iter)
{
*iter = begin_element++;
}
 
T

tom

Probably you want to make it like this:
#include <iostream>
#include <vector>

int main()
{
std::vector<int> ivec;

int begin_element = 10;
for(int i=10; i<=15; i++)
{
ivec.push_back(i);
}
return 0;
}

or

int main()
{
const size_t vectorSize = 6;
std::vector<int> ivec(vectorSize);

int beginElement = 10;
for(std::vector<int>::iterator iter=ivec.begin(); iter!=ivec.end();
iter++)
{
*iter = beginElement++;
}
return 0;
}
 
A

arnuld

peter koch said:
[...SNIP...]
const int v_size = 6;
std::vector<int> ivec(v_size); /* now, i can add any number of elelemnts :) */
No. You now have a vector with six elements, all being zero.

i can not add more elements ?

Please annotate the source code with the error message so we won't
have to count lines (which can be difficult when we need to guess e.g.
the word-wrap that occured).

i apologize for causing the trouble. nobosy ever complained that, youa
re the 1st one. from now i will put my a comment on the line where the
error happened e.g. i will add "this is line number 14, error occured
here". BTW, it is really quite cumbersome to find out the line #14 in
soemone else's code.

thanks
 
J

Jim Langston

arnuld said:
[...SNIP...]
const int v_size = 6;
std::vector<int> ivec(v_size); /* now, i can add any number of
elelemnts :) */
No. You now have a vector with six elements, all being zero.

i can not add more elements ?

yes, you can, just by using ivec.push_back();
But when you use push_back() it just adds it to the end, no reason to worry
about an iterator. I.E.

int begin_element = 10;
for ( int i = begin_element; i < begin_element + 6; ++i )
ivec.push_back( i );

this would push 10, 11, 12, 13, 14, 15 into the vector. With your prevoius
sizing, your vector would wind up being:
0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15
i apologize for causing the trouble. nobosy ever complained that, youa
re the 1st one. from now i will put my a comment on the line where the
error happened e.g. i will add "this is line number 14, error occured
here". BTW, it is really quite cumbersome to find out the line #14 in
soemone else's code.

So, in summary, if you want to change an element, dereference the pointer.
*it = someval;
if you want to add a new element, use pushback
ivec.push_back( someval );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top