J
john smith
HI, when I try the following code, I get a segfault when compiled with
VC.NET and g++ under cygwin.
#1 vector<int> vi;
#2 vector<int>::iterator ii = vi.begin();
#3 vi.reserve(10);
#4 for(int i = 0; i < 10; ++i) {
#5 *ii++ = 4;
#6 }
#7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
Here is my break down on why this code should work (fill a vector with
numbers, nevermind v syntax, or v.push_back(), or the fill(...) stl
function. I really wanted to know why the use of iterators causes a problem
here).
#1 - v.size is 0;
#2 - initiate a interator and point to v.begin().
#3 - make space for 10 more elements, v.size() is still 0;
#4-6 - loop and assign each space in the vector a number. #5 is where segv
happens.
I was under the assumption that reserve will make space available to add
more element, which the iterator will be able to walk through them. I'd
appreciate any help on this issue.
Smith
VC.NET and g++ under cygwin.
#1 vector<int> vi;
#2 vector<int>::iterator ii = vi.begin();
#3 vi.reserve(10);
#4 for(int i = 0; i < 10; ++i) {
#5 *ii++ = 4;
#6 }
#7 copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
Here is my break down on why this code should work (fill a vector with
numbers, nevermind v syntax, or v.push_back(), or the fill(...) stl
function. I really wanted to know why the use of iterators causes a problem
here).
#1 - v.size is 0;
#2 - initiate a interator and point to v.begin().
#3 - make space for 10 more elements, v.size() is still 0;
#4-6 - loop and assign each space in the vector a number. #5 is where segv
happens.
I was under the assumption that reserve will make space available to add
more element, which the iterator will be able to walk through them. I'd
appreciate any help on this issue.
Smith