188 lines
7.7 KiB
HTML
188 lines
7.7 KiB
HTML
<!--
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
contributor license agreements. See the NOTICE file distributed
|
|
with this work for additional information regarding copyright
|
|
ownership. The ASF licenses this file to you under the Apache
|
|
License, Version 2.0 (the License); you may not use this file
|
|
except in compliance with the License. You may obtain a copy of
|
|
the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
implied. See the License for the specific language governing
|
|
permissions and limitations under the License.
|
|
|
|
Copyright 1999-2007 Rogue Wave Software, Inc.
|
|
-->
|
|
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>inplace_merge()</TITLE>
|
|
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
|
|
<BODY BGCOLOR=#FFFFFF>
|
|
<A HREF="inner-product.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="inputiterators.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV>
|
|
<H2>inplace_merge()</H2>
|
|
<P><B>Library:</B> <A HREF="2-9.html">Algorithms</A></P>
|
|
|
|
<PRE><HR><B><I>Function</I></B><HR></PRE>
|
|
|
|
<UL>
|
|
<LI><A HREF="#sec1">Local Index</A></LI>
|
|
<LI><A HREF="#sec2">Summary</A></LI>
|
|
<LI><A HREF="#sec3">Synopsis</A></LI>
|
|
<LI><A HREF="#sec4">Description</A></LI>
|
|
<LI><A HREF="#sec5">Complexity</A></LI>
|
|
<LI><A HREF="#sec6">Example</A></LI>
|
|
<LI><A HREF="#sec7">See Also</A></LI>
|
|
<LI><A HREF="#sec8">Standards Conformance</A></LI>
|
|
</UL>
|
|
<A NAME="sec1"><H3>Local Index</H3></A>
|
|
No Entries
|
|
<A NAME="sec2"><H3>Summary</H3></A>
|
|
<P>Algorithm that merges two sorted consecutive sequences into a single sorted sequence</P>
|
|
<A NAME="sec3"><H3>Synopsis</H3></A>
|
|
|
|
<PRE>#include <algorithm>
|
|
|
|
namespace std {
|
|
template <class BidirectionalIterator>
|
|
void inplace_merge(BidirectionalIterator start,
|
|
BidirectionalIterator mid,
|
|
BidirectionalIterator finish);
|
|
|
|
template <class BidirectionalIterator, class Compare>
|
|
void inplace_merge(BidirectionalIterator start,
|
|
BidirectionalIterator mid,
|
|
BidirectionalIterator finish,
|
|
Compare comp);
|
|
}
|
|
</PRE>
|
|
<A NAME="sec4"><H3>Description</H3></A>
|
|
<P>The <SAMP>inplace_merge()</SAMP> algorithm merges two sorted consecutive ranges <SAMP>[start, mid)</SAMP> and <SAMP>[mid, finish)</SAMP>, and puts the result of the merge into the range <SAMP>[start, finish)</SAMP>. The merge is stable, which means that if the two ranges contain equivalent elements, the elements from the first range always precede the elements from the second. </P>
|
|
<P>There are two versions of the <SAMP>inplace_merge()</SAMP> algorithm. The first version uses <SAMP>operator<()</SAMP> as the default for comparison, and the second version accepts a third argument that specifies a binary predicate function object.</P>
|
|
<A NAME="sec5"><H3>Complexity</H3></A>
|
|
<P>When enough additional memory is available, <SAMP>inplace_merge()</SAMP> does at most<SAMP> (finish - start) - 1</SAMP> comparisons. If no additional memory is available, an algorithm with <SAMP>O(N * log(N))</SAMP> complexity may be used, where <SAMP>N</SAMP> is equal to <SAMP>finish - start</SAMP>.</P>
|
|
<A NAME="sec6"><H3>Example</H3></A>
|
|
|
|
<UL><PRE>//
|
|
// merge.cpp
|
|
//
|
|
|
|
#include <algorithm> // advance, copy, inplace_merge, merge
|
|
#include <functional> // less
|
|
#include <iostream> // cout
|
|
#include <iterator> // back_inserter, ostream_iterator
|
|
#include <vector> // vector
|
|
|
|
|
|
|
|
int main ()
|
|
{
|
|
typedef std::vector<int, std::allocator<int> > Vector;
|
|
|
|
const Vector::value_type d1[] = { 1, 2, 3, 4};
|
|
const Vector::value_type d2[] = { 11, 13, 15, 17,
|
|
12, 14, 16, 18};
|
|
|
|
// Set up two vectors.
|
|
Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
|
|
Vector v2 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
|
|
|
|
// Set up four destination vectors.
|
|
Vector v3 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
|
|
Vector v4 (v3);
|
|
Vector v5 (v3);
|
|
Vector v6 (v3);
|
|
|
|
// Set up one empty vector.
|
|
Vector v7;
|
|
|
|
// Merge v1 with v2.
|
|
std::merge (v1.begin (), v1.end (),
|
|
v2.begin (), v2.end (), v3.begin ());
|
|
|
|
// Now use comparator.
|
|
std::merge (v1.begin (), v1.end (),
|
|
v2.begin (), v2.end (), v4.begin (),
|
|
std::less<int>());
|
|
|
|
// In place merge v5.
|
|
Vector::iterator mid = v5.begin ();
|
|
std::advance (mid, 4); // equivalent to mid += 4
|
|
// but more generic
|
|
std::inplace_merge (v5.begin (), mid, v5.end ());
|
|
|
|
// Now use a comparator on v6.
|
|
mid = v6.begin ();
|
|
std::advance (mid, 4); // equivalent to mid += 4
|
|
// but more generic
|
|
std::inplace_merge (v6.begin (), mid, v6.end (),
|
|
std::less<int>());
|
|
|
|
// Merge v1 and v2 to empty vector using insert iterator.
|
|
std::merge (v1.begin (), v1.end (),
|
|
v2.begin (), v2.end (),
|
|
std::back_inserter (v7));
|
|
|
|
// Copy all to cout.
|
|
std::ostream_iterator<int, char, std::char_traits<char> >
|
|
out (std::cout," ");
|
|
|
|
std::copy (v1.begin (), v1.end (), out);
|
|
std::cout << std::endl;
|
|
std::copy (v2.begin(), v2.end (), out);
|
|
std::cout << std::endl;
|
|
std::copy (v3.begin (), v3.end (), out);
|
|
std::cout << std::endl;
|
|
std::copy (v4.begin (), v4.end (), out);
|
|
std::cout << std::endl;
|
|
std::copy (v5.begin (), v5.end (), out);
|
|
std::cout << std::endl;
|
|
std::copy (v6.begin (),v6.end (), out);
|
|
std::cout << std::endl;
|
|
std::copy (v7.begin (), v7.end (), out);
|
|
std::cout << std::endl;
|
|
|
|
// Merge v1 and v2 to cout.
|
|
std::merge (v1.begin (), v1.end (),
|
|
v2.begin (), v2.end (), out);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
Program Output:
|
|
</PRE></UL>
|
|
<UL><PRE>1 2 3 4
|
|
1 2 3 4
|
|
1 1 2 2 3 3 4 4
|
|
1 1 2 2 3 3 4 4
|
|
11 12 13 14 15 16 17 18
|
|
11 12 13 14 15 16 17 18
|
|
1 1 2 2 3 3 4 4
|
|
1 1 2 2 3 3 4 4
|
|
</PRE></UL>
|
|
<A NAME="sec7"><H3>See Also</H3></A>
|
|
<P><SAMP><A HREF="merge.html">merge()</A></SAMP></P>
|
|
<A NAME="sec8"><H3>Standards Conformance</H3></A>
|
|
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 25.3.4</I></P>
|
|
|
|
<BR>
|
|
<HR>
|
|
<A HREF="inner-product.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="inputiterators.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
|
|
|
|
<!-- Google Analytics tracking code -->
|
|
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
|
|
</script>
|
|
<script type="text/javascript">
|
|
_uacct = "UA-1775151-1";
|
|
urchinTracker();
|
|
</script>
|
|
<!-- end of Google Analytics tracking code -->
|
|
|
|
</BODY>
|
|
</HTML>
|