• According to the standard, the caller has to ensure that both source ranges are sorted on entry.
However, in most implementations, this algorithm also merges elements of two unsorted source
ranges into an unsorted destination range. Nevertheless, for unsorted ranges, you should call
copy() twice, instead of merge(), to be portable.
• The caller must ensure that the destination range is big enough or that insert iterators are used.
• The destination range should not overlap the source ranges.
• Lists and forward lists provide a special member function, merge(), to merge the elements of
two lists (see Section 8.8.1, page 423).
• To ensure that elements that are in both source ranges end up in the destination range only once,
use set_union() (see page 616).
• To process only the elements that are in both source ranges, use set_intersection() (see
page 617).
• Complexity: linear (at most, numElems1+numElems2-1 comparisons).
The following example demonstrates how to use merge():
// algo/merge1.cpp
#include "algostuff.hpp"
using namespace std;
int main()
{
list coll1;
set coll2;
// fill both collections with some sorted elements
INSERT_ELEMENTS(coll1,1,6);
INSERT_ELEMENTS(coll2,3,8);
PRINT_ELEMENTS(coll1,"coll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
// print merged sequence
cout
•ตามมาตรฐาน โทรได้เพื่อให้แน่ใจว่า แหล่งข้อมูลทั้งสองจะถูกเรียงลำดับในรายการ
อย่างไรก็ตาม ในการใช้งานมากที่สุด อัลกอริทึมนี้ยังผสานองค์ประกอบของแหล่ง unsorted สอง
ช่วงในช่วงปลายทาง unsorted อย่างไรก็ตาม ช่วง unsorted คุณควรโทร
copy() สอง แทน merge() จะพกพา
•ผู้เรียกต้องแน่ใจว่า ช่วงปลายมีขนาดใหญ่เพียงพอ หรือที่แทรกใช้ iterators
•ช่วงปลายไม่ควรทับซ้อนแหล่งข้อมูล
•รายการและรายการไปข้างหน้าให้ฟังก์ชันสมาชิกพิเศษ merge() การผสานองค์ประกอบของ
2 รายการ (ดูส่วน 8.8.1 หน้า 423)
•เพื่อให้แน่ใจว่า องค์ประกอบที่อยู่ในแหล่งข้อมูลทั้งสิ้นสุดขึ้นในช่วงปลายเพียงครั้งเดียว,
ใช้ set_union() (ดูหน้า 616)
•การประมวลผลเฉพาะองค์ประกอบที่เป็นทั้งแหล่งช่วง set_intersection() ใช้ (ดู
หน้า 617)
•ความซับซ้อน: เชิงเส้น (ที่ส่วนใหญ่ numElems1 numElems2-1 เปรียบเทียบ)
ตัวอย่างต่อไปนี้สาธิตวิธีการใช้ merge():
/ / algo/merge1.cpp
#include "algostuff.hpp"
ใช้ namespace มาตรฐาน
int main()
{
รายการ < int > coll1;
ชุด < int > coll2;
/ / fill ทั้งชุดกับเรียงลำดับองค์ประกอบ
INSERT_ELEMENTS(coll1,1,6);
INSERT_ELEMENTS(coll2,3,8);
PRINT_ELEMENTS (coll1, " coll1: ");
PRINT_ELEMENTS(coll2,"coll2:")
/ / พิมพ์ลำดับผสาน
cout << "ผสาน: ";
รวม (coll1.cbegin(), coll1.cend(),
coll2cbegin(), coll2.cend(),
ostream_iterator (cout "));
cout << endl
}
โปรแกรมมีผลลัพธ์ต่อไปนี้:
coll1: 1 2 3 4 5 6
coll2: 3 4 5 6 7 8
ผสาน: 1 2 3 3 4 4 5 5 6 6 7 8
การแปล กรุณารอสักครู่..

- ตามมาตรฐานเพื่อให้แน่ใจว่าช่วงที่โทรได้ทั้งแหล่งจะเรียงบนรายการ
แต่ในการใช้งานมากที่สุด วิธีนี้ยังผสานองค์ประกอบสอง
ที่มาเรียงช่วงในช่วงปลายทางเรียง . อย่างไรก็ตาม ในช่วงที่ไม่มีการเรียงลำดับ คุณควรโทรหา
copy() สองครั้ง แทน merge() เป็นแบบพกพา
แต่ละสายจะต้องมั่นใจว่าช่วงปลายมีขนาดใหญ่พอ หรือใส่ iterators ใช้
- ช่วงปลายทางไม่ควรทับซ้อนมาช่วง
- รายการและรายการส่งต่อให้ฟังก์ชันสมาชิกพิเศษ merge() เพื่อผสานองค์ประกอบ
2 รายการ ( ดูมาตรา 8.8.1 หน้า 423 )
- เพื่อให้แน่ใจว่าองค์ประกอบที่เป็นทั้งแหล่งช่วงท้ายในช่วงปลายทางเพียงครั้งเดียว
ใช้ set_union() ( ดูหน้า 616 )
- กระบวนการเฉพาะของธาตุในช่วงทั้งแหล่ง ใช้ set_intersection() ( ดู
หน้า 617 )
- ความซับซ้อน : เชิงเส้น ( ส่วนใหญ่ numelems1 numelems2-1 เปรียบเทียบ )
ตัวอย่างต่อไปนี้แสดงให้เห็นถึงวิธีการใช้ merge() :
/ / /
merge1.cpp อัลโก#รวมถึง " algostuff เอชพีใช้ namespace std . "
;
1 {
main() รายการ < 1 > coll1 ;
ชุด < int > coll2 ;
/ / จึงจะคอลเลกชันทั้งบางจัดเรียงองค์ประกอบ
insert_elements ( coll1,1,6 ) ;
insert_elements ( coll2,3,8 ) ;
print_elements ( coll1 " coll1 : " ) ;
print_elements ( coll2 " coll2 : " ) ;
/ / พิมพ์โดยลำดับ เคาท์ < <
" ผสาน " ;
ผสาน ( coll1 . cbegin() coll1 cend()
coll2 , . , .cbegin() coll2 , . cend()
< int > ( , ostream_iterator เคาท์ , " " ) ; < < Endl เคาท์
;
}
โปรแกรมมีดังต่อไปนี้ output :
coll1 : 1 2 3 4 5 6
coll2 : 3 4 5 6 7 8
รวม : 1 2 3 3 4 4 5 5 6 7 8
6
การแปล กรุณารอสักครู่..
