【问题标题】:How to fix "<hash_map> is deprecated and will be REMOVED. Please use <unordered_map>"?如何修复“<hash_map> 已弃用并将被删除。请使用 <unordered_map>”?
【发布时间】:2018-12-23 14:08:55
【问题描述】:

我正在学习 C++,所以我正在阅读Programming: Principles and Practice using C++一书。

我正在进行第一次练习,即如何制作“Hello, World!”程序使用Microsoft Visual Studio 2015。我使用了书中提供的源代码。

#include "../../std_lib_facilities.h"  //header file recommended by book
int main()                             //C++ programs start by executing the function main
{
    cout <<"Hello, World!\n";   //output "Hello, World!"
    keep_window_open();         //wait for a character to be entered
    return 0;
}

但是,当我尝试构建可执行程序时收到两个错误,如下所示:

IntelliSense: static assertion failed with "<hash_map> is    
deprecated and will be REMOVED. Please use <unordered_map>. You can define
_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to acknowledge that you have 
received this warning." 
Project: Hello, World!  
c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\hash_map 
Line 14

Error   C2338   <hash_map> is deprecated and will be REMOVED. Please use 
<unordered_map>. You can define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS to 
acknowledge that you have received this warning.    
Project: Hello, World!  
c:\program files (x86)\microsoft visual studio 14.0\vc\include\hash_map 
Line 17

你能告诉我如何纠正这个问题吗?

std_lib_facilities.h中的代码如下:

/*
    simple "Programming: Principles and Practice using C++" course header to
    be used for the first few weeks.
    It provides the most common standard headers (in the global namespace)
    and minimal exception/error support.

    Students: please don't try to understand the details of headers just yet.
    All will be explained. This header is primarily used so that you don't     have
    to understand every concept all at once.

    Revised April 25, 2010: simple_error() added
*/

#ifndef H112
#define H112 201004L

#include<iostream>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include<vector>
#include<algorithm>
#include<stdexcept>

//---------------------------------------------------------------------------    ---

#ifdef _MSC_VER
#include <hash_map>
using stdext::hash_map;
#else
#include <ext/hash_map>
using __gnu_cxx::hash_map;

namespace __gnu_cxx {

    template<> struct hash<std::string>
    {
        size_t operator()(const std::string& s) const
        {
            return hash<char*>()(s.c_str());
        }
    };

} // of namespace __gnu_cxx
#endif

//---------------------------------------------------------------------------    ---

#define unordered_map hash_map

//---------------------------------------------------------------------------    ---

typedef long Unicode;

//---------------------------------------------------------------------------    ---

using namespace std;

 template<class T> string to_string(const T& t)
{
    ostringstream os;
    os << t;
    return os.str();
}

struct Range_error : out_of_range { // enhanced vector range error reporting
    int index;
    Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i)     { }
};


// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
    typedef typename std::vector<T>::size_type size_type;

    Vector() { }
    explicit Vector(size_type n) :std::vector<T>(n) {}
    Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
    template <class I>
    Vector(I first, I last) :std::vector<T>(first,last) {}

    T& operator[](unsigned int i) // rather than return at(i);
    {
        if (i<0||this->size()<=i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
    const T& operator[](unsigned int i) const
    {
        if (i<0||this->size()<=i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {

    String() { }
    String(const char* p) :std::string(p) {}
    String(const string& s) :std::string(s) {}
    template<class S> String(S s) :std::string(s) {}
    String(int sz, char val) :std::string(sz,val) {}
    template<class Iter> String(Iter p1, Iter p2) : std::string(p1,p2) { }

    char& operator[](unsigned int i) // rather than return at(i);
    {
        if (i<0||size()<=i) throw Range_error(i);
        return std::string::operator[](i);
    }

    const char& operator[](unsigned int i) const
    {
        if (i<0||size()<=i) throw Range_error(i);
        return std::string::operator[](i);
    }
};

#ifndef _MSC_VER
namespace __gnu_cxx {

    template<> struct hash<String>
    {
        size_t operator()(const String& s) const
        {
            return hash<std::string>()(s);
        }
    };

} // of namespace __gnu_cxx
#endif


struct Exit : runtime_error {
    Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
    throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
    error(s+s2);
}

inline void error(const string& s, int i)
{
     ostringstream os;
    os << s <<": " << i;
    error(os.str());
}

#if _MSC_VER<1500
    // disgusting macro hack to get a range checked string:
    #define string String
    // MS C++ 9.0 have a built-in assert for string range check
    // and uses "std::string" in several places so that macro substitution      fails
#endif

template<class T> char* as_bytes(T& i)  // needed for binary I/O
{
    void* addr = &i;    // get the address of the first byte
                        // of memory used to store the object
    return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
    cin.clear();
    cout << "Please enter a character to exit\n";
    char ch;
    cin >> ch;
    return;
}

inline void keep_window_open(string s)
{
    if (s=="") return;
    cin.clear();
    cin.ignore(120,'\n');
    for (;;) {
        cout << "Please enter " << s << " to exit\n";
        string ss;
        while (cin >> ss && ss!=s)
            cout << "Please enter " << s << " to exit\n";
        return;
    }
}



// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s)  // write ``error: s?? and exit program
{
    cerr << "error: " << s << '\n';
    keep_window_open();     // for some Windows environments
    exit(1);
}

// make std::min() and std::max() accessible:
#undef min
#undef max

#include<iomanip>
inline ios_base& general(ios_base& b)   // to augment fixed and scientific
{
    b.setf(ios_base::fmtflags(0),ios_base::floatfield);
    return b;
}

// run-time checked narrowing cast (type conversion):
template<class R, class A> R narrow_cast(const A& a)
{
    R r = R(a);
    if (A(r)!=a) error(string("info loss"));
    return r;
}


inline int randint(int max) { return rand()%max; }

inline int randint(int min, int max) { return randint(max-min)+min; }

inline double sqrt(int x) { return sqrt(double(x)); }   // to match C++0x

#endif

【问题讨论】:

  • 如果您正在使用本书的第二版(应该),请使用标题的this version。如果必须使用第一版,请在#include &lt;hash_map&gt; 之前添加#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
  • Please use &lt;unordered_map&gt; 非常明确。如果您不担心学习过时的编程技术,您的书很旧,请找一本更好的书,或者继续阅读它。你应该担心,它也不会教你任何关于 C++11 的知识。这不好。
  • 供参考(在 Quora 上)Why has hash_map been deprecated in C++?;看起来它已经被贬值了,因为该功能已被移至标准库,但还有其他原因吗?旧的预标准代码呢?为什么要为此打破向后兼容性?

标签: c++ visual-studio-2015


【解决方案1】:

有几种方法可以修复它。您可以通过编写让编译器在 Visual Studio 中忽略它

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS 1

在他的头文件的顶部(你可能会在他的系列的其余部分没问题)。

您也可以将他的标头改写为使用unordered_map 而不是hash_map。我不建议您这样做,因为您是编程新手,但是 Stack Overflow 上的一些善良的灵魂可能会为您做到这一点,您可以复制它(一点也不难)。 编辑:T.C.在您帖子的 cmets 部分找到了 here

最后,您可以在书中包含您正在执行的任何单元的必要头文件,并根据需要手动编写他正在使用的任何功能。在这种情况下,要访问 cout 函数,您需要在文件顶部写入 #include &lt;iostream&gt;,然后还要在 int main() 之前写入 using namespace std;。或者,您可以编写 std::cout &lt;&lt; "Hello World!"; 而不包括 using namespace std;,仅此而已。最终产品如下所示:

#include <iostream>  //for learning, it's probably better to know what each header file does before you use it. silly book.
using namespace std; //act like std:: is before function calls from that library
int main()                             //C++ programs start by executing the function main
{
    cout <<"Hello, World!\n";   //output "Hello, World!"
    cin.get();         //wait for a return character to be entered
    return 0;
}

考虑到你一开始是如何在 Stack Overflow 上发帖的,并且可能有很高的学习 C++ 的能力,我建议你做后者,并在你读到他的书中以及之后尝试看看他的函数在做什么在互联网上快速搜索您想学习的任何很酷的东西。

【讨论】:

  • unordered_map 非常接近 hash_map 的直接替代品。一个简单的搜索和替换应该没问题。
  • @Puppy 假设有一个#define unordered_map hash_map,这样做可能非常容易
【解决方案2】:

对于OP的具体问题,我认为最好的解决方案是下载std_lib_facilities.hhere的更新版本。新文件使用unordered_map 而不是hash_map

【讨论】:

    【解决方案3】:

    您可以在源文件中输入以下内容

    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    inline void keep_window_open() { char ch; cin >> ch; }
    

    或者用这些创建一个头文件。

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 2013-12-14
      • 2014-04-04
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 2023-02-19
      相关资源
      最近更新 更多