Skip to main content

Posts

Showing posts with the label Containers

Sequence Containers: Vectors

Vectors are sequence containers representing arrays that can change in size. Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. Container properties Sequence Dynamic array Allocator-aware Example : #include #include int main () { std::vector< int > myvector; for ( int i=1; i<=5; i++) myvector.push_back(i); std::cout << "myvector contains:" ; for (std::vector< int >::iterator it = myvector.begin() ; it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n' ; return 0; }

Sequence Containers: Array

Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence. They cannot be expanded or contracted dynamically Another unique feature of array containers is that they can be treated as  tuple  objects Container properties Sequence Contiguous storage (allowing constant time random access to elements) Fixed-size aggregate Example: #include #include int main () {   std::array myarray = { 2, 16, 77, 34, 50 };   std::cout << "myarray contains:";   for ( auto it = myarray.begin(); it != myarray.end(); ++it )     std::cout << ' ' << *it;   std::cout << '\n';   return 0; }