Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7740 > unrolled thread

hash_set was not declared even I #include<hash_set>

Started byeric <cneric12lin0@gmail.com>
First post2011-07-11 23:42 -0700
Last post2011-07-12 03:27 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  hash_set was not declared even I #include<hash_set> eric <cneric12lin0@gmail.com> - 2011-07-11 23:42 -0700
    Re: hash_set was not declared even I #include<hash_set> Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-12 00:48 -0700
      Re: hash_set was not declared even I #include<hash_set> Miles Bader <miles@gnu.org> - 2011-07-12 17:19 +0900
        Re: hash_set was not declared even I #include<hash_set> Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-12 03:27 -0700

#7740 — hash_set was not declared even I #include<hash_set>

Fromeric <cneric12lin0@gmail.com>
Date2011-07-11 23:42 -0700
Subjecthash_set was not declared even I #include<hash_set>
Message-ID<e09fa4e0-d9c3-42ab-afce-727823696ce7@p19g2000prg.googlegroups.com>
Dear advanced c/g++ programers:
  I tried the following code
-----------------------------------------------------------------------------------------
// Example 6-8 Storing strings in a hast_set
#include <iostream>
#include <string>
#include <hash_set>
using namespace std;

int main() {

  hash_set<std::string> hsString;
  string s = "bravo";

  hsString.insert(s);
  s = "alpha";
  hsString.insert(s);
  s = "charlie";
  hsString.insert(s);

  for (hash_set<string>::const_iterator p = hsString.begin();
       p != hsString.end(); ++p)
     cout << *p << endl;    // Note that these aren't guaranteed
			    // to be in sorted order
}
---------------------------------------------------------------------------------------------------
but I got the following compile error(g++ 4.5.2)
----------------------------------------------------------------
eric@eric-laptop:~/cppcookbook/ch6$ g++ -Wno-deprecated Example6-8.cpp
Example6-8.cpp: In function ‘int main()’:
Example6-8.cpp:9:3: error: ‘hash_set’ was not declared in this scope
Example6-8.cpp:9:23: error: expected primary-expression before ‘>’
token
Example6-8.cpp:9:25: error: ‘hsString’ was not declared in this scope
Example6-8.cpp:18:23: error: expected primary-expression before ‘>’
token
Example6-8.cpp:18:24: error: ‘::const_iterator’ has not been declared
Example6-8.cpp:18:41: error: expected ‘;’ before ‘p’
Example6-8.cpp:19:8: error: ‘p’ was not declared in this scope
eric@eric-laptop:~/cppcookbook/ch6$
---------------------------------------------------------------------
your can get the code to test by yourself
source code
http://examples.oreilly.com/9780596007614/

Thanks your help a lot in advance, Eric

[toc] | [next] | [standalone]


#7744

FromMichael DOUBEZ <michael.doubez@free.fr>
Date2011-07-12 00:48 -0700
Message-ID<c9675133-084a-406d-9fbd-9476d3841391@h12g2000vbx.googlegroups.com>
In reply to#7740
On Jul 12, 8:42 am, eric <cneric12l...@gmail.com> wrote:
>   I tried the following code
[snip]
> #include <string>
> #include <hash_set>
> using namespace std;
[snip]
>   hash_set<std::string> hsString;
[snip]
> Thanks your help a lot in advance, Eric

hash_set is not part of c++. It was specified in STL SGI but was not
included in the standard. g++ provides it as a backward compatibility
in the namespace __gnu_cxx.

Add:
using __gnu_cxx::hash_set;

Note that no default hash function is provided for string, you have to
define it:
namespace __gnu_cxx {
  template<> struct hash< std::string > {
    size_t operator()( const std::string& x ) const {
      return hash< const char* >()( x.c_str() );
    }
  };
}

--
Michael

[toc] | [prev] | [next] | [standalone]


#7745

FromMiles Bader <miles@gnu.org>
Date2011-07-12 17:19 +0900
Message-ID<buohb6rgbck.fsf@dhlpc061.dev.necel.com>
In reply to#7744
Michael DOUBEZ <michael.doubez@free.fr> writes:
> hash_set is not part of c++. It was specified in STL SGI but was not
> included in the standard. g++ provides it as a backward compatibility
> in the namespace __gnu_cxx.

For a non-ancient version of g++ (or any other compiler with
sufficient c++0x support), wouldn't it be better to just use
"unordered_set" instead?  I thought that was basically just hash_set
with a different name (but an actual part of the standard)...

-Miles

-- 
It wasn't the Exxon Valdez captain's driving that caused the Alaskan oil spill.
It was yours.  [Greenpeace advertisement, New York Times, 25 February 1990]

[toc] | [prev] | [next] | [standalone]


#7748

FromMichael DOUBEZ <michael.doubez@free.fr>
Date2011-07-12 03:27 -0700
Message-ID<d512e3d2-2830-4726-82a2-f46f968756fb@y13g2000yqy.googlegroups.com>
In reply to#7745
On Jul 12, 10:19 am, Miles Bader <mi...@gnu.org> wrote:
> Michael DOUBEZ <michael.dou...@free.fr> writes:
> > hash_set is not part of c++. It was specified in STL SGI but was not
> > included in the standard. g++ provides it as a backward compatibility
> > in the namespace __gnu_cxx.
>
> For a non-ancient version of g++ (or any other compiler with
> sufficient c++0x support), wouldn't it be better to just use
> "unordered_set" instead?

Yes. Even TR1 support is enough.

> I thought that was basically just hash_set
> with a different name (but an actual part of the standard)...

But the OP is trying to compile examples from C++CookBook.

--
Michael

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web