Software & Finance

Visual C++ STL Using Map - inserting elements, and const_iterator

Here is the sample code for STL Map of two strings. The first string is a key field to store the filename and second string is the value field to store last file write time.

When accessing, I have used const_iterator for iterating through the map elements. The iterator it != map.end() represents, whether we reached the end of the map or not. ++it (preincrement is faster) refers to move to next item.

The it->first is used to access the key field and it->second is used to access the value field.


#include <string>

#include <vector>

#include <windows.h>


// searchkey = "c:\\kathir\\*.txt";

int GetFileList(const char *searchkey, std::map<std::string, std::string> &map)


    WIN32_FIND_DATA fd;

    HANDLE h = FindFirstFile(searchkey,&fd);




        return 0; // no files found





        char buf[128];

        FILETIME ft = fd.ftLastWriteTime;

        SYSTEMTIME sysTime;

        FileTimeToSystemTime(&ft, &sysTime);

        sprintf(buf, "%d-%02d-%02d",sysTime.wYear, sysTime.wMonth, sysTime.wDay);


        map[fd.cFileName] = buf;


        if(FindNextFile(h, &fd) == FALSE)



    return map.size();



void main()


    std::map<std::string, std::string> map;

    int count = GetFileList("c:\\kathir\\*.txt", map);


    // Using const_iterator


    for(std::map<std::string, std::string>::const_iterator it = map.begin();

        it != map.end(); ++it)