ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1496. Spammer

Microsoft Visual Studio 2008 / Intel C++ Compiler Discrepancy
Posted by Varun Sharma 30 Apr 2009 17:09
Hi,

I got compilation error with the following C++ program. This program runs perfectly in Microsoft Visual Studio 2008. I cannot understand why it produced compilation error ! As a result, I had to write it in C#.

#include <iostream>
#include <set>
#include <vector>
#include <hash_map>
#include <string>

using namespace std;

int main(){
    int n = 0;
    set<string> s;
    vector<string> v;
    hash_map<string, int> hm;

    cin>>n;
    for(int i = 0; i < n; i++){
        string temp = "";
        cin>>temp;
        s.insert(temp);
        v.push_back(temp);
    }

    set<string>::iterator it = s.begin();

    for(size_t i = 0; i < s.size(); i++){
        hm.insert(make_pair(*it, 0)); // ERROR IN THIS LINE FOR SOME REASON
        it++;
    }

    for(size_t i = 0; i < v.size(); i++){
        hash_map<string, int>::iterator it = hm.begin();
        it = hm.find(v[i]);
        (it->second)++;
    }

    hash_map<string, int>::iterator hit = hm.begin();

    for(size_t i = 0; i < hm.size(); i++){
        if(hit->second > 1){
            cout<<hit->first<<endl;
        }
        hit++;
    }
}

Here is the error message:-

7c683836-7a29-4ffc-849b-00c6921bbbe5
S:\checker\compile\\Vc7\include\xhash(38): error: no suitable conversion function from "const std::string" to "size_t={unsigned int}" exists
          return ((size_t)_Keyval);

                          ^
          detected during:
            instantiation of "size_t={unsigned int} std::hash_compare<_Kty, _Pr>::operator()(const _Kty &) const [with _Kty=std::string, _Pr=std::less<std::string>]" at line 217
            instantiation of "std::_Hash<_Tr>::_Pairib std::_Hash<_Tr>::insert(const std::_Hash<_Tr>::value_type &) [with _Tr=std::_Hmap_traits<std::string, int, std::hash_compare<std::string, std::less<std::string>>, std::allocator<std::pair<const std::string, int>>, false>]" at line 28 of "7c683836-7a29-4ffc-849b-00c6921bbbe5"

compilation aborted for 7c683836-7a29-4ffc-849b-00c6921bbbe5 (code 2)

Thanks
Re: Microsoft Visual Studio 2008 / Intel C++ Compiler Discrepancy
Posted by Fyodor Menshikov 2 May 2009 14:01
hash_map is not in standard C++ library yet. It is Microsoft extension. You use it on your own risk.

It seems that Intel C++ library can only hash values that can be converted into size_t using type cast operator, and std::string has not such cast.

It is _very_ simple problem, you do not need very fast data structure here. std::map can be used. It is the simplest solution.

If anyway it is required to use hash_map, then I'd suggest to write wrapper compatible with std::string and int and use the wrapper as key type of hash_map:

class HashableString {

   std::string _value;

public:

   HashableString(const std::string &value)
      : _value(value)
   {}

   operator std::string() const {
      return _value;
   }

   operator size_t() const {
      return _value.size(); // bad hash!
   }
};

std::ostream & operator << (std::ostream &out, const HashableString &value) {
   return out << (std::string)value;
}
Re: Microsoft Visual Studio 2008 / Intel C++ Compiler Discrepancy
Posted by Varun Sharma 2 May 2009 16:45
Hi,

Thanks Fyodor. You are absolutely right. The hashmap over here does not support any other keys except for size_t. But the next C++ standard will have hash_map as part of C++.

But thanks for your detailed explanation and time (may be that's why you are No. 40 :-))

Varun