Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have a toatl of four unsigned char arrays, which are added into a map container like the following:
std::map<std::string, unsigned char*> UCArray;
UCArray.insert(std::make_pair("A1", new unsigned char[10000])); 
UCArray.insert(std::make_pair("A2", new unsigned char[10000])); 
UCArray.insert(std::make_pair("A3", new unsigned char[10000])); 
UCArray.insert(std::make_pair("A4", new unsigned char[10000])); 

I want to get an array filled with average values based on UCArray, in which all four unsigned char buffers are initialized and populated with valid values. I know that I can carry my point through the use of two "for ... loop". Would please tell if there is any other effective way(s) to do that. Thank you in advance.
Posted

1 solution

No, there is no better way than using two loops, except if you accept unrolling the loop on the first index.

Anyway, you'd better avoid indexing the array inside the inner loop as this would cost you the overhead of the map lookup repeated 40000 times !

It is much better to keep an array of pointers to the four buffers and index them.

// Retrieve the buffers
unsigned char* Buffer[4];
Buffer[0]= UCArray["A1"];
Buffer[1]= UCArray["A2"];
Buffer[2]= UCArray["A3"];
Buffer[3]= UCArray["A4"];

for (int i= 0; i < 10000; i++)
{
    int Avg= 0;
    Avg+= Buffer[0][i];
    Avg+= Buffer[1][i];
    Avg+= Buffer[2][i];
    Avg+= Buffer[3][i];
    Avg/= 4;

    // Store the average
    ...
}

By the way, you could also do without the map container...

You didn't specify if you want the row or column averages. The other case is even simpler (a single pointer suffices).
 
Share this answer
 
v5
Comments
Golden Lee 16-Jul-11 10:58am    
@YDaoust, thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900