首页 > Mac下C++ STL的配置使用?

Mac下C++ STL的配置使用?

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

问题

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

追问

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

#include <stdlib.h>
#include <string>
#include <iostream>
#include <ext/hash_map>
//#include <ext/unorder_hash_map>
using namespace std;
using namespace __gnu_cxx;

struct string_less : public binary_function<const string, const string, bool>  //定义虚函数,以使用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<string, int , hash_compare<string, string_less> > zhao;
    hash_map<string, int, hash_compare<string, string_less>>::iterator zhi;
    hash_map<string, int, hash_compare<string, string_less>>::iterator qiang;
    hash_map<string, int, hash_compare<string, string_less>>::iterator miss;
    hash_map<string, int, hash_compare<string, string_less>>::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<<zhi->first<<" "<< zhi->second<<endl;
    //输出为:a 1 即first代表键 second代表值
    qiang = zhi;
    //可以直接赋值键值对哦
    cout << (++zhi)->first<<" ";  cout<< (zhi)->second<<endl;
    //输出f 6。即哈希桶内下一组键值.分开输入是因为cout输出流问题
    while (qiang != zhao.end())
        //遍历该哈希桶,可以看到字母的顺序afgbcdehi 即是一定顺序存放的,不要想当然以为按照字母顺序
    {
        cout << qiang->first << "\t" << qiang->second << endl; qiang++;
    }
    qiang = zhao.end(); 
    //监视内容: <读取字符串的字符时出错。>, -842150451)end应该是最后一个键值对后的空间,将其- -就应该是最后一个键值对 
    cout<<zhao.at("d")<<endl;
    //输出4 此函数用于找到键对应的值
    cout<<zhao.cbegin()->first<<endl;
    //输出a,此函数用于返回hash_map第一个元素的迭代器
    cout << (--zhao.cend())->first <<endl;
    //输出i。返回一个常量迭代器,此迭代器用于发现hash_map中最后一个元素后的位置。cend与end的区别在于end返回一个迭代器,而cend是一个常量迭代器   。
    cout << zhao.crbegin()->first << endl;
    //输出i。返回一个常量迭代器,此迭代器用于发现反向hash_map中的首个元素。
    cout << (--zhao.crend())->first << endl;
    //输出a。返回一个常量迭代器,此迭代器用于发现反向hash_map中最后一个元素后的位置。 记得- -哦。
    //zhao.clear();
    //亲测可用,清除整个hash。    
    cout<<zhao.count("a")<<endl; 
    //此处输出1     返回 1,若 hash_map 包含排序关键字参数匹配键的元素;返回0,若 hash_map 不包含具有匹配键的元素。
    cout<<zhao.emplace("j", 10).first->first<<endl;
    //插入一个元素,通过遍历亲测发现若已经存在该键则无操作,若不存在则进行插入。
    cout<<zhao.emplace_hint(zhao.begin(), "k", 11)->first<<endl;
    //输出K 。 插入构造一个元素到 hash_map,附带位置提示.完全不知道第一个变量是干什么的,通过遍历发现并不是插入到该位置,但是返回值可以直接定位到该键
    cout << zhao.empty()<<endl;
    //输出0 .  若为空则输出1,非空则输出0.
    cout<< zhao.equal_range("h").first->first<<endl;
    //输出h   hash_map中键比指定键大或相等的第一个元素。
    cout << zhao.equal_range("h").second->first << endl;
    //输出i  hash_map中键比指定键大的第一个元素,使用j VS崩溃了。
    zhao.erase("k");
    //删除掉该键,若不存在则无法删除
    cout<<zhao.find("a")->second<<endl;
    //输出1  显而易见。找到该键
    zhao.insert(++zhao.begin(),--zhao.end());
    //这个insert,将一个或一个范围的元素插入hash_map。 我尝试了好多好多参数,终于大概理解了,就是把另一个的一段hash插入到这边来。或者这样zhao.insert(zhi,qiang);
    //zhao.key_comp(); 先不做介绍了,上面那个insert就研究了二十分钟多才懂 同理 zhao.lower_bound()也先放下 upper_bound()也放下
    cout << zhao.size()<< endl;
    //10输出元素个数
    zhao.swap(zhao);
    //交换两个对象元素的hashmap,或者swap(zhao,zhao);由于我只定义了一个对象..亲多定义一个就可以了。
    system("pause");
}

运行报错

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

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

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

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

【热门文章】
【热门文章】