{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# STL Associative Containers and Iterators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the previous post, we explored two of the STL's sequence containers:\n", "\n", "- **vector**\n", "- **deque**\n", "\n", "These containers are ideally suited for situations where we need to keep track of an ordered list of elements. However, representing dta in ordered lists is not optimal in many applications. \n", "\n", "In this installment of **Play interactively with C++**, we will explore four new STL container classes:\n", "- **map**\n", "- **set**\n", "- **multimap**\n", "- **multiset**\n", "\n", "that provide new abstractions for storing data. These containers allow us to ask different questions of our data sets and make it possible to write programs to solve increasingly complex problems." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sets are a type of associative containers in which each element has to be unique. The **set** container represents an arbitrary, unordered collection of elements and has good support for the following operations:\n", "\n", "- Adding elements to the collection.\n", "- Removing elements from the collection\n", "- Determining whether a particular element is in the collection.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Creating a set of integers**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# include \n", "# include " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "std::set myset;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Insert elements in random order**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "myset.insert(40);\n", "myset.insert(5);\n", "myset.insert(78); \n", "myset.insert(56);\n", "myset.insert(100);" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "myset.insert(100);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Traversing Containers with Iterators**\n", "\n", "At a high level, **an iterator** is like a cursor in a text editor. Like a cursor, an iterator has a well defined position inside a container, and can move from one character to the next. Also like a cursor, an iterator can be used to read or write a range of data one element at a time." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Declare iterator to a set**" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "std::set ::iterator itr;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This line of code creates an object of type `std::set ::iterator`, an iterator variable named `itr` that can traverse a `std::set `. Note that this iterator can only iterate over a `std::set ." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\t5\t40\t56\t78\t100\n" ] } ], "source": [ "for(itr = myset.begin(); itr != myset.end(); ++itr){\n", " std::cout << \"\\t\" << *itr;\n", "}\n", "std::cout << std::endl;" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(unsigned long) 5\n" ] } ], "source": [ "// Get number of elements in the set\n", "myset.size()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(bool) false\n" ] } ], "source": [ "// Check whether the set is empty or not\n", "myset.empty()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(std::less) @0x5507760\n" ] } ], "source": [ "// Return object that determines how elements in the set are\n", "// ordered ('<' by default)\n", "myset.key_comp()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(std::less) @0x54baf50\n" ] } ], "source": [ "myset.value_comp()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(const int) 40\n" ] } ], "source": [ "// Get an iterator to the lower bound\n", "*myset.lower_bound(40)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(const int) 56\n" ] } ], "source": [ "*myset.upper_bound(40)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "xeus C++14", "language": "", "name": "xeus-cling-cpp14" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "" }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "toc_cell": true, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }