macos - Mac下C++ STL的配置使用?
高洛峰
高洛峰 2017-04-17 11:28:54
[C++讨论组]

在Mac下默认安装了Xcode是配置好了STL的,像stack,vector,list等基本的类是可以使用的,但在使用/的使用总是报出一大堆问题,请问有谁知道怎么解决吗?

问题

我在尝试着在我的机子上测试如下代码时,发现找不倒hash_compare,该怎么办???

追问

抱歉,自己真的菜鸟一个,我理解mac下的默认是有stl的,但功能可能不全。我调试了是成功的,但hash_map不知该怎么调用,可能是平台的问题吧,我还没有解决。

#include 
#include 
#include 
#include 
//#include 
using namespace std;
using namespace __gnu_cxx;

struct string_less : public binary_function  //定义虚函数,以使用hashmap函数
{
public:
    result_type operator()(const first_argument_type& _Left, const second_argument_type& _Right) const
    {
        return(_Left.compare(_Right) < 0 ? true : false);
    }
};
int main()
{
    hash_map > zhao;
    hash_map>::iterator zhi;
    hash_map>::iterator qiang;
    hash_map>::iterator miss;
    hash_map>::iterator you;
    //运行监视zhao为{ size=9 }
    zhao["a"] = 1;//键a对应值1
    zhao["b"] = 2;
    zhao["c"] = 3;
    zhao["d"] = 4;
    zhao["e"] = 5;
    zhao["f"] = 6; 
    zhao["g"] = 7;
    zhao["h"] = 8;
    zhao["i"] = 9;

    zhi=zhao.begin();
    //监视显示zhi为("a",1)
    cout<first<<" "<< zhi->second<first<<" ";  cout<< (zhi)->second<first << "\t" << qiang->second << endl; qiang++;
    }
    qiang = zhao.end(); 
    //监视内容: <读取字符串的字符时出错。>, -842150451)end应该是最后一个键值对后的空间,将其- -就应该是最后一个键值对 
    cout<first<first <first << endl;
    //输出i。返回一个常量迭代器,此迭代器用于发现反向hash_map中的首个元素。
    cout << (--zhao.crend())->first << endl;
    //输出a。返回一个常量迭代器,此迭代器用于发现反向hash_map中最后一个元素后的位置。 记得- -哦。
    //zhao.clear();
    //亲测可用,清除整个hash。    
    cout<first<first<first<first << endl;
    //输出i  hash_map中键比指定键大的第一个元素,使用j VS崩溃了。
    zhao.erase("k");
    //删除掉该键,若不存在则无法删除
    cout<second<

运行报错

bin/../lib/c++/v1/ext/hash_map:209:2: warning: 
      Use of the header  is deprecated. Migrate to 
      [-W#warnings]
#warning Use of the header  is deprecated.  Migrate to  > zhao;
                               ^
./hash_map_test.cpp:19:38: error: unexpected type name 'string': expected
      expression
        hash_map > zhao;
                                            ^
./hash_map_test.cpp:20:24: error: use of undeclared identifier 'hash_compare'
        hash_map>::iterator zhi;
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
黄舟

到底报什么错误你得说出来啊?我的读心术还没有修炼成功,得等到飞升之后才有能力回答这个问题。

Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
说的很明白,不要用hash_map,用unordered_map;也不要用hash_compare,用std::hash

把你现在正学的这本书扔了,换本书重炼。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号