<?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[binary_search ohne Value oder BindIterator]]></title><description><![CDATA[<p>Gegeben ein sortierter vector&lt;shared_ptr&lt;BigObject&gt;&gt; v. BigObject enthält ein paar MB Daten sowie einen string path. Der vector ist nach BigObject::path sortiert. Ziel: Finde heraus, ob der vector v ein BigObject mit gegebenem string p enthält. Die Lösung ist irgendwie sowas:</p>
<pre><code>return binary_search(cbegin(v), cend(v), ???, [](shared_ptr&lt;BigObject&gt; &amp;bo1, shared_ptr&lt;BigObject&gt; &amp;bo2){return bo1.path &lt; bo2.path;});
</code></pre>
<p>Ich möchte es vermeiden ein shared_ptr&lt;BigObject&gt; zu erstellen und dort path setzen nur um damit binary_search aufrufen zu können. Einen Iterator bauen, der aus dem shared_ptr&lt;BigObject&gt; den pfad rausfummelt und auf dem vector-Iterator basiert erscheint mir unnötig kompliziert. Alternativ ginge std::find, aber das hat dasselbe Problem. Geht das irgendwie einfacher? Sowas wie BindIterator&lt;VectorIterator, VectorIterator::path&gt; oder so?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/324800/binary_search-ohne-value-oder-binditerator</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 10:51:33 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/324800.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Apr 2014 11:11:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 11:11:10 GMT]]></title><description><![CDATA[<p>Gegeben ein sortierter vector&lt;shared_ptr&lt;BigObject&gt;&gt; v. BigObject enthält ein paar MB Daten sowie einen string path. Der vector ist nach BigObject::path sortiert. Ziel: Finde heraus, ob der vector v ein BigObject mit gegebenem string p enthält. Die Lösung ist irgendwie sowas:</p>
<pre><code>return binary_search(cbegin(v), cend(v), ???, [](shared_ptr&lt;BigObject&gt; &amp;bo1, shared_ptr&lt;BigObject&gt; &amp;bo2){return bo1.path &lt; bo2.path;});
</code></pre>
<p>Ich möchte es vermeiden ein shared_ptr&lt;BigObject&gt; zu erstellen und dort path setzen nur um damit binary_search aufrufen zu können. Einen Iterator bauen, der aus dem shared_ptr&lt;BigObject&gt; den pfad rausfummelt und auf dem vector-Iterator basiert erscheint mir unnötig kompliziert. Alternativ ginge std::find, aber das hat dasselbe Problem. Geht das irgendwie einfacher? Sowas wie BindIterator&lt;VectorIterator, VectorIterator::path&gt; oder so?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392321</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392321</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Wed, 02 Apr 2014 11:11:10 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 11:18:51 GMT]]></title><description><![CDATA[<p>Mit find_if gehts.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392322</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392322</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Wed, 02 Apr 2014 11:18:51 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 11:22:37 GMT]]></title><description><![CDATA[<p>Ich denke, die eifnachste Möglichkeit wäre, dass du den Path relativ easy zugänglich machst (z.B. ein einfacher Getter) und ein Dummy BigObject erzeugst, was nur eben den path gesetzt hat.</p>
<p>Zur Not kapselst du das noch weiter ab nach folgendem Motto:</p>
<pre><code>// Anstelle von
class BigObject
{
    // xXx MB daten
    std::string path;
    // ...
};
std::vector&lt;std::shared_ptr&lt;BigObject&gt;&gt; vec;

// eher sowas:
class BigObject
{
    class RealBigObject
    {
        // xXx MB Daten
    };
    std::string path;
    std::unique_ptr&lt;RealBigObject&gt; data;
};
std::vector&lt;std::shared_ptr&lt;BigObject&gt;&gt; vec;
</code></pre>
<p>Du trennst halt die Daten vom path ab.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392323</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392323</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 02 Apr 2014 11:22:37 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 16:58:08 GMT]]></title><description><![CDATA[<p>nwp3 schrieb:</p>
<blockquote>
<p>Mit find_if gehts.</p>
</blockquote>
<p>Ist stark suboptimal, bei einem sortieren Container.</p>
<p>Es gibt eine sehr einfache Lösung für dieses Problem</p>
<pre><code>binary_search( cbegin(v), cend(v), p, [] (string const&amp; val, shared_ptr&lt;BigObject&gt; const&amp; bo2) {return val &lt; bo2.path;} ); 
// Du nutzt ja scheinbar C++14? Warum kein generisches Lambda
</code></pre>
<p>Der Trick ist, dass der Comparator als erstes Argument nur einen Typen haben muss, in den der Typ vom zusuchenden Argument konvertierbar sein muss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392409</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 02 Apr 2014 16:58:08 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 19:45:27 GMT]]></title><description><![CDATA[<p>Ich benutze das <a href="http://msdn.microsoft.com/en-us/library/hh567368.aspx" rel="nofollow">leicht kastrierte C++11 von VS2013</a>, das kann keine generischen Lambdas. Ich will clang <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<pre><code>#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;memory&gt;
#include &lt;algorithm&gt;

using namespace std;

struct BigObject{
	BigObject(int i) : bigdata(i), path(to_string(i)){}
	string path;
	int bigdata;
	operator const string&amp;() const{ //nur zum testen
		return path;
	}
};

int main(){
	vector&lt;shared_ptr&lt;BigObject&gt;&gt; v;
	for (int i = 0; i &lt; 10; i++)
		v.push_back(make_shared&lt;BigObject&gt;(i));
	string p(to_string(5));
	binary_search(cbegin(v), cend(v), p, [](string const&amp; val, shared_ptr&lt;BigObject&gt; const&amp; bo2) {return val &lt; bo2-&gt;path; });
}
</code></pre>
<pre><code>Error: cannot convert argument 1 from 'const std::shared_ptr&lt;BigObject&gt;' to 'const std::string &amp;'
</code></pre>
<p>Die Fehlermeldung hatte ich erwartet. Ich weiß auch nicht wie ich shared_ptr&lt;BigObject&gt; beibringen soll sich in einen string konvertieren zu lassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392434</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392434</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Wed, 02 Apr 2014 19:45:27 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 19:53:30 GMT]]></title><description><![CDATA[<p>Geht sowas wie</p>
<pre><code class="language-cpp">struct comparator {
  bool operator&lt;(string const&amp; val, shared_ptr&lt;BigObject&gt; const&amp; bo2) { ... }
  bool operator&lt;(shared_ptr&lt;BigObject&gt; const&amp; bo1, string const&amp; val) { ... }
};

binary_search(cbegin(v), cend(v), p, comparator{});
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392437</guid><dc:creator><![CDATA[lamb]]></dc:creator><pubDate>Wed, 02 Apr 2014 19:53:30 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 20:04:33 GMT]]></title><description><![CDATA[<p>Ups - ich habe den <a href="http://en.cppreference.com/w/cpp/algorithm/binary_search" rel="nofollow">Referenz-Artikel</a> nicht ganz zuende gelesen, mein Fehler:</p>
<blockquote>
<p>Update type requirements? The types <code>Type1</code> and <code>Type2</code> must be such that an object of type <code>T</code> can be implicitly converted to either type, <em>and</em> such that an object of type <code>ForwardIt</code> can be dereferenced and then implicitly converted to either type. [..]</p>
</blockquote>
<p>Dann wird das wohl nicht ganz so hübsch...</p>
<p>Eigener Vergleichs-Funktor? Mein Vorposter hat es ja vorgemacht:</p>
<pre><code>struct Comp
    {
		// (1) nicht nötig: bool operator()( shared_ptr&lt;BigObject&gt; const&amp; a, shared_ptr&lt;BigObject&gt; const&amp; b ) { return a-&gt;path &lt; b-&gt;path; }
		/* (2) */bool operator()( std::string const&amp; val, shared_ptr&lt;BigObject&gt; const&amp; b ) { return val &lt; b-&gt;path; }
		/* (3) */bool operator()( shared_ptr&lt;BigObject&gt; const&amp; b, std::string const&amp; val ) { return b-&gt;path &lt; val; }
    };

    binary_search(begin(v), end(v), p, Comp{});
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2392439</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392439</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 02 Apr 2014 20:04:33 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 19:59:14 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<pre><code>bool operator()( shared_ptr&lt;BigObject&gt; const&amp; a, shared_ptr&lt;BigObject&gt; const&amp; b ) { return a-&gt;path &lt; b-&gt;path; }
</code></pre>
</blockquote>
<p>Sicher, dass es den braucht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392442</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392442</guid><dc:creator><![CDATA[lamb]]></dc:creator><pubDate>Wed, 02 Apr 2014 19:59:14 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 20:03:17 GMT]]></title><description><![CDATA[<p>lamb schrieb:</p>
<blockquote>
<p>Arcoth schrieb:</p>
<blockquote>
<pre><code>bool operator()( shared_ptr&lt;BigObject&gt; const&amp; a, shared_ptr&lt;BigObject&gt; const&amp; b ) { return a-&gt;path &lt; b-&gt;path; }
</code></pre>
</blockquote>
<p>Sicher, dass es den braucht?</p>
</blockquote>
<p>Edit: <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
<p>Nächstes mal gucke ich gleich in den Standard:</p>
<blockquote>
<p>The elements e of [first,last) are partitioned with respect to the expressions [...] <code>comp(e, value)</code> and <code>!comp(value, e)</code> .</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2392444</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392444</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 02 Apr 2014 20:03:17 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Wed, 02 Apr 2014 20:08:17 GMT]]></title><description><![CDATA[<p>Extrem coole Idee, das geht sogar <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /><br />
Vielleicht sind Lambdas doch kein Allheilmittel <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2392445</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392445</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Wed, 02 Apr 2014 20:08:17 GMT</pubDate></item><item><title><![CDATA[Reply to binary_search ohne Value oder BindIterator on Thu, 03 Apr 2014 06:55:31 GMT]]></title><description><![CDATA[<p>Alternative:</p>
<pre><code>struct Delegate{
	Delegate(const string &amp;str) : s(str){}
	Delegate(const shared_ptr&lt;BigObject&gt; &amp;b) : s(b-&gt;path){}
	const string &amp;s;
};

int main(){
	vector&lt;shared_ptr&lt;BigObject&gt;&gt; v;
	for (int i = 0; i &lt; 10; i++)
		v.push_back(make_shared&lt;BigObject&gt;(i));
	string p(to_string(5));
	binary_search(cbegin(v), cend(v), p, [](const Delegate &amp;d1, const Delegate &amp;d2){ return d1.s &lt; d2.s; });
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2392486</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2392486</guid><dc:creator><![CDATA[nwp3]]></dc:creator><pubDate>Thu, 03 Apr 2014 06:55:31 GMT</pubDate></item></channel></rss>