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

Home Posts Topics Members FAQ

why no merge without sorting in STL

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
Jul 22 '05 #1
9 2559
Hunter Hou wrote:

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.


Maybe you want list.splice() ?

--
Russell Hanneken
eu*******@cbobk .pbz
Use ROT13 to decode my email address.
Jul 22 '05 #2
Hunter Hou wrote in news:cb******** @netnews.proxy. lucent.com in
comp.lang.c++:
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.


Isn't this just an *append* operation:

#include <iostream>
#include <ostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main()
{
using namespace std;

vector< int > a, b;
a.push_back( 1 );
b = a;
a.push_back( 2 );

copy( a.begin(), a.end(), back_inserter( b ) );

copy(
b.begin(), b.end(),
ostream_iterato r< int >( cout, "\n" )
);
}
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Rob Williscroft wrote:

Isn't this just an *append* operation:

#include <iostream>
#include <ostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main()
{
using namespace std;

vector< int > a, b;
a.push_back( 1 );
b = a;
a.push_back( 2 );

copy( a.begin(), a.end(), back_inserter( b ) );

copy(
b.begin(), b.end(),
ostream_iterato r< int >( cout, "\n" )
);
}

Yes. This is a solution, but don't know if it's as efficient as merge if
STL provide .

Since merge without sorting is more general, this is still an intriguing
question.
Jul 22 '05 #4
Hunter Hou wrote:

Rob Williscroft wrote:

Isn't this just an *append* operation:

#include <iostream>
#include <ostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main()
{
using namespace std;

vector< int > a, b;
a.push_back( 1 );
b = a;
a.push_back( 2 );

copy( a.begin(), a.end(), back_inserter( b ) );

copy(
b.begin(), b.end(),
ostream_iterato r< int >( cout, "\n" )
);
}

Yes. This is a solution, but don't know if it's as efficient as merge if
STL provide .

Since merge without sorting is more general


???

A merge operation in computing is usually defined as:
merge 2 sorted sequences into 1 sorted sequence

Since merge has to take this into account, it has to do much
more the just simple appending (to answer your fear about efficiency).
While the above is just some data movement, merge also has to compare
items in order to keep the sequence sorted.

So merge does a completely different thing to what you seem to want.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5
On Wed, 23 Jun 2004 16:20:07 +0800 in comp.lang.c++, "Hunter Hou"
<hy***@lucent.c om> wrote,
I looked at list.merge() and merge() algorithm, both require sorted
sequenece.


Merge takes two sorted sequences and produces a sorted sequence.
That's what it does, it's a special purpose tool. If you don't want
sorted sequences, then you want something other than merge, probably
something that is actually much simpler.

What do you want to accomplish?

Jul 22 '05 #6
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@130 .133.1.4...
vector< int > a, b;
a.push_back( 1 );
b = a;
a.push_back( 2 );

copy( a.begin(), a.end(), back_inserter( b ) );

~~~~
Just a comment about this idiom, which I see too often.
While a smart compiler+librar y implementation would be
able to optimize the above call, the following is likely
to be more efficient, and is IMHO more explicit:
b.insert( b.end(), a.begin(), a.end() );

As a rule of thumb, member functions should be preferred
to using a general algorithm (+ the back_inserter adapter here).

Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #7
"Ivan Vecerina" <pl************ *****@ivan.vece rina.com> wrote:
As a rule of thumb, member functions should be preferred
to using a general algorithm (+ the back_inserter adapter here).


I disagree, especially when it comes to generic code: the non-member
functions are the way to go. The library should generally provide
specialized version of the general algorithms wehre feasible - although,
admittedly, most don't. The problem with member function is that they
tend to more restrictive than the non-member algorithms and are thus
unsuitable for generic code. For example, to use 'back_inserter( )' with
some object, it merely needs a 'push_back()' operation. This may be a
suitable way to fill data into a custom container. Actually, I'd even
prefer if 'push_back()' were a non-member which would forward be
specialized to forward to suitable member functions.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting
Jul 22 '05 #8
"Dietmar Kuehl" <di***********@ yahoo.com> wrote in message
news:5b******** *************** ***@posting.goo gle.com...
"Ivan Vecerina" <pl************ *****@ivan.vece rina.com> wrote:
As a rule of thumb, member functions should be preferred
to using a general algorithm (+ the back_inserter adapter here).
I disagree, especially when it comes to generic code: the non-member
functions are the way to go. The library should generally provide
specialized version of the general algorithms wehre feasible - although,
admittedly, most don't. The problem with member function is that they
tend to more restrictive than the non-member algorithms and are thus
unsuitable for generic code. For example, to use 'back_inserter( )' with
some object, it merely needs a 'push_back()' operation.


