Map with two key values
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Map with two key values

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Map with two key values

    I have an assignment where I need to go through all the files in a folder. For each file I need to know each unique file extension, how many files for each unique file extension, and the total size for each unique file extension. I have to be able to sort through this using either the file extension or the total size of the file extension. The first thing I thought of using was a map. This will keep track of each unique file extension and the amount of times that file extension was found. How do I now associate the total size of the file extension to my map? So for example I need the output to be something like this:

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

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

    Here is the code I have so far:

    Code:
    #include <iostream>
    #include <filesystem>
    #include <map>
    
    using namespace std;
    using namespace std::tr2::sys;
    
    void scan(path f)
    {
    	map<string, int> map;
    	cout << "Scanning = " << system_complete(f) << endl;
    	directory_iterator d(f);
    	directory_iterator e;
    	for( ; d != e; ++d)
    	{
    		path p = d->path();
    		int temp = file_size(p);
    		map[extension(p)]++;
    	}
    }
    
    int main(int argc, char* argv[] )
    {
    	path folder = "..";
    
    	scan(folder);
    
    	return 0;
    }
    I was thinking of somehow using a class to implement this, but don't know how to go about doing that. Any input will help, thanks for you time.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,429

    Re: Map with two key values

    Quote Originally Posted by dhines87 View Post
    I have an assignment where I need to go through all the files in a folder. For each file I need to know each unique file extension, how many files for each unique file extension, and the total size for each unique file extension.
    1) Your code has nothing to do with Visual C++ as I see no Windows, MFC, ATL, or other Visual C++ specific entities in your code. It is purely a C++ example using the boost library. Therefore it should have been posted in the Non-Visual C++ forum

    2) Learn to use structs and classes. You can't design a system as you've described without learning how to group common items together into a single entity. Otherwise you have variables all over the place with no cohesion and relationship to each other (as your code is doing now).

    Code:
    #include <map>
    struct FileInfo
    {
        int numFiles;
        int totalSize;
        FileInfo : numFiles(0), totalSize(0) {}
    };
    
    typedef std::map<string, FileInfo> FileInfoMap;  // string is the extension, and FileInfo is complete information pertaining to that extension
    Now each unique name will have a FileInfo associated with it.
    Code:
    FileInfoMap fInfo;
    //...
    FileInfoMap::iterator it = fInfo.find("exe");
    if ( it != fInfo.end() )
    {
       it->second. /* you now adjust the info for the files here */
    }
    That is the idea of how to do this. I didn't finish the rest since it is your assignment.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center