<?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[STL_   search()]]></title><description><![CDATA[<p>hallo zusammen,<br />
in STL gibt es die search() Algorithmen, bei der <strong>Version_2</strong> verstehe ich nicht die folgende Codes:</p>
<p>**while (__first1 != __last1) {<br />
if (__predicate(*__first1, *__first2)) break;<br />
++__first1;<br />
}<br />
while (__first1 != __last1 &amp;&amp; !__predicate(*__first1, *__first2))<br />
++__first1;<br />
**<br />
ich glaube, die zwei while() Schleife funktionieren genau selbe! einmal überflüssig?</p>
<p>version_1:</p>
<pre><code class="language-cpp">template &lt;class _ForwardIter1, class _ForwardIter2&gt;
_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,_ForwardIter2 __first2, _ForwardIter2 __last2)
{
  // Test for empty ranges
  if (__first1 == __last1 || __first2 == __last2)
    return __first1;

  // Test for a pattern of length 1.
  _ForwardIter2 __tmp(__first2);
  ++__tmp;
  if (__tmp == __last2)
    return find(__first1, __last1, *__first2);

  // General case.
  _ForwardIter2 __p1, __p;
  __p1 = __first2; ++__p1;
  _ForwardIter1 __current = __first1;

  while (__first1 != __last1) {
    __first1 = find(__first1, __last1, *__first2);
    if (__first1 == __last1) return __last1;

    __p = __p1;
    __current = __first1;
    if (++__current == __last1)
      return __last1;
    while (*__current == *__p) {
      if (++__p == __last2)
        return __first1;
      if (++__current == __last1)
        return __last1;
    }
    ++__first1;
  }
  return __first1;
}
</code></pre>
<p>version_2:</p>
<pre><code class="language-cpp">template &lt;class _ForwardIter1, class _ForwardIter2, class _BinaryPred&gt;
_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,_ForwardIter2 __first2, _ForwardIter2 __last2,
                     _BinaryPred  __predicate)
{
  // Test for empty ranges
  if (__first1 == __last1 || __first2 == __last2)
    return __first1;

  // Test for a pattern of length 1.
  _ForwardIter2 __tmp(__first2);
  ++__tmp;
  if (__tmp == __last2) {
    while (__first1 != __last1 &amp;&amp; !__predicate(*__first1, *__first2))
      ++__first1;
    return __first1;    
  }

  // General case.
  _ForwardIter2 __p1, __p;
  __p1 = __first2; ++__p1;
  _ForwardIter1 __current = __first1;

  while (__first1 != __last1) {

//---------------------------------------------------
    while (__first1 != __last1) {
      if (__predicate(*__first1, *__first2)) break;
      ++__first1;
    }
    while (__first1 != __last1 &amp;&amp; !__predicate(*__first1, *__first2))
      ++__first1;
//-------------------------------------------------------------

    if (__first1 == __last1) return __last1;

    __p = __p1;
    __current = __first1;
    if (++__current == __last1) return __last1;

    while (__predicate(*__current, *__p)) {
      if (++__p == __last2)
        return __first1;
      if (++__current == __last1)
        return __last1;
    }
    ++__first1;
  }
  return __first1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/199206/stl_-search</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 21:42:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/199206.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Nov 2007 21:00:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to STL_   search() on Thu, 29 Nov 2007 21:00:23 GMT]]></title><description><![CDATA[<p>hallo zusammen,<br />
in STL gibt es die search() Algorithmen, bei der <strong>Version_2</strong> verstehe ich nicht die folgende Codes:</p>
<p>**while (__first1 != __last1) {<br />
if (__predicate(*__first1, *__first2)) break;<br />
++__first1;<br />
}<br />
while (__first1 != __last1 &amp;&amp; !__predicate(*__first1, *__first2))<br />
++__first1;<br />
**<br />
ich glaube, die zwei while() Schleife funktionieren genau selbe! einmal überflüssig?</p>
<p>version_1:</p>
<pre><code class="language-cpp">template &lt;class _ForwardIter1, class _ForwardIter2&gt;
_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,_ForwardIter2 __first2, _ForwardIter2 __last2)
{
  // Test for empty ranges
  if (__first1 == __last1 || __first2 == __last2)
    return __first1;

  // Test for a pattern of length 1.
  _ForwardIter2 __tmp(__first2);
  ++__tmp;
  if (__tmp == __last2)
    return find(__first1, __last1, *__first2);

  // General case.
  _ForwardIter2 __p1, __p;
  __p1 = __first2; ++__p1;
  _ForwardIter1 __current = __first1;

  while (__first1 != __last1) {
    __first1 = find(__first1, __last1, *__first2);
    if (__first1 == __last1) return __last1;

    __p = __p1;
    __current = __first1;
    if (++__current == __last1)
      return __last1;
    while (*__current == *__p) {
      if (++__p == __last2)
        return __first1;
      if (++__current == __last1)
        return __last1;
    }
    ++__first1;
  }
  return __first1;
}
</code></pre>
<p>version_2:</p>
<pre><code class="language-cpp">template &lt;class _ForwardIter1, class _ForwardIter2, class _BinaryPred&gt;
_ForwardIter1 search(_ForwardIter1 __first1, _ForwardIter1 __last1,_ForwardIter2 __first2, _ForwardIter2 __last2,
                     _BinaryPred  __predicate)
{
  // Test for empty ranges
  if (__first1 == __last1 || __first2 == __last2)
    return __first1;

  // Test for a pattern of length 1.
  _ForwardIter2 __tmp(__first2);
  ++__tmp;
  if (__tmp == __last2) {
    while (__first1 != __last1 &amp;&amp; !__predicate(*__first1, *__first2))
      ++__first1;
    return __first1;    
  }

  // General case.
  _ForwardIter2 __p1, __p;
  __p1 = __first2; ++__p1;
  _ForwardIter1 __current = __first1;

  while (__first1 != __last1) {

//---------------------------------------------------
    while (__first1 != __last1) {
      if (__predicate(*__first1, *__first2)) break;
      ++__first1;
    }
    while (__first1 != __last1 &amp;&amp; !__predicate(*__first1, *__first2))
      ++__first1;
//-------------------------------------------------------------

    if (__first1 == __last1) return __last1;

    __p = __p1;
    __current = __first1;
    if (++__current == __last1) return __last1;

    while (__predicate(*__current, *__p)) {
      if (++__p == __last2)
        return __first1;
      if (++__current == __last1)
        return __last1;
    }
    ++__first1;
  }
  return __first1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1412594</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1412594</guid><dc:creator><![CDATA[damo]]></dc:creator><pubDate>Thu, 29 Nov 2007 21:00:23 GMT</pubDate></item><item><title><![CDATA[Reply to STL_   search() on Thu, 29 Nov 2007 21:20:57 GMT]]></title><description><![CDATA[<p>Ich kann jedenfalls keinen Unterschied bei den while-Schleifen entdecken!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1412603</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1412603</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Thu, 29 Nov 2007 21:20:57 GMT</pubDate></item></channel></rss>