Getting a count of intervals The statement of the problem is there are 2 files, one has a set of intervals i.e. 0-10, 3-40, etc which may be repetitive. The second file has a set of numbers which are also repetitive. The exercise is to get a count of the intervals, a number in the 2nd file would fit into.

Intervals.txt

0 12
2 36
6 98
2 36


Numbers.txt

2
4
123
20


What I thought of as a  possible solution was keep a count of intervals when you populate a map of intervals. So if you encounter an interval 3-40 5 times, you keep the count as 5 so you don't try to populate a map 5 times with the same interval. How it helps downstream is, if a number is in a specific interval you can count its existence in 5 intervals. Secondly there is a map of found numbers in your previous iterations, so that you don't loop through the intervals again.

I have been told, by some random guy, that my implementation is ok but not what he desires. No further explanations. I have tried raking my brain but haven't got any further. Any pointers or help would be much appreciated. Any further clarifications, please ask.

void generateCounts(const string&amp; fname1, const string&amp; fname2)
{
    std::ifstream file(fname1.c_str());
    string line;
    map&lt;int, pair&lt;int, int&gt; &gt; extents;
    map&lt;int, pair&lt;int, int&gt; &gt;::iterator it;
    std::string left;
    std::string right;

    if(file.is_open())
    {
        while(file.good())
        {
            getline(file, line);
            string::size_type pos = line.find(' '); 
            left = line.substr(0, pos);
            right = line.substr((++pos));
            int ll = atoi(left);
            int up = atoi(right);
            it = extents.find(ll);
            if(it != extents.end()) 
            {
                if(it-&gt;second.first != up){
                    extents.insert(make_pair&lt;const int, pair&lt;int, int&gt; &gt;(ll, make_pair&lt;int, int&gt;(up, ++count)));
                }
                else{
                    it-&gt;second.second += 1 ;
                }
            }
            else
            {
                extents.insert(make_pair&lt;const int, pair&lt;int, int&gt; &gt;(ll, make_pair&lt;int, int&gt;(up, ++count)));
            }
            count = 0;
        }
    }
    fclose(inp_file);

    std::ifstream file1(fname2.c_str());
    map&lt;int, int&gt; found;
    map&lt;int, int&gt;::iterator it1;
    string str;
    if(file1.is_open())
    {
        while(file1.good())
        {
            count = 0;
            getline(file1, str);
            if(str.empty()) continue;
            int var = atoi(str.c_str());
            it1 = found.find(var);
            if (it1 == found.end())
            {
                for(it = extents.begin(); it != extents.end(); ++it)
                {
                    if((var &gt;= it-&gt;first ) &amp;&amp; (var &lt;= it-&gt;second.first))
                    {
                           count += it-&gt;second.second ;
                    }
                }
                found.insert(make_pair(var, count));
            }
            else
                count = it1-&gt;second;

            cout &lt;&lt; count &lt;&lt; "\n";
            str.clear();
    }
    file1.close();
}
OK. Since this is homework I am going to limit my points to hints (I may add more in a week or so).


Why are you doing this?

if(file.is_open())

This is an anti-pattern (in every language not just C++):
Remember the eof (part of not being good) does not happen until you read past the end of line.

Lots of examples on SO (go look it up).

    while(file.good())
    {
        getline(file, line);

You are doing too much work here:

There is an easy way to read integers from a stream.  Lots of examples on SO (go look it up).

    getline(file, line);
    string::size_type pos = line.find(' '); 
    left = line.substr(0, pos);
    right = line.substr((++pos));   // Also this is wrong if no space was found.
    int ll = atoi(left);
    int up = atoi(right);

Why do a 2-phase compare?

    if(it != extents.end()) 
    {
        if(it-&gt;second.first != up){
            extents.insert(make_pair&lt;const int, pair&lt;int, int&gt; &gt;(ll, make_pair&lt;int, int&gt;(up, ++count)));
        }
        else{
            it-&gt;second.second += 1 ;
        }
    }
    else
    {
        extents.insert(make_pair&lt;const int, pair&lt;int, int&gt; &gt;(ll, make_pair&lt;int, int&gt;(up, ++count)));
    }


If you make the key std::pair&amp;lt;left, right&amp;gt; then you only need to do one test. Not this complicated mess. It also makes more sense to keep the left and right together.
Don't do this: (see Implementation using fstream failed evaluation)

fclose(inp_file);


Same comments about the second loop:

This is not wrong. But when I compare a number against two others of a range I put the value that is being tested for inclusion in the range in the middle of the comparison. I personally find it nicer to read this way:

 if((var &gt;= it-&gt;first ) &amp;&amp; (var &lt;= it-&gt;second.first))

 // I prefer

 if ((it-&gt;first &lt;= var) &amp;&amp; (var &lt;= it-&gt;second.first))