I have asked this question before, but it wasn't clear enough! Please do not remove it! the other question was closed.
I have two vector structs, one contains the codes and cost for each, and another is just orders that contains requested codes and totalCost (to be calculated)
vector parts(codes, cost);
vector orders(codes, totalCost); //totalCost set to 0;
where the parts vector contains something like ('A', 10, 'B', 20);
Where A and B are parts, 10 is cost for A, 20 cost for B.
and the orders vector contains orders such as A,B,C or just A,B Say we receive an order of parts A and B.
how do we go about adding these two based on cost from parts and storing the result in a new int that we gonna use later to add to the vector struct orders (totalCost).
I was trying for several hours, I think I need to use std::transform and maybe std::accumulate? just not sure how to put the two together...
This is what I have written so far:
int totalCost;
for(auto i : customers){
for(auto x : parts){
totalCost = accumulate(i.partsList.begin(), i.partsList.end(), ???);
//then need to store cost for each order for use later
}
I have also tried:
void test(){
int cost;
for(unsigned i =0; i < customers.size(); i++){
for(unsigned x=0; x < partsVec.size(); x++){
for(unsigned z=0; z < customers.at(i).partsList.size(); z++){
if(partsVec.at(x).code == customers.at(i).partsList.at(z)){
cost += partsVec.at(x).cost;
cout << cost << endl;
}
}
}
}
}
This output this:
30
60
80
100
120
140
160 //end of order n 1.
180 // this needs to be reset to 0.
200
220
240 // end of order n 2, this needs to be 80 not the total of both orders
As you can see, this keeps incriminating and I can't figure out a way to stop it.
what I am trying to do, is get the total cost of all parts in partsList based on info from the parts vector. Customers vector has another vector of chars in it called partsList, then I can change the totalCost of each order to value obtained from above using accumulate or some other algorithm
Please help!
Aucun commentaire:
Enregistrer un commentaire