Reputation: 507
Suppose I have two arrays that look like this:
first array : 8 5 6 1 4 11 7
second array: 1 1 1 1 0 0 0
I want to sort the first array in descending order and the order of the elements in the second array should change in the same way as of first array and remove elements in the first array whose corresponding value in the second array is 0. The elements in the first array whose corresponding value is 0 should go into a different array. At last sum of both arrays should be printed.
so the final array should look like this:
first array : 8 6 5 1
second array: 1 1 1 1
sum= 8+6+5+1=20
new arrays with value:
first array : 11 7 4
second array: 0 0 0
sum = 11+7+4=22
Any ideas how to do this in c++
this is what i have done so far... i tried playing with waytoShort() method:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool wayToSort(int a, int b)
{
return a > b;
}
int main()
{
int n;
int sum;
sum=0;
cout<<"no. of elements in first array: "<<endl;
cin>>n;
//no. of elements in both array should be same.
vector<int> l(n);
vector<int> t(n);
for(int i=0;i<n;i++)
{
cin>>l[i]>>t[i];
}
sort(l.begin(),l.end(),wayToSort);
for(int i=0;i<n;i++)
{
cout<<l[i]<<" "<<t[i]<<endl;
}
for(int j= 0; j<n;j++)
{
sum = sum+l[j];
}
cout<<sum<<endl;
return 0;
}
This only sort the first array.
Upvotes: 0
Views: 829
Reputation: 38919
You're performing operations that indicate that your container type is wrong.
multimap
already accomplishes 1 so you'll never need to sort your keys ("first array".) Given multimap<int, int> arrays
2 can be done like this:
multimap<int, int> newarrays;
auto it = begin(arrays);
while(it != end(arrays)) {
if(it->second != 0) {
++it;
} else {
newArrays.insert(*it);
it = arrays.erase(it);
}
}
Upvotes: -1
Reputation: 56
If I am understanding you correctly, you wish to filter a list of tuples based on the second element, and then you wish to sort a list of tuples based on the first element. I'm not detecting any vital impact on the order of operations, so you should filter first.
Representing the problem as two distinct sets of elements is probably not the way to go (this of course assumes that you are not required to use two separate lists due to some outside constraint).
In keeping with the above, you should use one list, but it should be of pairs (std::pair). Any mutations based in X will drag Y along for the ride implicitly.
As far as removing pairs whose right element == 0. That should be fairly linear. Even if you did it first, the resultant impact on your overall run-time (a single run through an array of pairs, will not be noticed when the sort is where the real heavy lifting happens anyway) will not be noticed.
Sorting pairs is fairly easy:
Sorting a std::vector<std::pair<std::string,bool>> by the string?
Choice of data representation is important. You may want to use an std::vector but since you may be removing a lot of things your performance may suffer. For a large range of datasets this could trigger massive reshuffles. In these cases, it may be better to use std::list.
Upvotes: 0
Reputation: 13
Notice that you can split the array into two parts before sorting the array, and then sort each array alone.
Example:
8 5 6 1 4 11 7
1 1 1 1 0 0 0
Split into:
1) [8 5 6 1],[1,1,1,1]
2) [4 11 17],[0,0,0]
then sort each array alone, result:
1) [8 6 5 1],[1,1,1,1]
2) [17 11 4],[0,0,0]
Upvotes: 1