473,796 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on merge algorithm

consider :

template<class InIt1, class InIt2, class OutIt>
OutIt merge(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2,
OutIt dest);

Can the destination container already contain some elements ? If so,
will they also be taken into account for doing the sorting and
merging ? Or, should the destination container have just enough space
to hold the result of the merge( that is, it should not contain any
elements) ?

The reason for asking this question is the following:

#include <cstdlib>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
#include <utility>

using namespace std;

void print(const pair<const int, int>& arg)
{
cout << arg.first << " " << arg.second << endl;
return;
}

int main()
{
typedef pair<int, intp_type;

multiset<p_type c1;

c1.insert(make_ pair(0, 2));
c1.insert(make_ pair(0, 0));
c1.insert(make_ pair(0, 1));
c1.insert(make_ pair(2, 3));

vector<p_typec2 ;

c2.push_back(ma ke_pair(0, -2));
c2.push_back(ma ke_pair(1, 2));
c2.push_back(ma ke_pair(2, 2));

multimap<int, intc3;

c3.insert(make_ pair(5, 0));
c3.insert(make_ pair(0, 0));
c3.insert(make_ pair(3, 2));
c3.insert(make_ pair(0, -1));
c3.insert(make_ pair(1, -1));

merge(c1.begin( ), c1.end(), c2.begin(), c2.end(), inserter(c3,
c3.begin()));

for_each(c3.beg in(), c3.end(), print);

return EXIT_SUCCESS;
}

The output with
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
is
0 -2
0 0
0 1
0 2
0 0
0 -1
1 -1
1 2
2 2
2 3
3 2
5 0

The second pair (0, 0) in the output should have come before the pair
(0, 1) and
the pair (0, -1) in the output should have come after the pair (0,
-2). Am I correct or wrong ?

Kindly clarify.

Thanks
V.Subramanian
Jun 27 '08 #1
0 1153

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
349
by: Saikrishna | last post by:
Hi friends, I am trying to choose the best possible data structure for the probelm I am going to describe now. I have lets say tens of thousands of numbers in file1 and tens of thousands of numbers in another file2. file1 & file2 contents (only numbers) can be entirely different. Now the program should be able to read the files and give me a difference file. Ofcourse this is a very easy implementation if I go
9
2559
by: Hunter Hou | last post by:
Folks, I am just curious why standard library doesn't provide a "merge" operation without sorting, because sometimes I don't require sorting, just merge. I looked at list.merge() and merge() algorithm, both require sorted sequenece. Thanks, Hunter
11
6975
by: Dave [Hawk-Systems] | last post by:
have the table "numbercheck" Attribute | Type | Modifier -----------+------------+---------- svcnumber | integer | not null svcqual | varchar(9) | svcequip | char(1) | svctroub | varchar(6) | svcrate | varchar(4) | svcclass | char(1) | trailer | varchar(3) |
11
464
by: Jack | last post by:
Hi, I was asked this question during an interview. It seems very simple but I don't know if there is a more efficient way. The question said, we have 2 arrays as follows A = B = As you can see both arrays are sorted. Also we see that B has enough space for all the elements in A. We want to put element A in to B
46
3427
by: junky_fellow | last post by:
Hi, Is there any efficient way of finding the intesection point of two singly linked lists ? I mean to say that, there are two singly linked lists and they meet at some point. I want to find out the addres of the node where the two linked intersect. thanks for any help...
1
2636
by: vekka | last post by:
Hi! Could someone please help me to understand the use of merge algorithm(conceptually) with linked lists. What I want to do in the linked list function is: the function gets two inputs, i.e the pointers to the two smaller lists, then the function should able to merge these two together, and then return the head-pointer to this merged list. What happens in my case, is that I "lose" data when running it . My result is like this: Even OR...
3
2411
by: subramanian100in | last post by:
Consider the following program: #include <cstdlib> #include <iostream> #include <set> #include <map> #include <algorithm> #include <iterator> #include <utility>
5
1845
by: Chad | last post by:
I was looking at some old posts in comp.lang.c and found the following http://groups.google.com/group/comp.lang.c/browse_thread/thread/d26abbdf4d99abd9/b3b5046326994e18?hl=en&lnk=gst&q=recurrence+relation#b3b5046326994e18 I have some questions regarding the post. First, and I quote "Given an algorithm with loops or recursive calls, the way you find a big-O equivalence class for that algorithm is to write down a
24
7151
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases where the table is not empty, it can take care of the updating part, which makes the app cleaner. However, my concern is the merge state would slow dowm the insertion of new data, since in most cases the table is empty. So my questions (before I...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.