473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 copyUniqueObject(const vector<T &seqContainer,
set<T &uniqueSet)
{
std::copy(seqContainer.begin(), seqContainer.end(),
std::inserter(uniqueSet, 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 4045
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.insert(seqContainer.begin(), seqContainer.end());
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 copyUniqueObject(const vector<T &seqContainer,
set<T &uniqueSet)
{
std::copy(seqContainer.begin(), seqContainer.end(),
std::inserter(uniqueSet, 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.googlegroups.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.insert(seqContainer.begin(), seqContainer.end());
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.googlegroups.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.googlegroups.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
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!! -...
2
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...
4
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...
3
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...
12
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...
1
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
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...
11
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...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.