Really, is it better to use "copy(....,back _inserter(a))" rather than
"a.insert( a.end(), ... )" just because copy is a non-member function ?

The portability argument doesn't really favor the use of push_back,
since all sequence containers are required to provide an insert(p,i,j)
member function, while push_back() is an optional feature (§23.1.1).

And as far as I know, few library implementations (do you know
an example?) actually specialize algorithms on back_inserter.. .
This may be a
suitable way to fill data into a custom container. Actually, I'd even
prefer if 'push_back()' were a non-member which would forward be
specialized to forward to suitable member functions.


Philosophically , I totally agree with this. I would prefer a standard
library with more non-member functions...

But I prefer to explicitly perform an insertion, than to distort the use
of 'copy' -- which to me suggests that items are being overwritten.
Among two non-member functions, I would still choose 'insert'
( unless of course an "append" algorithm was available ;) )

Kind regards,
Ivan


Jul 22 '05 #9
Ivan Vecerina wrote in news:cb******** **@newshispeed. ch in comp.lang.c++:
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@130 .133.1.4...
vector< int > a, b;
a.push_back( 1 );
b = a;
a.push_back( 2 );

copy( a.begin(), a.end(), back_inserter( b ) );

~~~~
Just a comment about this idiom, which I see too often.
While a smart compiler+librar y implementation would be
able to optimize the above call, the following is likely
to be more efficient, and is IMHO more explicit:
b.insert( b.end(), a.begin(), a.end() );

As a rule of thumb, member functions should be preferred
to using a general algorithm (+ the back_inserter adapter here).


Yep I agree, this was my thinking (if it can be called that):

The OP says they want "merge" but actually they want "append".

A quick double-check later there is no std::append, so how
would it be writen ?

... some typing ...

hit [Send].

:).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #10

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

Similar topics

2
8255
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a different story. Linked lists have been giving me a headache. I can barely manage to create a single/doubly linked list, it's just that when it gets to merge sorting or searching (by reading shit data from text files), I lose track.
3
4606
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many comparisons. It seems to me like I should just use a single counter in the merge function's 'if' statement, but this can't be right because an array of 50 takes about 100 comparisons this way. If anyone has any suggestions I would greatly...
6
8766
by: cylin | last post by:
Dear all, If I declared a map<int,string> MapA; How can I sort MapA by its data,i.e. string. for example: map<int,string> MapA; MapA=string("ccc"); MapA=string("bbb"); MapA=string("ddd");
4
4957
by: Andreas Kasparek | last post by:
Hola! I'm preparing my master thesis about a XML Merge Tool implementation and was wondering if there is any open standard for XML diff regarding topics like: - is a diff result computed on the ordered or unordered xml node tree of the compared documents? - what identifiers/criteria should be used by default to match elements of the same type in different documents? - should a diff tool consider move operations or only insert/delete
8
1873
by: ilikesluts | last post by:
Hi Group, I'm new to XML, here is my question: Would it be possible to write an algorithm that takes in two XML documents with the only condition being that they have the same root element? If it is possible what would be the best technology to implement the solution (XMLDom, XSLT...)? How hard would the solution be?
5
3548
by: uppe | last post by:
Hey everyone, I've just finished my implementation of the merge-sort algorithm in C, and I thought I could ask for some feedback. (One can always improve, they say) Right now, the code sorts integers in an ascending order, which probably isn't very useful on its own. I know the code works for a reasonable and arbitrary size of input, but
9
5627
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
7
576
by: mooon33358 | last post by:
I have a little problem with implementing a recursive merge sort I have to use a function mergesort that takes 3 arguments - an array, its size, and an help array(i.e mergesort(int array, int size, int temp] . I have to use only the temp array for the sorting, I cant use two help arrays for the sorting, and I cant change the signature of the function. also have a merge function that takes 5 arguments - left array and its size, right...
0
1154
by: subramanian100in | last post by:
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
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
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
10182
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
10017
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
7552
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
5445
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...
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
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.