James @ Minsoft

Your Rank: 92
Points: 2
Here is an updated version with bugs removed;
I wish to find the set_difference between two vector containers with non-unique values.
The program below illustrates the problem. The answer I get is
data3[0].element1 = 1
data3[1].element1 = 2
data3[2].element1 = 2
data3[3].element1 = 4
data3[4].element1 = 4
data3[5].element1 = 5
data3[6].element1 = 5
data3[7].element1 = 0
whereas, the answer I want is;
data3[0].element1 = 1
data3[1].element1 = 5
data3[2].element1 = 5
i.e. all 2's and 4's removed. Any ideas how my code could be modified to do this?
Here is the current program example:
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
struct data {
int element1;
int element2;
};
int i, records;
std::vector<data> data1(10), data2(10), data3(10);
std::vector<data>::iterator it_difference;
struct difference {
bool operator()(const data& d1, const data& d2) const {
return (d1.element1 < d2.element1);
}
};
int main()
{
data1[0].element1 = 1;
data1[1].element1 = 2;
data1[2].element1 = 2;
data1[3].element1 = 2;
data1[4].element1 = 4;
data1[5].element1 = 4;
data1[6].element1 = 4;
data1[7].element1 = 5;
data1[8].element1 = 5;
data2[0].element1 = 2;
data2[1].element1 = 4;
it_difference = std::set_difference(data1.begin(), data1.end(),
data2.begin(), data2.end(),
data3.begin(), difference());
data3.resize(it_difference-data3.begin());
records = std::distance(data3.begin(), it_difference);
for(i = 0; i < records; i++)
std::cout << "data3[" <<i<<"].element1 = "<< data3.element1 << "\n";
}
Read More...
Hello,
I have following code;
struct data {
int element1;
int element2;
};
vector<data> data1(5), data2(5), data3(5)
data1[0].element1 = 1;
data1[1].element1 = 2;
data1[2].element1 = 2;
data1[3].element1 = 3;
data1[4].element1 = 3;
data1[5].element1 = 4;
data2[0].element1 = 2;
data2[1].element1 = 3;
struct comp {
bool operator()(const data& data1, const data& data2) const
{
return (data1.element1 != data2.element1);
};
std::set_difference(data1.begin(), data1.end(), data2.begin(),
data2.end(), data3.begin(), comp());
The result I get is data3.element1 = {1,2,3,4}
The result I want is data3.element1 = {1,4}
i.e. I want all occurrences of 2 and 3 removed, not just the first ones.
I was wondering what modifications I could make to do this?
I prefer to do it in one pass of the set_difference command, as I need for it
to run as fast as possible.
Thankyou,
James
Read More...
Hello,
I was wondering if set_intersection can be used with classes or does it only work with C++ types
e.g. I have following code with C++ classes;
struct data {
int field1
int commonfield
};
vector<data> data1(5), data2(5), data3(5)
struct comp { // -Checked 12-2-02
bool operator()(const data& lhs, const data& rhs) const
{
return (lhs.commonfield < rhs.commonfield);
}
};
std::set_intersection(data1.begin(), data1.end(), data2.begin(),
data2.end(), data3.begin(), comp());
Does this code then find the intersection based on commonfield, or can I only use C++ types e.g. vector<int> ?
Read More...
Hello,
I have some computer code which I want to speed up with STL. I was wondering if there is a faster implementation of the code presented.
I wish to delete all the elements in array1 which have matching elements in array2. array1 and array2 are not sorted, and I need
to preserve their orders after deletion.
vector<int> array1(1000), array2(20);
int array1_elements, array2_elements;
struct array_remove {
bool operator()(const int& rhs) const
{
return (rhs == -1);
}
k = array1_elements;
for(i = 1; i <= array1_elements; i++) {
for(j = 1; j <= array2_elements; j++) {
if(array1 == array2[j]) {
array1 = -1;
k--;
}
}
}
std::remove_if(array1.begin(), array1.begin() + array1_elements, array_remove());
array1_elements = k;
I was thinking of using 2 sorts and a while statement, but
I was wondering if STL could be used for a faster implementation.
Any comments appreciated.
Thankyou.
Read More...
Basic Information
-
Gender
Male -
Birth Date
1965-08-01 -
About me
Software engineer
Contact Information
-
Mobile phone
0406750668 -
Land phone
+61885221785 -
Address
4 Flynn Court -
State
SA -
City / Town
Evanston -
Country
Australia -
Website
http://www.minsoft.com.au
Education
-
College / University
BEng, MEngSc -
Graduation Year
2009