<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Datenstruktur für ein Menu]]></title><description><![CDATA[<p>Hi,</p>
<p>in welcher Datenstruktur würdet ihr ein Menu ablegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/271597/datenstruktur-für-ein-menu</link><generator>RSS for Node</generator><lastBuildDate>Sat, 29 Aug 2026 09:22:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/271597.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 31 Jul 2010 15:50:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Datenstruktur für ein Menu on Sat, 31 Jul 2010 15:50:03 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>in welcher Datenstruktur würdet ihr ein Menu ablegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1934456</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1934456</guid><dc:creator><![CDATA[cppfrager]]></dc:creator><pubDate>Sat, 31 Jul 2010 15:50:03 GMT</pubDate></item><item><title><![CDATA[Reply to Datenstruktur für ein Menu on Sat, 31 Jul 2010 15:57:48 GMT]]></title><description><![CDATA[<p>Ich hab mal eins gebaut, weils mir aufn senkel ging, immer wieder das gleiche tippen zu müssen - und weil ich msvc eh gerad auf hab:</p>
<pre><code class="language-cpp">#ifndef H_MY_MENU_20100406012317
#define H_MY_MENU_20100406012317

#include &lt;cassert&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;utility&gt;
#include &lt;vector&gt;

#include &lt;boost/function.hpp&gt;
#include &lt;boost/shared_ptr.hpp&gt;

namespace my
{
	namespace detail
	{
		unsigned count_digits(unsigned int value)
		{
			if(value &gt; 1000000000)  return 10;
			if(value &gt; 100000000)   return 9;
			if(value &gt; 10000000)    return 8;
			if(value &gt; 1000000)     return 7;
			if(value &gt; 100000)      return 6;
			if(value &gt; 10000)       return 5;
			if(value &gt; 1000)        return 4;
			if(value &gt; 100)         return 3;
			if(value &gt; 10)          return 2;
			return 1;
		}
	}

	struct menu
	{
		struct function_t
		{
			function_t()
			{}
			function_t(boost::function0&lt;void&gt; to_call)
			: to_call(new boost::function0&lt;void&gt;(to_call))
			{}

			void operator() () const
			{
				assert(to_call &amp;&amp; &quot;cannot dereference a nullptr&quot;);
				boost::function0&lt;void&gt;&amp; x = *to_call;
				x();
			}
			friend bool operator== (function_t lhs, function_t rhs)
			{
				return lhs.to_call == rhs.to_call;
			}
		private:
			boost::shared_ptr&lt; boost::function0&lt;void&gt; &gt; to_call;
		};

		typedef std::string text_t;
		typedef std::pair&lt;text_t, function_t&gt; pair_t;
		typedef std::vector&lt;pair_t&gt; container_t;
		typedef container_t::size_type size_t;

		void add(const text_t&amp; title, function_t function)
		{
			items.push_back( pair_t(title, function) );
		}
		void skip_nr()
		{
			items.push_back( pair_t() );
		}
		void skip_till(size_t next_number)
		{
			while(items.size() &lt; next_number-1)
			{
				skip_nr();
			};
		}

		void print(std::ostream&amp; stream) const
		{
			const size_t size = items.size();
			const unsigned length = detail::count_digits(size);

			size_t nr = 1;
			bool skipped_last = false;
			for(container_t::const_iterator i(items.begin()), e(items.end()); i != e; ++i, ++nr)
			{
				if(!i-&gt;first.empty())
				{
					stream &lt;&lt; '[' &lt;&lt; std::setw( static_cast&lt;std::streamsize&gt;(length) ) &lt;&lt; nr &lt;&lt; ']'
						&lt;&lt; '\t' &lt;&lt; i-&gt;first;
					skipped_last = false;
				}
				if(!skipped_last)
				{
					stream &lt;&lt; std::endl;
					skipped_last = true;
				}
			}

			stream &lt;&lt; std::endl;
		}

		function_t operator() (std::istream&amp; in, std::ostream&amp; out)
		{
			for(;;)
			{
				size_t nr;
				in &gt;&gt; nr;
				--nr;

				if(nr &lt; items.size())
				{
					pair_t&amp; choice = items[nr];
					if(!choice.first.empty())
						return choice.second;
				}
				out &lt;&lt; &quot;wrong number - again&quot; &lt;&lt; std::endl;

				std::string rubbish;
				std::getline(in, rubbish);
				in.clear();
			}
		}

	private:
		container_t items;
	};

}

#endif //#ifndef H_MY_MENU_20100406012317
</code></pre>
<p>benutzung:</p>
<pre><code class="language-cpp">my::menu::function_t put_in_store        = boost::bind(hwp::source::features::put_in_store, the_user, &amp;store_house, boost::ref(logger));
		my::menu::function_t remove_from_store   = boost::bind(hwp::source::features::remove_from_store, the_user, &amp;store_house, boost::ref(logger));
		my::menu::function_t save_to_db          = boost::bind(hwp::source::features::save_to_db, the_user, &amp;store_house, boost::ref(logger));
		my::menu::function_t show_store_house    = boost::bind(hwp::source::features::show_store_house, the_user, &amp;store_house, boost::ref(logger));
		my::menu::function_t show_single_storing = boost::bind(hwp::source::features::show_single_storing, the_user, &amp;store_house, boost::ref(logger));
		my::menu::function_t add_user            = boost::bind(hwp::source::features::add_user, the_user, boost::ref(std::cin), boost::ref(std::cout), boost::ref(logger));
		my::menu::function_t abort               = boost::bind(hwp::source::features::abort, the_user, boost::ref(logger));

		my::menu menu;
		menu.add(&quot;Waren einlagern&quot;, put_in_store);
		menu.add(&quot;Waren auslagern&quot;, remove_from_store);
		menu.add(&quot;Speichern des Lagerzustands&quot;, save_to_db);
		menu.skip_till(5);
		menu.add(&quot;Etwas ueber einen bestimmten Lagerplatz erfahren&quot;, show_store_house);
		menu.add(&quot;Einlager-Priorität eines Produktes herausfinden&quot;, show_single_storing);
		menu.skip_till(7);
		if(the_user-&gt;is_admin())
			menu.add(&quot;Nutzer hinzufügen&quot;, add_user);
		menu.skip_till(9);
		menu.add(&quot;Beenden&quot;, abort);

		menu.print(std::cout);
		my::menu::function_t to_do = menu(std::cin, std::cout);
		to_do();

		if(to_do == abort) /*beenden, vll noch mal speichern etc.*/
 /*...*/
</code></pre>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1934462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1934462</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sat, 31 Jul 2010 15:57:48 GMT</pubDate></item><item><title><![CDATA[Reply to Datenstruktur für ein Menu on Sat, 31 Jul 2010 16:00:42 GMT]]></title><description><![CDATA[<p>vielen lieben dank, da habe ich erstmal was zu verdauen. witzig, ich habe auch gerade vc auf und probiere mit vectoren rum <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1934463</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1934463</guid><dc:creator><![CDATA[cppfrager]]></dc:creator><pubDate>Sat, 31 Jul 2010 16:00:42 GMT</pubDate></item><item><title><![CDATA[Reply to Datenstruktur für ein Menu on Sat, 31 Jul 2010 23:39:11 GMT]]></title><description><![CDATA[<p>Schau dir mal das Composite Design Pattern an. Die verwendete Datenstruktur ist ein Implementierungsdetail, das hängt von weiteren Anforderungen (welche Operationen wie oft benötigt, Validität der Iteratoren, Performance) ab.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1934587</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1934587</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 31 Jul 2010 23:39:11 GMT</pubDate></item></channel></rss>