top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is size and capacity of a Vector ?

+1 vote
453 views
What is size and capacity of a Vector ?
posted Mar 9, 2016 by Ritika Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

vector::size :

Returns the number of elements currently in the vector(that you have inserted).

It does not differ will different compilers.

vector::size

vector::capacity :

Returns the size of the storage space currently allocated for the vector. It tells the number of elements that can be added to the vector before it is full. As soon as the the elements make the vector full, new larger block of memory will be allocated and the values will be copied to it.
This is always equal or larger than vector::size .

answer Mar 10, 2016 by Manikandan J
+1 vote

Size is the storage space currently allocated for the vector, expressed in terms of elements where as capacity is not necessarily equal to the vector size. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion.

Capacity does not suppose a limit on the size of the vector. When capacity is exhausted and more is needed, it is automatically expanded by the container (reallocating it storage space). The theoretical limit on the size of a vector is given by member max_size.
The capacity of a vector can be explicitly altered by calling member vector::reserve.

answer Mar 10, 2016 by Ashish Kumar Khanna
Similar Questions
+1 vote

I wonder how one could get the compiler to generate the "movdqu" instruction, since the vector extensions always seem to assume that everything will be aligned to 16 byte.
I tried using a packed struct and this dint help much. Of course one can always resort to inline assembly but this should not be necessary

Compile with:

gcc -O2 -S -msse2 testvecs.c

Using built-in specs.

COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i486-linux-gnu/4.7/lto-wrapper
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' 
--with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs 
--enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr 
--program-suffix=-4.7 --enable-shared --enable-linker-build-id 
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext 
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu 
--enable-libstdcxx-debug --enable-libstdcxx-time=yes 
--enable-gnu-unique-object --enable-plugin --enable-objc-gc 
--enable-targets=all --with-arch-32=i586 --with-tune=generic 
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu 
--target=i486-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5)
+1 vote

I am looking for an example with explanation that elaborate the functioning of capacity method.

...