Hi,
I'm trying to come up with an efficient way to solve the following
problem.
Let A = { (1, b), (2, d), (3, a), (4, c) ,... }
B = { (1, d), (2, a), (3, e), (4, f),... }
where a,b,c... belong to set of integers and are UNIQUE.
Achieve
C = A - B;
D = B - A;
where the difference operator is defined such that
C = { (1, b), (4, c),... }
D = { (3, e), (4, f),... }
My *tedious* solution:
------------------------------
std::vector<int > V1, V2, V1sort, V2sort, V3, V4, V5, V6
std::vector<int >::Iterator iter;
std::map<int, int> C,D;
V1= { b, d, a, c, ... } /* get all the elements of A */
V2= { d, a, e, f, ... }
V1sort = /* sorted V1 */
V2sort = /* sorted V2 */
set_difference ( V1sort.begin(), V1sort.end(), V2sort.begin(),
V2sort.end(), V3.begin(), V3.end())
set_difference ( V2sort.begin(), V2sort.end(), V1sort.begin(),
V1sort.end(), V4.begin(), V4.end())
for (iter= V3.begin(); iter != V3.end(); ++iter)
{
for (int i = 0; i< V1.size(); i++)
{
if ( *iter = V1[i] )
{
V5.push_back(i) ;
break;
}
}
}
/* Similarly compute V6 for vector V4 */
sort( V5.begin(), V5.end() );
sort( V6.begin(), V6.end() );
for ( iter = V5.begin(); i != V5.end(); ++iter)
C[*iter] = V1(*iter);
for ( iter = V6.begin(); i != V6.end(); ++iter)
D[*iter] = V1(*iter);
The above program is not tested for errors but you get the idea. As you
see in the above approach I've been using several for loops and sort
functions which slow down the execution when the sizes of V1 & V2 are
huge. Is there an efficient way to achieve the above task?
Thank you very much.
-KK 1 1893
The elements of the groups are not: a,b,c.. but the pairs. You should
define a struct that represents the pair, and define a "less than"
function object (actually a struct with () operator), that compares two
pair by comparing the right members from them.
If it wasn't too clear, just look for "set" in an STL help, like: http://www.sgi.com/tech/stl
Yuval. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dylan |
last post by:
Is there an STL algorithm that will return true if each element in
coll1 is present in coll2
|
by: CodeCracker |
last post by:
Problem details below:
I have few items(simple values or objects) to be put into an array and
I implement it through a set rather than just an array of items. This
is because every time I get a new item I have to look into the Set
whether it is there or not. Depending on whether it is there or not I
have to respond differntly.
Since it is easy to search into a Set rather than a number of if
statement which will compare each of the...
|
by: hoopsho |
last post by:
Hi Everyone,
I am trying to write a program that does a few things very fast
and with efficient use of memory...
a) I need to parse a space-delimited file that is really large,
upwards fo a million lines.
b) I need to store the contents into a unique hash.
c) I need to then sort the data on a specific field.
d) I need to pull out certain fields and report them to the user.
|
by: sdilip519 |
last post by:
i am using maps with a string in the template argument and i want to
find the difference of two maps into another amp for this i can use the
set_difference function but the arguments of the functions are
iterators.
can any one help me in this how to send the arguments in
to the function.
Thanks in advance.
Regards
|
by: tezheng |
last post by:
since std::map is some kind of std::set<std::pair>
can i use "set_difference" with std::map?
how to deal with the "operator =" of std::pair<T1, T2>, as T1 and T2 could
be various type.
| |
by: Duncan Smith |
last post by:
Just before I have to implement my own.. I don't suppose the .NET
framework has an equivalent of the STL set_difference algorithm
yet...?
For e.g. If I have two containers: CompareSet={1,2,3} ToSet={2,3,4}
then diff(CompareSet,ToSet) = {4} and diff(ToSet,CompareSet) = {1}
Thanks,
Duncan
|
by: Duncan Smith |
last post by:
Just before I have to implement my own.. I don't suppose the .NET
framework has an equivalent of the STL set_difference algorithm
yet...?
For e.g. If I have two containers: CompareSet={1,2,3} ToSet={2,3,4}
then diff(CompareSet,ToSet) = {4} and diff(ToSet,CompareSet) = {1}
Thanks,
|
by: pedagani |
last post by:
Dear comp.lang.c++,
Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.
//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
|
by: Bill David |
last post by:
SUBJECT: How to make this program more efficient?
In my program, a thread will check update from server periodically and
generate a stl::map for other part of this program to read data from.
Let's name the update method as doUpdate and stl::map read methods as
getData and copyData.
Since stl::map is not thread-safe, we should do synchronization by
ourselves. A usable solution is to create a boost::mutex::scoped_lock
object in all above...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |