Re: std::vector's reserve(), erase() and clear()

Tech-Archive recommends: Fix windows errors by optimizing your registry




Alex Vinokur wrote:
> ====== foo.cpp ======
> #include <vector>
> #include <iostream>
> using namespace std;
>
> #define SHOW cout << "capacity = " << v.capacity() << "\t size = " << v.size() << endl;
>
> int main()
> {
> vector<int> v;
> SHOW;
> v.reserve(100);
> SHOW;
> v.resize(100);
> SHOW;
> v.erase(v.end() - 1);
> SHOW;
> v.clear();
> SHOW;
>
> return 0;
>
> }
>
>
> =====================
>
> 1. Output for GNU g++ 3.4.4, GNU gpp 4.0.1, Bolrand C++ 5.5.1, Digital Mars 8.38n
> ---------------------------
> capacity = 0 size = 0
> capacity = 100 size = 0
> capacity = 100 size = 100
> capacity = 100 size = 99
> capacity = 100 size = 0
> ---------------------------
>
> 2. Output for Microsoft C++ 13.00.9466
> ---------------------------
> capacity = 0 size = 0
> capacity = 100 size = 0
> capacity = 100 size = 100
> capacity = 100 size = 99
> capacity = 0 size = 0
> ---------------------------
>
> We can see that capacity == 0 after clear() for Microsoft C++.
> Is it correct behavior?

AFAIK it's unspecified by the standard, so the implementation is free
to do whatever she wants.

Arnaud
MVP - VC

.



Relevant Pages

  • Re: vector : reserve(LONG_MAX)
    ... "Alex Vinokur" wrote in message ... > using namespace std; ... > int main ... Peter van Merkerk ...
    (comp.lang.cpp)
  • Re: vector : reserve(LONG_MAX)
    ... Alex Vinokur wrote: ... > using namespace std; ... > int main ... Dinkumware, Ltd. ...
    (comp.lang.cpp)
  • Re: Program aborted; new doesnt return NULL
    ... Alex Vinokur wrote: ... > using namespace std; ... > int main ... If you want 'new' to return NULL instead of throwing an exception, ...
    (comp.lang.cpp)
  • Re: second largest element in an array
    ... I compile my prorams in VC++. ... using namespace std; ... int main ... Prev by Date: ...
    (comp.lang.c)
  • If .....
    ... using namespace std; ... int main ... Why does this fragment of code generate the following error! ... Prev by Date: ...
    (microsoft.public.vc.language)