哈希表 知识速记(9/11 and 9/13)

哈希表

拉链法

#include <cstring>
#include<string>
#include <iostream>

using namespace std;

const int N = 100003;

int h[N], e[N], ne[N], idx;

void insert(int x)
{
    int k = (x % N + N) % N;
    e[idx] = x;//按照单链表的方式进行连接
    ne[idx] = h[k];
    h[k] = idx ++ ;
}

bool find(int x)
{
    int k = (x % N + N) % N;
    for (int i = h[k]; i != -1; i = ne[i])//让开头等于哈希值所在数组的值,然后依次查找,比对判断
        if (e[i] == x)
            return true;

    return false;
}

int main()
{
    int n;
    scanf("%d", &n);

    memset(h, -1, sizeof h);//初始化数组,记得包含头文件cstring

    while (n -- )
    {
        string op;
        int x;
        cin>>op ;scanf("%d", &x);

        if (op == "I") insert(x);
        else
        {
            if (find(x)) puts("Yes");
            else puts("No");
        }
    }

    return 0;
}

 

 

 

 

 

1、结构体用sort排序

定义结构体

struct student{

int x;

int y;

}stu[100];

用函数定义谓语

bool cmp(student a,student b){

return a.x>b.x;

}

sort(stu,stu+100,cmp);

 2.c++和数学取余运算的差别

-10%3   c++中为 -1

数学中为 2;

3.memset函数

int a[4];

memset(a,1,sizeof(a));给a初始化为1,;

需要引入头文件 cstring

 

posted @ 2023-09-13 21:37  敲代码的6  阅读(34)  评论(0)    收藏  举报