M
ma740988
Consider the source snippet
int main()
{
std::vector<double> vec_push( 0x10 );
size_t const SZ = vec_push.size();
std::cout << vec_push.size() << std::endl;
if ( SZ != 0x10 ) {
cout << " something's amiss " << endl;
return 0;
}
vec_push.reserve(0x10000); // for test ..
for (size_t jdx(0); jdx < SZ; ++jdx)
{
vec_push.push_back( jdx );
std::cout << " JDX " << jdx << std::endl;
std::cout << " vec_push.size() " << vec_push.size() <<
std::endl;
}
std::cout << vec_push.size() << std::endl;
}
------ The results:
16
JDX 0
vec_push.size() 17
JDX 1
vec_push.size() 18
JDX 2
vec_push.size() 19
JDX 3
vec_push.size() 20
JDX 4
vec_push.size() 21
JDX 5
vec_push.size() 22
JDX 6
vec_push.size() 23
JDX 7
vec_push.size() 24
JDX 8
vec_push.size() 25
JDX 9
vec_push.size() 26
JDX 10
vec_push.size() 27
JDX 11
vec_push.size() 28
JDX 12
vec_push.size() 29
JDX 13
vec_push.size() 30
JDX 14
vec_push.size() 31
JDX 15
vec_push.size() 32
32
-------
At issue: The results from the program suggest that - with each call
to push_back the vector re-allocates. A bit of a mystery since I've
already allocated enough space with reserve
This compiler ( which unfortunately I'm stuck with ) is gcc 2.96 -
which I suspect is sub par. The question then becomes: From the looks
of it, I suspect what it amounts to is - the implementation 'is what it
is' and my choice the becomes a different container - perhaps or use
operator []?
i.e.
vec_push[ jdx ] = jdx;
In that regard, the result now becomes:
16
JDX 0
vec_push.size() 16
JDX 1
vec_push.size() 16
JDX 2
vec_push.size() 16
JDX 3
vec_push.size() 16
JDX 4
vec_push.size() 16
JDX 5
vec_push.size() 16
JDX 6
vec_push.size() 16
JDX 7
vec_push.size() 16
JDX 8
vec_push.size() 16
JDX 9
vec_push.size() 16
JDX 10
vec_push.size() 16
JDX 11
vec_push.size() 16
JDX 12
vec_push.size() 16
JDX 13
vec_push.size() 16
JDX 14
vec_push.size() 16
JDX 15
vec_push.size() 16
16
int main()
{
std::vector<double> vec_push( 0x10 );
size_t const SZ = vec_push.size();
std::cout << vec_push.size() << std::endl;
if ( SZ != 0x10 ) {
cout << " something's amiss " << endl;
return 0;
}
vec_push.reserve(0x10000); // for test ..
for (size_t jdx(0); jdx < SZ; ++jdx)
{
vec_push.push_back( jdx );
std::cout << " JDX " << jdx << std::endl;
std::cout << " vec_push.size() " << vec_push.size() <<
std::endl;
}
std::cout << vec_push.size() << std::endl;
}
------ The results:
16
JDX 0
vec_push.size() 17
JDX 1
vec_push.size() 18
JDX 2
vec_push.size() 19
JDX 3
vec_push.size() 20
JDX 4
vec_push.size() 21
JDX 5
vec_push.size() 22
JDX 6
vec_push.size() 23
JDX 7
vec_push.size() 24
JDX 8
vec_push.size() 25
JDX 9
vec_push.size() 26
JDX 10
vec_push.size() 27
JDX 11
vec_push.size() 28
JDX 12
vec_push.size() 29
JDX 13
vec_push.size() 30
JDX 14
vec_push.size() 31
JDX 15
vec_push.size() 32
32
-------
At issue: The results from the program suggest that - with each call
to push_back the vector re-allocates. A bit of a mystery since I've
already allocated enough space with reserve
This compiler ( which unfortunately I'm stuck with ) is gcc 2.96 -
which I suspect is sub par. The question then becomes: From the looks
of it, I suspect what it amounts to is - the implementation 'is what it
is' and my choice the becomes a different container - perhaps or use
operator []?
i.e.
vec_push[ jdx ] = jdx;
In that regard, the result now becomes:
16
JDX 0
vec_push.size() 16
JDX 1
vec_push.size() 16
JDX 2
vec_push.size() 16
JDX 3
vec_push.size() 16
JDX 4
vec_push.size() 16
JDX 5
vec_push.size() 16
JDX 6
vec_push.size() 16
JDX 7
vec_push.size() 16
JDX 8
vec_push.size() 16
JDX 9
vec_push.size() 16
JDX 10
vec_push.size() 16
JDX 11
vec_push.size() 16
JDX 12
vec_push.size() 16
JDX 13
vec_push.size() 16
JDX 14
vec_push.size() 16
JDX 15
vec_push.size() 16
16