I am wondering if anyone has any better idea of how to approach
this problem than I do. . . .
I have a vector of items (data). I have to do a pairwise
comparison of each item to each other item and apply logic to see if
one should be deleted. In the past I have done this using for
statements, like:
for (int i = 0; i < input_vector.si ze(); i++)
for (int j = i; i < input_vector.si ze(); j++)
{
. . . process data . . .
}
The problem is that I have do delete items in the middle, which makes
index-watching tricky. So, in the past, I have copied the vector
first and manipulated the copy vice the original when I process the
data. However, then I have to keep track of which items in the
original vector have already been merged away. For example, I compare
items 1 and 2, and I decide to delete 2. So, then after I finish all
the comparisons with item 1, I need to do that with item 3 (not 2,
which I`ve deleted in the copied vector.
In addition to being complicated in the past, this has been slow.
Is there a better approach? I come across the same scenario
frequently.
Thanks, Alan 1 2081
Alan wrote:
I am wondering if anyone has any better idea of how to approach
this problem than I do. . . .
I have a vector of items (data). I have to do a pairwise
comparison of each item to each other item and apply logic to see if
one should be deleted. In the past I have done this using for
statements, like:
for (int i = 0; i < input_vector.si ze(); i++)
for (int j = i; i < input_vector.si ze(); j++)
{
. . . process data . . .
}
The problem is that I have do delete items in the middle, which makes
index-watching tricky.
Tricky? Really?... Most of vector cleanup code I've seen used to
decrement the index ('j', for example) right after deletion of the
element which it indexes (so the next ++ would essentially keep it
"correct").
So, in the past, I have copied the vector
first and manipulated the copy vice the original when I process the
data. However, then I have to keep track of which items in the
original vector have already been merged away. For example, I compare
items 1 and 2, and I decide to delete 2. So, then after I finish all
the comparisons with item 1, I need to do that with item 3 (not 2,
which I`ve deleted in the copied vector.
In addition to being complicated in the past, this has been slow.
Is there a better approach? I come across the same scenario
frequently.
Keep the second vector<charand "mark" the elements you've decided
to throw away. When going over your 'i' and 'j', first check with the
"marked" vector and if the element is set, skip to the next. Fast
and simple to understand. At the end walk from start and copy into
another vector only the elements that don't have corresponding "marked"
elements set.
Or write a proper functor and use 'remove_if'. You can rely on the
requirement that elements of a vector exist in an array...
And if you post a bit more of the code that illustrates your problem
we might point out inefficiencies further (if you want).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and "push_back" a copy of this into a
vector V. This is repeated many times in an iterative process.
Ok whenever I "push_back" a copy of Class A, I also want to assign a
pointer contained in an exisiting instance of a Class B to this
|
by: koperenkogel |
last post by:
Dear cpp-ians,
I am working with a vector of structures.
vector <meta_segment> meta_segm (2421500);
and the structure look like:
struct meta_segment
{
float id;
float num;
|
by: chandra.somesh |
last post by:
Hi
I recently had to write a small code in a competition ,but my code was
rejected cause it failed in 1 of test cases.
The problm was .....we are given vector of strings....each string
consists of either 1 or 2("12122" 0r "2121" so on..)...i had to find
the that string where percentage of '1' is minimum.Now the problem and
solution both are trivial but i was told that comparing double with <
or > sign doesn't ensure a correct...
|
by: Bob |
last post by:
Here's what I have:
void miniVector<T>::insertOrder(miniVector<T>& v,const T& item)
{
int i, j;
T target;
vSize += 1;
T newVector;
newVector=new T;
|
by: Patrick |
last post by:
I have got 2 XML documents, both of which conform to the same XSD Schema,
which define possible optional elements. The 2 XML documents contain 2
disjoint set of XML elements.
What is the best, easiest, most efficient way of merging the 2 XML
Documents? Can I use DataSet.Merge() facility in ADO.NET?? Any
pre-requisites?
Any other suggestions?
| |
by: Alan |
last post by:
Hi. I have programmed in C++ before, but I`m a couple of years out
of practice. I am seeking some advice on getting started on a quickie
project. . . .
I have to read a 54MB text file and do a pairwise comparison
among 2500 items or so in the file. Each of those items have to be
compared to every other item. Many of the comparison will only require
comparing one field of the items. I will probably sort on this field
before I do the...
|
by: SvenMathijssen |
last post by:
Hi,
I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer.
What I try to do is sort the records in a plain-text index file based on certain columns. The index file consists of records and fields within the records. The individual fields are separated by semicolons, the records by newlines. The index file is loaded into memory...
|
by: fade |
last post by:
Good afternoon,
I need some advice on the following:
I've got a class that has a member
std::vector<CStringm_vFileName and a member CString m_path;
The vector contains a bunch of filenames with no path included
(no C:\...) eg: my_file2.jpg, my_file1.bmp, etc...
and m_path stores the path, eg: C:\folder1
I want to sort this vector according to different criterion, such as
|
by: gianluca |
last post by:
Hy list
I have to build a function for pairwise comparation of matrix
elements. The most logical approach is with several for(;;) but my
matrix is very large (5000 x 5000) elements (is a raster geographical
map) and required a huge machine resource. Is there a more efficient
algorithm for my pourpuse.
Thanks
Gianluca
|
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: 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...
| |
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?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |