473,772 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm copy from vector to set - newbie question

I need to write code to copy unique elements in from a sequence
container (say vector) to a set (or another sequence container will
do). Is following code efficient?

template <class T>
void copyUniqueObjec t(const vector<T &seqContaine r,
set<T &uniqueSet)
{
std::copy(seqCo ntainer.begin() , seqContainer.en d(),
std::inserter(u niqueSet, uniqueSet.end() ));
}

Or is there a better (more efficient) way (say, some way using
lower_bound or so) to write similar functionality?
Does using unique_copy over copy gives any advantage in this case?

Thanks in advance,
Regards,
~ Soumen
Jul 7 '08 #1
7 4075
Soumen wrote:
I need to write code to copy unique elements in from a sequence
container (say vector) to a set (or another sequence container will
do). Is following code efficient?
A set will already contain only unique elements, so you could simply do a:

uniqueSet.inser t(seqContainer. begin(), seqContainer.en d());
Jul 7 '08 #2
Soumen wrote:
I need to write code to copy unique elements in from a sequence
container (say vector) to a set (or another sequence container will
do). Is following code efficient?

template <class T>
void copyUniqueObjec t(const vector<T &seqContaine r,
set<T &uniqueSet)
{
std::copy(seqCo ntainer.begin() , seqContainer.en d(),
std::inserter(u niqueSet, uniqueSet.end() ));
}

Or is there a better (more efficient) way (say, some way using
lower_bound or so) to write similar functionality?
Does using unique_copy over copy gives any advantage in this case?

Thanks in advance,
Regards,
~ Soumen
If your vector is sorted, *and* you don't mind mutating it, then using
'std::unique' on it might give you some advantage. If it isn't sorted,
and/or you don't want to mutate it, then your approach is sufficient.

The problem (as I see it) with 'std::set' is that its "insert" member
will have to attempt insertion every time, which means it will search
through the set more than necessary. Sorting the source container and
then using 'unique_copy' has the advantage of skipping the search for
any object that is equal to the one that was just inserted. You can use
'std::deque' as the destination container in that case (the fastest from
the insertion-at-end/indexed-retrieval POV).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 7 '08 #3
In article <d886de30-a5f8-441f-83c8-c88d680a6731
@m3g2000hsc.goo glegroups.com>, so******@gmail. com says...

[ ... ]
Or is there a better (more efficient) way (say, some way using
lower_bound or so) to write similar functionality?
Does using unique_copy over copy gives any advantage in this case?
Assuming your input is already sorted, there's a pretty good chance that
unique_copy will give a slight performance advantage. If your input is
in a vector, the memory is guaranteed to be contiguous, so it'll tend to
have better locality of reference. The memory in the set will normally
be in the form of noncontiguous dynamically allocated nodes. As such,
traversing the identical inputs in the vector will usually be faster
than in the set.

OTOH, if your input is NOT already sorted, sorting it first so you can
use unique_copy (meaningfully) isn't likely to work out so well.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 7 '08 #4
On Jul 7, 6:14*pm, Juha Nieminen <nos...@thanks. invalidwrote:
Soumen wrote:
I need to write code to copy unique elements in from a sequence
container (say vector) to a set (or another sequence container will
do). Is following code efficient?

* A set will already contain only unique elements, so you could simply do a:

* uniqueSet.inser t(seqContainer. begin(), seqContainer.en d());
Thanks, I missed the range member function of set.
Jul 7 '08 #5
On Jul 7, 6:48*pm, Jerry Coffin <jcof...@taeus. comwrote:
In article <d886de30-a5f8-441f-83c8-c88d680a6731
@m3g2000hsc.goo glegroups.com>, soume...@gmail. com says...

[ ... ]
Or is there a better (more efficient) way (say, some way using
lower_bound or so) to write similar functionality?
Does using unique_copy over copy gives any advantage in this case?

Assuming your input is already sorted, there's a pretty good chance that
unique_copy will give a slight performance advantage. If your input is
in a vector, the memory is guaranteed to be contiguous, so it'll tend to
have better locality of reference. The memory in the set will normally
be in the form of noncontiguous dynamically allocated nodes. As such,
traversing the identical inputs in the vector will usually be faster
than in the set.

