C++ count and map

前端 未结 5 1120
难免孤独
难免孤独 2021-01-22 21:54

I am counting the number of times every word occurs in a text file. I would like to avoid cases and hence am doing tolower to my input and then counting. I have a map data struc

5条回答
  •  终归单人心
    2021-01-22 22:56

    This should work. For multiple cases the first case will be inside the map and not lower case. Also the solution uses only one map as you wanted

    using namespace std;
    
    struct StrCaseInsensitive
    {
        bool operator() (const string& left , const string& right )
        {
            return _stricmp( left.c_str() , right.c_str() ) < 0;
        }
    };
    
    int main(void)
    {
        char* input[] = { "Foo" , "bar" , "Bar" , "FOO" };
        std::map CountMap;
    
        for( int i = 0 ; i < 4; ++i )
        {
            CountMap[ input[i] ] += 1;
        }
        return 0;
    }
    

提交回复
热议问题