Map with two key values

I have an appointment where I need to go through all the files in a folder. For each file, I need to know each unique file extension, the number of files for each unique file extension, and the total size for each unique file extension. I should be able to sort this using either the file extension or the total file extension size. The first thing I thought about using was a map. This will keep track of each unique file extension and the number of times the file extension has been detected. How to associate the total file extension size with my card? So, for example, I need the result to be something like this:

Using the file extension to sort .cpp: 1: 3400 .exe: 3: 3455600 .mp4: 25: 200000404

Using the total file extension size to sort .mp4: 25: 200000404 .exe: 3: 3455600 .cpp: 1: 3400

Here is the code that I have after some editing:

#include <iostream> #include <filesystem> #include <map> using namespace std; using namespace std::tr2::sys; class fileInfo { public: int fileCount; long long fileSize; }; void scan(path f) { map<string, fileInfo> fileMap; cout << "Scanning = " << system_complete(f) << endl; directory_iterator d(f); directory_iterator e; for( ; d != e; ++d) { path p = d->path(); cout << "\nExtension is: " << extension(p) << "\tFile size is: " << file_size(p) << endl; fileMap[extension(p)].fileCount ++; fileMap[extension(p)].fileSize += file_size(p); } for (map<string, fileInfo>::iterator it = fileMap.begin(); it != fileMap.end(); ++it) { cout << it->first << " : " << it->second.fileCount << " : " << it->second.fileSize << endl; } } int main(int argc, char* argv[] ) { path folder = ".."; scan(folder); return 0; } 

EDIT: So, I implemented the fileInfo file. This is a kind of work. But I have a problem with the size file. After the first run through the loop, it correctly returns file_size, but for each other run through the loop, file_size returns 0.

+4
source share
3 answers

You can create one map:

  map<string, vector<int> > fileExtToSizeMap; 

Basically, for each file extension in string format, if the current file is associated with this extension, put the file size in the vector vector<int> . After you finish scanning the directory, the size of this vector will tell you how many files are associated with the file extension , and the sum of the vector will tell you the total size for each unique file extension . Then you will be answered with two main questions:

0
source

I would create a data structure like ExtensionStat like this:

 struct ExtensionStat { int totalFile; int totalSize; }; 

And have a map from the extension name in stat

 map<string, ExtensionStat> extMap; 

Then you simply extMap over the contents of your folder, collecting statistics in extMap . Keep in mind that you do not need to use a struct, you can also use a class. You can also choose the map to be map<string, ExtensionStat*> extMap; if you want to avoid completely copying values

0
source

You can also use a multi-card.

 map<string, size_t> fileExtSizeMap; 

Just add a new entry for each file found.

std :: lower_bound and std :: upperbound can be used to find this structure. Use std :: distance in the resulting iterators to get the number of files.

Or just iterate over the whole map, increasing the counter when you go.

0
source

All Articles