OTOH, if your input is NOT already sorted, sorting it first so you can
use unique_copy (meaningfully) isn't likely to work out so well.

--
* * Later,
* * Jerry.

The universe is a figment of its own imagination.
Thanks. But my input is not sorted. Even in that case I believe
sorting and using
unique_copy will give better performance if I use with vector and
back_inserter
for destination. Correct me if it's going to be worse than using deque
as pointed by
Victor.

Regards,
- Soumen
Jul 7 '08 #6
On 2008-07-07 12:48:34 -0400, Soumen <so******@gmail .comsaid:
>
Thanks. But my input is not sorted. Even in that case I believe
sorting and using
unique_copy will give better performance if I use with vector and
back_inserter
for destination. Correct me if it's going to be worse than using deque
as pointed by
Victor.
Better yet: try it and see. Hypothetical analyses of what might be
faster often collapse when confronted with real data.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 7 '08 #7
In article <390c72eb-7502-445c-9f63-6fc36335d488
@m36g2000hse.go oglegroups.com> , so******@gmail. com says...

[ ... ]
Thanks. But my input is not sorted. Even in that case I believe
sorting and using
unique_copy will give better performance if I use with vector and
back_inserter
for destination. Correct me if it's going to be worse than using deque
as pointed by Victor.
I think a few options that are all likely to be viable have been pointed
out, so at this point picking out the best will probably involve some
testing and profiling.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 7 '08 #8

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

Similar topics

4
1295
by: Home Newbie | last post by:
I have some stupid questions.... - Why would program 1 run a lot faster than program 2? Can someone explain? If I change the sample_size to be a larger one, program 2 will my browser freeze!! - What is the optimum value (1024 in this case) to use? Program 1: ----------- int sample_size = 5120; // 5K
2
2962
by: Peter R | last post by:
Hi, Probably this question was asked before, but what is the algorithm to generate all combinations based on N numbers? For example, for 3 elements = {1,2,3}, I'd like to generate a list of (2^n - NULL): 1, 2, 3 1, 2 2, 3
4
1796
by: Jef Driesen | last post by:
Is there an STL algorithm that does something like for (iterator i = begin(); i != end(); ++i) f(*i,value); where f results in the expression *i += value. I already found the algorithm 'transform' with the 'plus' function object, but it does evaluate to *i = *i + value. And this is not exactly what I want.
3
1615
by: nan.li.g | last post by:
Hello, all, This is probably not so much related to C++ (Sorry in advance). But I did get this question during a C++ job interview. You are asked to design some data structure and algorith for the following function: List getNames ( List nameList, String prefix ); What it does is: given a name list and a prefix, return all the names in the name list that start with prefix.
12
3164
by: No Such Luck | last post by:
Hi All: I'm not sure if this is the right place to ask this question, but I couldn't find a more appropriate group. This is more of a theory question regarding an algorithm implemented in C, not necessarily a C language question. I'm trying to break up a vector into an arbitrary number of subvectors, equal (or as near to equal) in size as possible. My problem occurs when the vector is not evenly divisible by the number of subvectors...
1
2127
by: utab | last post by:
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <iterator> using std::cout; using std::vector; using std::string; using std::endl;
1
3207
by: samuel.y.l.cheung | last post by:
Hi, I wrote a template to use copy() algorithm, called copyAll: template<class T> void copyAll(const T& src , T& dest ) { copy (src.begin(), src.end(), back_inserter(dest)); } but when I call it, I get a segmentation fault: Thread (Suspended: Signal 'SIGSEGV' received. Description:
11
2371
by: Dilip | last post by:
Howdy I have code similar to this in my project: Note: BSTR is an abomination conjured up some disturbed person in the COM world. BSTR strings must be allocated/deallocated using the SysAllocString/SysFreeString Windows APIs. typedef struct tagMyStruct {
10
6076
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list * and the even values from your vector.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
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
10038
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,...
1
7460
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
6715
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2850
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.