萌新求助

P1001 A+B Problem

Zhangikun @ 2023-04-16 10:59:42

当然我这题已经AC了,但是我有个问题:

#include<bits/stdc++.h>
using namespace std;
template<class T1,class T2>struct ikundered_map
{
  vector<pair<T1,T2> >hash[114514];//T1为下标的类型,T2为存储的类型
  int put_id(T1 x)
  {
    int id;
    if(typeid(T1)==typeid(1)||typeid(T1)==typeid(10000000000LL)||typeid(T1)==typeid('A')||typeid(T1)==typeid(false)||typeid(T1)==typeid(float(0.1))||typeid(T1)==typeid(0.1))
    id=((int)x%114514+114514)%114514;
    else
    id=put_id(x[0]);
    return id;
  }
  T2& operator[](T1 x)
  {
    int id;
    id=put_id(x);
    for(int i=0;i<hash[id].size();i++)
    {
      if(hash[id][i].first==x)
      {
        return hash[id][i].second;
      }
    }
    T2 num;
    hash[id].push_back({x,num});
    return hash[id][hash[id].size()-1].second;
  }
};
int main()
{
  string s;
  vector<string>v;
  int n;
  cin>>n;
  ikundered_map<string,bool>kun;
  for(int i=1;i<=n;i++)
  {
    cin>>s;
    if(kun[s]==0)
    {
      v.push_back(s);
      kun[s]=1;
    }
  }
  for(int i=0;i<v.size();i++)
  {
    cout<<v[i]<<" ";
  }
  return 0;
}

这个手写哈希为什么不能通过编译


by Miracle_InDream @ 2023-04-16 11:02:20

嗯……啊……额……

你都AC了你还问?


by Zhangikun @ 2023-04-16 11:05:59

@liyirong567 嗯……啊……额
你不妨看看我问的是什么


by hycqvvq @ 2023-04-16 11:06:55

要查编译问题怎么不给个编译输出。


by Zhangikun @ 2023-04-16 11:10:38

@hycJuRuo 我用的VScode,所以……:

test.cpp: In instantiation of 'int ikundered_map<T1, T2>::put_id(T1) [with T1 = std::__cxx11::basic_string<char>; T2 = bool]':
test.cpp:18:8:   required from 'T2& ikundered_map<T1, T2>::operator[](T1) [with T1 = std::__cxx11::basic_string<char>; T2 = bool]'
test.cpp:41:13:   required from here
test.cpp:10:9: error: invalid cast from type 'std::__cxx11::basic_string<char>' to type 'int'
     id=((int)x%114514+114514)%114514;
         ^~~~~~
test.cpp:12:7: error: no matching function for call to 'ikundered_map<std::__cxx11::basic_string<char>, bool>::put_id(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)'
     id=put_id(x[0]);
test.cpp:6:7: note: candidate: 'int ikundered_map<T1, T2>::put_id(T1) [with T1 = std::__cxx11::basic_string<char>; T2 = bool]'
   int put_id(T1 x)
       ^~~~~~
test.cpp:6:7: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' {aka 'char'} to 'std::__cxx11::basic_string<char>'

没有波型曲线,但是编译的时候终端会弹出这个


by hycqvvq @ 2023-04-16 11:12:28

@Zhangikun 你怎么 (int)xx[0]


by hycqvvq @ 2023-04-16 11:13:49

你一 string 怎么强转 int


by Zhangikun @ 2023-04-16 11:14:35

@hycJuRuo 我考虑的是如果是int、long long、double、float、bool、char类型直接转成int, 其他的有[]重载的就取第一个,也就是x[0]


by hycqvvq @ 2023-04-16 11:16:37

不懂哈希,但是为什么不直接指定 type 呢,日报看多了吧


by Zhangikun @ 2023-04-16 11:17:45

@hycJuRuo 因为太麻烦了 我不会


by hycqvvq @ 2023-04-16 11:18:14

@Zhangikun 在 ikundered_map<string,bool> 中,typeid(T1) 一定为 string,不受其值影响。


| 下一页