如何修复“< hash_map>被驱逐,将被删除。请使用< unordered_map>&quot ;? [英] How to fix "<hash_map> is depracated and will be REMOVED. Please use <unordered_map>"?

查看:3951
本文介绍了如何修复“< hash_map>被驱逐,将被删除。请使用< unordered_map>&quot ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者,对学习C ++非常感兴趣,因此我正在编写编程:使用C ++的原则和实践

I am a beginner and very interested in learning C++ so I am working through the book Programming: Principles and Practice using C++.

我在第一次演习,这是如何使用Microsoft Visual Studio 2015制作一个Hello,World!程序。我使用本书中提供的源代码。

I am on the first drill, which is how to make a 'Hello, World!' program using Microsoft Visual Studio 2015. I have used the source code as provided in the book.

#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;
}

但是,当我尝试构建一个可执行程序时,如下所示:

However, I receive two errors when I try to build an executable program which are as follows:

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

请向我解释如何纠正这个问题?

Please could you explain to me how I can rectify this?

非常感谢!

std_lib_facilities.h中的代码如下:

The code in the std_lib_facilities.h is as follows:

/*
    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


推荐答案

有几种方法可以解决它。您可以让编译器忽略它在visual studio中写

There are several ways to fix it. You could just have the compiler ignore it in visual studio by writing

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS 1

在他的头文件的顶部(你可能会对他的系列的其余部分很好)。

at the top of his header file (and you'll likely be fine for the rest of his series).

也重写他的头而不是 hash_map 使用 unordered_map 。我不推荐这,因为你是新的编程,但某些类型的灵魂上Stack Overflow可以为你做,你可以复制它(应该不是很难)。
编辑:T.C.已在您的帖子的评论部分此处找到它。

You could also rewrite his header to instead of hash_map use unordered_map. I do not recommend this since you are new to programming, but some kind soul on Stack Overflow may do that for you and you can copy it (shouldn't be hard at all). T.C. has found it here in the comments section of your post.

最后,你可以为书中正在做的任何单元添加必要的头文件,并根据需要手动编写他正在使用的函数。在这种情况下,要访问cout函数,您需要在文件的顶部写入 #include ,然后使用命名空间写入 std; int main()之前。或者,你可以写 std :: cout<< Hello World!; 不包括 using namespace std; ,这就是它所做的。最终产品将如下所示:

Lastly, you could just include the necessary header files for whatever unit you are doing in the book, and manually write whatever functions he is using as you need them. In this case to access the cout function you need to write #include <iostream> at the top of your file, and then also write using namespace std; before your int main(). Alternatively, you could write std::cout << "Hello World!"; without including using namespace std;, that's all it does. The final product would look like:

#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 ++的能力,我建议做后者,试图看看他的功能是做什么,一旦你到他的书,然后在互联网上快速搜索你想学习的任何酷的东西。

Seeing as to how you are posting on Stack Overflow to begin with and probably have a high aptitude for learning C++, I recommend doing the latter and trying to see exactly what his functions are doing once you get there in his book and afterwards doing a quick search on the internet for whatever cool things you want to learn.

这篇关于如何修复“&lt; hash_map&gt;被驱逐,将被删除。请使用&lt; unordered_map&gt;&quot ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