当前位置: 首页 > news >正文

C++ unordered_map unordered_set 模拟实现

1. 关于unordered_map 和 unordered_set

区别于C++的另外两个容器mapsetmapset的底层是红黑树;而unordered_mapunordered_set的底层是哈希

因为unordered_mapunordered_set的底层是哈希,因此他们存储的数据是没有顺序unordered

下面,我们就通过之前写的基于开散列(哈希桶)的代码,来封装出一份简单的unordered_mapunordered_set

开散列哈希表的代码如下:

namespace open_hash
{template<class K, class V, class Hash = HashFunc<K>>class HashTable{public:struct Node{std::pair<K, V> kv;Node* next;Node(const std::pair<K, V>& KV = std::pair<K, V>()) : kv(KV),next(nullptr){}};public:HashTable(): _load_factor(1.0),_n(0) {_hash.resize(10, nullptr);}~HashTable(){for (auto& node : _hash){while (node){Node* next = node->next;delete node;node = next;}}}size_t size() const{return _n;}Node* find(const K& key){size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];while (node){if (node->kv.first == key)return node;node = node->next;}return nullptr;}bool insert(std::pair<K, V> kv){if (find(kv.first))return false;if (1.0 * _n / _hash.capacity() >= _load_factor){size_t capacity = _hash.capacity() == 0 ? 10 : 2 * _hash.capacity();HashTable<K, V, Hash> new_hash;new_hash._hash.resize(capacity, nullptr);//将原来哈希桶中的节点转移到新的桶中for (auto& node : _hash){while (node){//从新计算映射关系size_t hash_index = _hash_func(node->kv.first) % new_hash._hash.capacity();Node* next = node->next;node->next = new_hash._hash[hash_index];new_hash._hash[hash_index] = node;node = next;}}_hash.swap(new_hash._hash);}Node* newnode = new Node(kv);size_t hash_index = _hash_func(kv.first) % _hash.capacity();//头插newnode->next = _hash[hash_index];_hash[hash_index] = newnode;++_n;return true;}bool erase(const K& key){size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];Node* prev = nullptr;while (node){if (node->kv.first == key){if (prev == nullptr)_hash[hash_index] = _hash[hash_index]->next;elseprev->next = node->next;delete node;--_n;return true;}prev = node;node = node->next;}return false;}private:std::vector<Node*> _hash;double _load_factor;Hash _hash_func;size_t _n;};
}

2. 模拟实现

2.1 修改哈希表的类模板参数

如果想要用同一份哈希表代码来模拟实现出unordered_mapunordered_set两份容器,就需要考虑一个问题:

  • unordered_setKey结构的;而unordered_mapKey_Value结构的

  • 在上面的哈希表代码中,我们直接使用的就是KV模型,因此就需要想办法来屏蔽这种差异

可以通过这样的方式来解决:

将哈希表的模板参数改成这样:

template<class K, class T, class KeyOfT, class Hash>

KKey值的类型

T:表示要存放什么类型的数据。

  • 对于unordered_set,填写的就是K
  • 对于unordered_set,填写的就是std::pair<K, V>
    在这里插入图片描述

Hash:计算哈希值的哈希函数

KeyOfT:获得键值Key的函数

  • 由于哈希表自己不知道存储的数据到底是Key模型的还是Key_Value模型的

  • 因此需要通过一个函数KeyOfT来获得数据的键值

  • 而这个KeyOfT由封装哈希表的unordered_mapunordered_set来提供

    类似这样:

    对于unordered_mapKey值是pair.first

    template <class K, class V, class Hash = HashFunc<const K>>
    class unordered_map
    {struct MapKeyOfT{const K operator()(const std::pair<const K, V>& p) const { return p.first;}};
    public:using HT = typename HashTable<K, std::pair<const K, V>, MapKeyOfT, Hash>;private:HT _ht;
    };
    

    对于unordered_setKey值就是自己本身

    template <class K, class Hash = HashFunc<const K>>
    class unordered_set
    {struct SetKeyOfT{const K operator()(const K& key) const{return key;}};
    public:using HT = typename HashTable<K, K, SetKeyOfT, Hash>;private:HT _ht;
    };
    

2.2 哈希表的迭代器实现

注:STL中的unordered_map(set)只支持正向迭代器,不支持反向迭代器

规定:哈希表迭代器的begin(),为第一个不为空的哈希桶中的第一个节点;哈希表迭代器的end(),为nullptr

对于哈希表的迭代器,我们只需要关注operator++()即可

对于开散列哈希桶的哈希表,当其迭代器向后移动一位之后,可能出现两种情况:

  • 移动之后,不为空,说明这个哈希桶(单链表)还没有走完,那么直接返回这个节点即可

  • 移动之后,为空,说明这个哈希桶(单链表)已经走完了

    • 那么就找下一个不为空的哈希桶,并返回这个哈希桶的第一个节点
    • 如果找不到下一个不为空的哈希桶了,就返回end()

在这里插入图片描述

通过上面的分析我们发现,对于哈希表的迭代器类,其需要两个成员来完成迭代工作:

  • Node:用于迭代当前所在的哈希桶
  • HashTable:用于迭代所有的哈希桶

根据上面的分析,我么可以实现出哈希表迭代器:

//非const迭代器
template<class K, class T, class KeyOfT, class Hash = HashFunc<const K>>
struct __HashIterator
{using Node = typename HashTable<K, T, KeyOfT, Hash>::Node;using HT = typename HashTable<K, T, KeyOfT, Hash>;using Self = typename  __HashIterator< K, T, KeyOfT, Hash>;Node* node;HT* ht;__HashIterator(Node* n, HT* h) : node(n), ht(h) {}//前置++Self& operator++(){//如果已经是end(),直接返回if (nullptr == node)return *this;Node* next = node->next;if (next != nullptr)	//如果哈希桶还没走完,就迭代到哈希桶的下一个{node = next;return *this;}//否则,找到下一个非空哈希桶size_t hash_index = Hash()(KeyOfT()(node->data)) % ht->_hash.capacity();while (++hash_index < ht->_hash.capacity()){//找到了,就迭代到这个非空哈希桶的第一个节点if (ht->_hash[hash_index]){node = ht->_hash[hash_index];return *this;}}//走到这里,说明已经把哈希数组走完了(不需要往回走)//也就是把所有的哈希桶走完了//即走到end() nullptrnode = nullptr;return *this;}//后置++Self operator++(int){Self temp = *this;this->operator++();return temp;}bool operator==(const Self& it) const{return node == it.node;}bool operator!=(const Self& it) const{return node != it.node;}T& operator*() const{return node->data;}T* operator->() const{return &node->data;}
};//const迭代器
template<class K, class T, class KeyOfT, class Hash = HashFunc<K>>
struct __HashConstIterator
{using Node = typename HashTable<K, T, KeyOfT, Hash>::Node;using HT = typename HashTable<K, T, KeyOfT, Hash>;using Self = typename  __HashConstIterator< K, T, KeyOfT, Hash>;const Node* node;const HT* ht;__HashConstIterator(const Node* n, const HT* h) : node(n), ht(h) {}//前置++Self& operator++(){if (nullptr == node)return *this;Node* next = node->next;if (next != nullptr){node = next;return *this;}size_t hash_index = Hash()(KeyOfT()(node->data)) % ht->_hash.capacity();while (++hash_index < ht->_hash.capacity()){if (ht->_hash[hash_index]){node = ht->_hash[hash_index];return *this;}}//走到这里,说明已经把哈希数组走完了(不需要往回走)//也就是把所有的哈希桶走完了//即走到尾 nullptrnode = nullptr;return *this;}//后置++Self operator++(int){Self temp = *this;this->operator++();return temp;}bool operator==(const Self& it) const{return node == it.node;}bool operator!=(const Self& it) const{return node != it.node;}const T& operator*() const{return node->data;}const T* operator->() const{return &node->data;}
};

2.3 修改哈希表部分函数的返回值

后续我们模拟实现unordered_setunordered_map的接口都是直接调用其内部封装的哈希表的接口

因此,为了符合规范,我们需要对原先哈希表部分函数的返回值进行修改

find()

unordered_map的成员函数find()为例:

iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;
  • 对于非const对象,返回非const迭代器
  • 对于const对象,返回const迭代器
iterator find(const K& key)
{size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];while (node){if (_key_of_t(node->data) == key)return iterator(node, this);node = node->next;}return iterator(nullptr, this);
}const_iterator find(const K& key) const 
{size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];while (node){if (_key_of_t(node->data) == key)return const_iterator(node, this);node = node->next;}return const_iterator(nullptr, this);
}

insert()

unordered_map的成员函数insert()为例:

pair<iterator,bool> insert ( const value_type& val );

可见,返回值是一个pair<iterator,bool>,其含义如下:

  • 如果哈希表中已经有相同关键字的元素,那么就表示插入失败,并返回指向这个相同关键字元素的迭代器
  • 如果哈希表中没有相同关键字的元素,那么就插入成功,并返回这个指向新插入的元素的迭代器
std::pair<iterator, bool> insert(const T& data)
{auto it = find(_key_of_t(data));if (it != end())return std::make_pair(it, false);if (1.0 * _n / _hash.capacity() >= _load_factor){size_t capacity = _hash.capacity() == 0 ? 10 : 2 * _hash.capacity();HashTable<K, T, KeyOfT, Hash> new_hash;new_hash._hash.resize(capacity, nullptr);//将原来哈希桶中的节点转移到新的桶中for (auto& node : _hash){while (node){//从新计算映射关系size_t hash_index = _hash_func(_key_of_t(node->data)) % new_hash._hash.capacity();Node* next = node->next;node->next = new_hash._hash[hash_index];new_hash._hash[hash_index] = node;node = next;}}_hash.swap(new_hash._hash);}Node* newnode = new Node(data);size_t hash_index = _hash_func(_key_of_t(data)) % _hash.capacity();//头插newnode->next = _hash[hash_index];_hash[hash_index] = newnode;++_n;return std::make_pair(iterator(newnode, this), true);
}

erase()

unordered_map的成员函数erase()为例:

by position (1)	iterator erase ( const_iterator position );by key (2)	size_type erase ( const key_type& k );
  1. 通过迭代器删除

    • 如果删除成功,就返回指向被删除元素的下一个元素的迭代器
    • 如果删除失败,就返回end()
  2. 通过关键字Key删除

    • 如果删除成功,返回true
    • 如果删除失败,返回false
iterator erase(const_iterator pos)
{//不能删除end()if (pos == cend())return cend();size_t hash_index = _key_of_t(*pos) % _hash.capacity();Node* node = _hash[hash_index];Node* prev = nullptr;Node* next = nullptr;while (node){if (_key_of_t(node->data) == _key_of_t(*pos)){if (prev == nullptr)_hash[hash_index] = _hash[hash_index]->next;elseprev->next = node->next;delete node;--_n;//找到下一个节点next = (prev == nullptr) ? _hash[hash_index] : prev->next;if (next == nullptr){while (++hash_index < _hash.capacity()){if (_hash[hash_index]){next = _hash[hash_index];break;}}}return iterator(next, this);}prev = node;node = node->next;}return end();
}bool erase(const K& key)
{size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];Node* prev = nullptr;while (node){if (_key_of_t(node->data) == key){if (prev == nullptr)_hash[hash_index] = _hash[hash_index]->next;elseprev->next = node->next;delete node;--_n;return true;}prev = node;node = node->next;}return false;
}

开散列哈希表实现代码

namespace open_hash
{template<class K, class T, class KeyOfT, class Hash = HashFunc<const K>>class HashTable;template<class K, class T, class KeyOfT, class Hash = HashFunc<const K>>struct __HashIterator{using Node = typename HashTable<K, T, KeyOfT, Hash>::Node;using HT = typename HashTable<K, T, KeyOfT, Hash>;using Self = typename  __HashIterator< K, T, KeyOfT, Hash>;Node* node;HT* ht;__HashIterator(Node* n, HT* h) : node(n), ht(h) {}//前置++Self& operator++(){if (nullptr == node)return *this;Node* next = node->next;if (next != nullptr){node = next;return *this;}size_t hash_index = Hash()(KeyOfT()(node->data)) % ht->_hash.capacity();while (++hash_index < ht->_hash.capacity()){if (ht->_hash[hash_index]){node = ht->_hash[hash_index];return *this;}}//走到这里,说明已经把哈希数组走完了(不需要往回走)//也就是把所有的哈希桶走完了//即走到尾 nullptrnode = nullptr;return *this;}//后置++Self operator++(int){Self temp = *this;this->operator++();return temp;}bool operator==(const Self& it) const{return node == it.node;}bool operator!=(const Self& it) const{return node != it.node;}T& operator*() const{return node->data;}T* operator->() const{return &node->data;}};template<class K, class T, class KeyOfT, class Hash = HashFunc<K>>struct __HashConstIterator{using Node = typename HashTable<K, T, KeyOfT, Hash>::Node;using HT = typename HashTable<K, T, KeyOfT, Hash>;using Self = typename  __HashConstIterator< K, T, KeyOfT, Hash>;const Node* node;const HT* ht;__HashConstIterator(const Node* n, const HT* h) : node(n), ht(h) {}//支持非const迭代器到const迭代器的类型转换__HashConstIterator(const __HashIterator<K, T, KeyOfT>& it) : node(it.node), ht(it.ht) {}//前置++Self& operator++(){if (nullptr == node)return *this;Node* next = node->next;if (next != nullptr){node = next;return *this;}size_t hash_index = Hash()(KeyOfT()(node->data)) % ht->_hash.capacity();while (++hash_index < ht->_hash.capacity()){if (ht->_hash[hash_index]){node = ht->_hash[hash_index];return *this;}}//走到这里,说明已经把哈希数组走完了(不需要往回走)//也就是把所有的哈希桶走完了//即走到尾 nullptrnode = nullptr;return *this;}//后置++Self operator++(int){Self temp = *this;this->operator++();return temp;}bool operator==(const Self& it) const{return node == it.node;}bool operator!=(const Self& it) const{return node != it.node;}const T& operator*() const{return node->data;}const T* operator->() const{return &node->data;}};template<class K, class T, class KeyOfT, class Hash>class HashTable{friend struct __HashIterator<K, T, KeyOfT, Hash>;;friend struct __HashConstIterator<K, T, KeyOfT, Hash>;public:struct Node{T data;Node* next;Node(const T& Data = T()) : data(Data),next(nullptr){}};public:using iterator = __HashIterator<K, T, KeyOfT, Hash>;using const_iterator = __HashConstIterator<K, T, KeyOfT, Hash>;using HT = HashTable<K, T, KeyOfT, Hash>;public:HashTable(): _load_factor(1.0),_n(0) {_hash.resize(10, nullptr);}//拷贝构造HashTable(const HT& ht):_load_factor(ht._load_factor),_n(ht._n),_hash_func(ht._hash_func),_key_of_t(ht._key_of_t){_hash.resize(ht._hash.capacity(), nullptr);for (auto& node : ht._hash){Node* cur = node;while (cur){size_t hash_index = _hash_func(_key_of_t(cur->data)) % _hash.capacity();Node* newnode = new Node(cur->data);newnode->next = _hash[hash_index];_hash[hash_index] = newnode;cur = cur->next;}}}//移动构造HashTable(HT&& ht){swap(ht);}HT& operator=(const HT& ht){HT temp(ht);swap(temp);return *this;}//移动赋值HT& operator=(HT&& ht){swap(ht);return *this;}~HashTable(){for (auto& node : _hash){while (node){Node* next = node->next;delete node;node = next;}}}void swap(HT& ht){std::swap(_hash, ht._hash);std::swap(_n, ht._n);std::swap(_hash_func, ht._hash_func);std::swap(_key_of_t, ht._key_of_t);std::swap(_load_factor, ht._load_factor);}iterator begin(){//找到第一个非空的哈希桶for (auto& node : _hash){if (node)return iterator(node, this);}return iterator(nullptr, this);}iterator end(){return iterator(nullptr, this);}const_iterator begin() const{//找到第一个非空的哈希桶for (auto& node : _hash){if (node)return const_iterator(node, this);}return const_iterator(nullptr, this);}const_iterator cbegin() const{//找到第一个非空的哈希桶for (auto& node : _hash){if (node)return const_iterator(node, this);}return const_iterator(nullptr, this);}const_iterator end() const{return const_iterator(nullptr, this);}const_iterator cend() const{return const_iterator(nullptr, this);}size_t size() const{return _n;}iterator find(const K& key){size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];while (node){if (_key_of_t(node->data) == key)return iterator(node, this);node = node->next;}return iterator(nullptr, this);}const_iterator find(const K& key) const {size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];while (node){if (_key_of_t(node->data) == key)return const_iterator(node, this);node = node->next;}return const_iterator(nullptr, this);}std::pair<iterator, bool> insert(const T& data){auto it = find(_key_of_t(data));if (it != end())return std::make_pair(it, false);if (1.0 * _n / _hash.capacity() >= _load_factor){size_t capacity = _hash.capacity() == 0 ? 10 : 2 * _hash.capacity();HashTable<K, T, KeyOfT, Hash> new_hash;new_hash._hash.resize(capacity, nullptr);//将原来哈希桶中的节点转移到新的桶中for (auto& node : _hash){while (node){//从新计算映射关系size_t hash_index = _hash_func(_key_of_t(node->data)) % new_hash._hash.capacity();Node* next = node->next;node->next = new_hash._hash[hash_index];new_hash._hash[hash_index] = node;node = next;}}_hash.swap(new_hash._hash);}Node* newnode = new Node(data);size_t hash_index = _hash_func(_key_of_t(data)) % _hash.capacity();//头插newnode->next = _hash[hash_index];_hash[hash_index] = newnode;++_n;return std::make_pair(iterator(newnode, this), true);}iterator erase(const_iterator pos){//不能删除end()if (pos == cend())return cend();size_t hash_index = _key_of_t(*pos) % _hash.capacity();Node* node = _hash[hash_index];Node* prev = nullptr;Node* next = nullptr;while (node){if (_key_of_t(node->data) == _key_of_t(*pos)){if (prev == nullptr)_hash[hash_index] = _hash[hash_index]->next;elseprev->next = node->next;delete node;--_n;//找到下一个节点next = (prev == nullptr) ? _hash[hash_index] : prev->next;if (next == nullptr){while (++hash_index < _hash.capacity()){if (_hash[hash_index]){next = _hash[hash_index];break;}}}return iterator(next, this);}prev = node;node = node->next;}return end();}bool erase(const K& key){size_t hash_index = _hash_func(key) % _hash.capacity();Node* node = _hash[hash_index];Node* prev = nullptr;while (node){if (_key_of_t(node->data) == key){if (prev == nullptr)_hash[hash_index] = _hash[hash_index]->next;elseprev->next = node->next;delete node;--_n;return true;}prev = node;node = node->next;}return false;}private:std::vector<Node*> _hash;double _load_factor;Hash _hash_func;KeyOfT _key_of_t;size_t _n;};
}

2.4 unordered_set 模拟实现

namespace lwj
{using namespace open_hash;template <class K, class Hash = HashFunc<const K>>class unordered_set{struct SetKeyOfT{const K operator()(const K& key) const{return key;}};public:using HT = typename HashTable<K, const K, SetKeyOfT, Hash>;using iterator = typename HT::iterator;using const_iterator = typename HT::const_iterator;using Self = typename unordered_set<K, Hash>;//支持默认构造unordered_set() = default;//拷贝构造unordered_set(const Self& set){_ht = set._ht;}//移动构造unordered_set(Self&& set){_ht.swap(set._ht);}//拷贝赋值Self& operator=(const Self& set){_ht = set._ht;return *this;}//移动复赋值Self& operator=(Self&& set){_ht.swap(set._ht);return *this;}bool count(const K& key) const{return find(key) != end();}iterator find(const K& key){return _ht.find(key);}const_iterator find(const K& key) const{return _ht.find(key);}std::pair<iterator, bool> insert(const K& val){return _ht.insert(val);}bool erase(const K& key){return _ht.erase(key);}iterator erase(const_iterator pos){return _ht.erase(pos);}iterator begin(){return _ht.begin();}const_iterator begin() const{return _ht.cbegin();}const_iterator cbegin() const{return _ht.cbegin();}iterator end(){return _ht.end();}const_iterator end() const{return _ht.cend();}const_iterator cend() const{return _ht.cend();}private:HT _ht;};
}

2.5 unordered_map 模拟实现

operator[] ()

mapped_type& operator[] ( const key_type& k );
mapped_type& operator[] ( key_type&& k );

这个函数的作用是:查找Key值所对应的Value,这分为两种情况:

  • Key值不存在,那么该函数就会直接向hash中以Key为键值插入一个新的键值对,然后返回这个键值对的Value
  • Key值存在,那么就返回该Key值对应的Value

我们可以直接利用insert的返回值来实现:

V& operator[](const K& key)
{//无论inset成功插入与否,其都会返回一个指向key值的迭代器std::pair<iterator, bool> p = insert({ key, V() });return p.first->second;
}

整体实现

namespace lwj
{using namespace open_hash;template <class K, class V, class Hash = HashFunc<const K>>class unordered_map{struct MapKeyOfT{const K operator()(const std::pair<const K, V>& p) const { return p.first;}};public:using HT = typename HashTable<K, std::pair<const K, V>, MapKeyOfT, Hash>;using iterator = typename HT::iterator;using const_iterator = typename HT::const_iterator;using Self = typename unordered_map<K, V, Hash>;//支持默认构造unordered_map() = default;//拷贝构造unordered_map(const Self& map){_ht = map._ht;}//移动构造unordered_map(Self&& map){_ht.swap(map._ht);}//拷贝赋值Self& operator=(const Self& map){_ht = map._ht;return *this;}//移动赋值Self& operator=(Self&& map){_ht.swap(map._ht);return *this;}V& operator[](const K& key){std::pair<iterator, bool> p = insert({ key, V() });return p.first->second;}bool count(const K& key) const{return find(key) != end();}iterator find(const K& key){return _ht.find(key);}const_iterator find(const K& key) const{return _ht.find(key);}std::pair<iterator, bool> insert(const std::pair<const K, V>& val){return _ht.insert(val);}bool erase(const K& key){return _ht.erase(key);}iterator erase(const_iterator pos){return _ht.erase(pos);}iterator begin(){return _ht.begin();}const_iterator begin() const{return _ht.cbegin();}iterator end(){return _ht.end();}const_iterator end() const{return _ht.cend();}private:HT _ht;};
}

相关文章:

C++ unordered_map unordered_set 模拟实现

1. 关于unordered_map 和 unordered_set 区别于C的另外两个容器map和set&#xff0c;map和set的底层是红黑树&#xff1b;而unordered_map和unordered_set的底层是哈希 因为unordered_map和unordered_set的底层是哈希&#xff0c;因此他们存储的数据是没有顺序​​unordered​…...

Java使用自定义类加载器实现插件动态加载

虚拟机类加载子系统 Java虚拟机的⼀个重要子系统&#xff0c;主要负责将类的字节码加载到JVM内存的⽅法区&#xff0c;并将其转换为JVM内部的数据结构。 一个类从被加载到虚拟机开始&#xff0c;一直到卸载出内存为止&#xff0c;会经历七个阶段&#xff1a;加载&#xff0c;…...

【初级篇】如何使用DeepSeek和Dify构建高效的企业级智能客服系统

在当今数字化时代,企业面临着日益增长的客户服务需求。使用Dify创建智能客服不仅能够提升客户体验,还能显著提高企业的运营效率。关于DIfy的安装部署,大家可以参考之前的文章: 【入门级篇】Dify安装+DeepSeek模型配置保姆级教程_mindie dify deepseek-CSDN博客 AI智能客服…...

Java开发之数据库应用:记一次医疗系统数据库迁移引发的异常:从MySQL到PostgreSQL的“dual“表陷阱与突围之路

记一次医疗系统数据库迁移引发的异常&#xff1a;从MySQL到PostgreSQL的"dual"表陷阱与突围之路 一、惊魂时刻&#xff1a;数据库切换引发的系统雪崩 某医疗影像系统在进行国产化改造过程中&#xff0c;将原MySQL数据库迁移至PostgreSQL。迁移完成后&#xff0c;系…...

Langchian构建代理

文章目录 概要ReAct 代理 ReAct 使用ReAct基本用法提示词模板内存使用迭代使用返回执行每一步情况限制输出行数设置运行超时时间 不使用代理下LLM如何结合工具案例案例2 概要 单靠语言模型无法采取行动 - 它们只输出文本。 LangChain 的一个重要用例是创建 代理。 代理是使用大…...

Vim软件使用技巧

目录 Demo Vim怎么看一个文件的行号&#xff0c;不用打开文件的前提下&#xff1f;进入文件后怎么跳转到某一行? 不打开文件查看行号&#xff08;查看文件的方法&#xff09; 方法1、使用命令行工具统计行数 方法2、通过vim的 - 参数查看文件信息 进入文件后跳转到指定行…...

SQL与NoSQL的区别

以下是SQL与NoSQL数据库的详细对比&#xff0c;涵盖核心特性、适用场景及技术选型建议&#xff1a; 一、核心区别对比 特性SQL&#xff08;关系型数据库&#xff09;NoSQL&#xff08;非关系型数据库&#xff09;数据模型基于表格&#xff0c;严格预定义模式&#xff08;Schem…...

1191:流感传染--BFS

这里写目录标题 题目 解析代码BFS代码 题目 解析 在同一天对一个病原体进行处理时&#xff0c;如果直接更改数组&#xff0c;将直接影响到后续的遍历 方法一&#xff1a;那么我们可以定义一个数组用来存储坐标&#xff1a;vectoir<pair<int,int>>&#xff0c;遍历…...

gfortran编译器调试功能选项

在使用 gfortran 编译器进行调试时&#xff0c;以下选项可以帮助你更好地定位和解决问题&#xff1a; 1. 生成调试信息 -g&#xff1a;生成调试信息&#xff0c;供调试器&#xff08;如 gdb&#xff09;使用。-ggdb&#xff1a;生成更详细的调试信息&#xff0c;优化 gdb 的使…...

小程序配置

注册小程序账号和安装开发工具 参考文档&#xff1a;注册小程序账号和安装开发工具https://blog.csdn.net/aystl_gss/article/details/127878658 HBuilder新建项目 填写项目名称&#xff0c;选择UNI-APP&#xff0c;修改路径&#xff0c;点击创建 manifest.json 配置 需要分别…...

【Linux】进程(1)进程概念和进程状态

&#x1f31f;&#x1f31f;作者主页&#xff1a;ephemerals__ &#x1f31f;&#x1f31f;所属专栏&#xff1a;Linux 目录 前言 一、什么是进程 二、task_struct的内容 三、Linux下进程基本操作 四、父进程和子进程 1. 用fork函数创建子进程 五、进程状态 1. 三种重…...

MySQL(事物下)

目录 一 多版本并发控制&#xff08; MVCC &#xff09;是一种用来解决 读-写冲突 的无锁并发控制 1. 前置知识 示例&#xff1a; 二 Read View 1. 当事物进行快照读(读历史数据)会MySQL会创建一个Read Vidw类对象&#xff0c;用来记录和当前一起并发的事物(活跃的事物)&a…...

Springboot+mybatis实现增删改查效果

我们前面实现了增加效果&#xff0c;现在来写一下修改~我们首先在controller里面写update 接着在service和mapper写方法 最后我们测试一下 没问题~需要注意的是mapper的sql别写错了&#xff01;...

【“以退为进“、“不得已而为之“与“风险对冲“的协同机制】

深度解析&#xff1a;“以退为进”、"不得已而为之"与"风险对冲"的协同机制 一、“以退为进”&#xff1a;空间重构的博弈艺术 1. 三维战略坐标系 权力维度&#xff1a;唐太宗"玄武门之变"后跪哭李渊&#xff0c;通过降维姿态化解道德危机&am…...

AUTOSAR 网络安全 架构

实现AUTOSAR网络安全架构的步骤指南 在当今汽车电子系统中&#xff0c;AUTOSAR&#xff08;AUTomotive Open System ARchitecture&#xff09;正在成为业界标准。结合网络安全要求&#xff0c;我们可以确保汽车在网络通信中保持安全。接下来&#xff0c;我们将讨论如何实现AUT…...

洛谷 P2801 教主的魔法 题解

之前学过 莫队 算法&#xff0c;其运用了分块思想&#xff1b;但是我居然是第一次写纯种的分块题目。 题意 给你一个长度为 n n n 的序列 a a a&#xff08;一开始 ∀ a i ∈ [ 1 , 1000 ] \forall a_i\in[1,1000] ∀ai​∈[1,1000]&#xff09;。要求执行 q q q 次操作&…...

Google最新生图模型Gemini-2.0-Flash-Exp免费用

Google发布新生图模型 Google释放出最新生图模型&#xff0c;在发布说明中提到&#xff1a; 2025年3月12日 在 Gemini-2.0-Flash-Exp 中发布原生图像输出功能 Gemini 2.0 Flash Experimental 模型发布&#xff0c;支持原生图像输出功能。开发者能够使用 Gemini 进行图像输出和…...

windows安装Elasticsearch

下载 下载最新版 https://www.elastic.co/downloads/elasticsearch 下载历史版本 安装 进入bin目录中 成功启动 访问 http://localhost:9200...

vulnhub靶场之stapler靶机

前言 靶机&#xff1a;stapler靶机&#xff0c;IP地址为192.168.10.12 攻击&#xff1a;kali&#xff0c;IP地址为192.168.10.6 靶机采用virtualbox&#xff0c;攻击机采用VMware虚拟机&#xff0c;都采用桥接网卡模式 文章涉及的靶机及工具&#xff0c;都可以自行访问官网或…...

2025年AI搜索引擎开源项目全景指南:从核心框架到生态工具

2025年AI搜索引擎开源项目全景指南&#xff1a;从核心框架到生态工具 在人工智能技术迅猛发展的当下&#xff0c;开源项目已成为构建AI搜索引擎的核心驱动力。本文整理9个具有代表性的开源项目&#xff0c;涵盖搜索框架、扩展生态及底层支持技术&#xff0c;助你快速搭建或优化…...

数字孪生像魔镜,映照出无限可能的未来

在当今科技飞速发展的时代&#xff0c;数字孪生作为一项极具潜力的前沿技术&#xff0c;正逐渐崭露头角&#xff0c;成为众多领域关注的焦点。它犹如一面神奇的魔镜&#xff0c;以数字化的方式精准映照出现实世界中的各种实体与系统&#xff0c;为我们开启了一扇通往无限可能未…...

PDF Reader

Acrobat Reader...

C++友元

1.什么是友元&#xff1f; 当我们需要在类的外部访问该类的私有成员和保护成员时&#xff0c;就可以利用友元来实现这一操作 在类中用 friend 关键字对函数或类进行声明 2.非成员函数友元 友元函数不是当前类的成员函数&#xff0c;而是当前类的外部函数&#xff0c;但是他可以…...

吴恩达机器学习笔记复盘(五)均方误差函数

只讲了线性回归的代价函数。 均方误差&#xff08;Mean Squared Error, MSE&#xff09; 均方误差&#xff08;MSE&#xff09;基于最小二乘法&#xff0c;通过计算预测值与真实值之间差值的平方的平均值来衡量模型的误差。 原理 假设我们有一组数据集&#xff0c;其中是第…...

使用 Docker 部署前端项目全攻略

文章目录 1. Docker 基础概念1.1 核心组件1.2 Docker 工作流程 2. 环境准备2.1 安装 Docker2.2 验证安装 3. 项目配置3.1 项目结构3.2 创建 Dockerfile 4. 构建与运行4.1 构建镜像4.2 运行容器4.3 访问应用 5. 使用 Docker Compose5.1 创建 docker-compose.yml5.2 启动服务5.3 …...

珠算与珠心算发展简介

珠算是中华传统优秀文化的科学遗产&#xff0c;它是我国劳动人民的伟大创造&#xff0c;被誉为中国的第五大发明&#xff0c;至今已有 1800 余年的历史。 珠算&#xff0c;是以算盘为工具&#xff0c;用手指拨动算珠进行数值计算的一门计算技术。同时&#xff0c;珠算又是一门科…...

基于SSM + JSP 的水果蔬菜商城

基于ssm的水果蔬菜商城系统前台和后台&#xff08;源码安装视频数据库环境&#xff09;计算机项目程序设计管理系统java小程序网站商城 一.相关技术 Java、Spring、Springboot、MVC、Mybatis、MySQL、SSM框架、Web、HTML、maven、JavaScript、css、vue 二.部署配置 1.IntelliJ …...

基于深度学习的蛀牙智能检测与语音提示系统【python源码+Pyqt5界面+数据集+训练代码】

《------往期经典推荐------》 一、AI应用软件开发实战专栏【链接】 项目名称项目名称1.【人脸识别与管理系统开发】2.【车牌识别与自动收费管理系统开发】3.【手势识别系统开发】4.【人脸面部活体检测系统开发】5.【图片风格快速迁移软件开发】6.【人脸表表情识别系统】7.【…...

Linux与深入HTTP序列化和反序列化

深入HTTP序列化和反序列化 本篇介绍 在上一节已经完成了客户端和服务端基本的HTTP通信&#xff0c;但是前面的传递并没有完全体现出HTTP的序列化和反序列化&#xff0c;为了更好得理解其工作流程&#xff0c;在本节会以更加具体的方式分析到HTTP序列化和反序列化 本节会在介绍…...

音视频入门基础:RTP专题(20)——通过FFprobe显示RTP流每个packet的信息

通过FFprobe命令&#xff1a; ffprobe -protocol_whitelist "file,rtp,udp" -of json -show_packets XXX.sdp 可以显示SDP描述的RTP流每个packet&#xff08;数据包&#xff09;的信息&#xff1a; 对于RTP流&#xff0c;上述的“packet”&#xff08;数据包&#…...

Java Web 大文件上传优化:从困境到高效

文章目录 Java Web 大文件上传优化&#xff1a;从困境到高效一、优化前的困境&#xff08;一&#xff09;内存占用问题&#xff08;二&#xff09;上传速度缓慢&#xff08;三&#xff09;稳定性欠佳 二、优化后的实现方案&#xff08;一&#xff09;客户端&#xff08;Vue&…...

C++——STL 常用的查找算法

算法简介&#xff1a; find //查找元素find_if //按条件查找元素adjacent_find //查找相邻重复元素binary_search //二分查找法count //统计元素个数count_if //按条件统计元素个数 1. find 功能描述&#xff1a; 查找指定元素&#xff0c;找到返回指定元素的迭…...

【一次成功】Win10本地化单机部署k8s v1.31.2版本及可视化看板

【一次成功】Win10本地化单机部署k8s v1.31.2版本及可视化看板 零、安装清单一、安装Docker Desktop软件1.1 安装前<启用或关闭Windows功能> 中的描红的三项1.2 查看软件版本1.3 配置Docker镜像 二、更新装Docker Desktop三、安装 k8s3.1 点击启动安装3.2 查看状态3.3 查…...

Vulkan视频解码decode显示display之同步

在ReleaseDisplayedPicture函数中消耗图片资源并且显示display完成&#xff0c;设置两个标志m_hasConsummerSignalFence true 和m_hasConsummerSignalSemaphore true virtual int32_t ReleaseDisplayedPicture(DecodedFrameRelease** pDecodedFramesRelease, uint32_t nu…...

专题|Python梯度提升实例合集:GBM、XGBoost、SMOTE重采样、贝叶斯、逻辑回归、随机森林分析信贷、破产数据...

全文链接&#xff1a;https://tecdat.cn/?p41051 分析师&#xff1a;Jiajie Shi&#xff0c;Yimeng Li 在当今数据驱动的时代&#xff0c;数据分析师和数据建模师面临着各式各样复杂且极具挑战性的任务。本专题合集便是围绕这些挑战展开的宝贵知识盛宴&#xff08;点击文末“阅…...

4.0 相机引导XY轴控制螺丝枪打螺丝

假如一个产品的同一水平上要打6个螺钉&#xff0c;是通过伺服XY轴移动带动相机以及螺丝枪&#xff0c;由相机拍照&#xff0c;根据拍照后螺丝孔位置来引导伺服进行移动以对准螺丝孔位置的。步骤如下&#xff1a; 一、9点标定&#xff0c;即把相机与伺服的实际位置关联起来。步骤…...

【ElasticSearch】学习笔记

一、lucene的组成 segment是一个具备完整搜索功能的最小单元。 多个segment组成了一个单机文本检索库lucene。 inverted index:倒排索引&#xff0c;用于快速根据关键词找到对应的文章term index: 构建出关键词的目录树&#xff0c;解决了term dictionary数据量过大&#xff…...

Spring Boot整合RabbitMQ极简教程

一、消息队列能解决什么问题&#xff1f; 异步处理&#xff1a;解耦耗时操作&#xff08;如发短信、日志记录&#xff09;流量削峰&#xff1a;应对突发请求&#xff0c;避免系统过载应用解耦&#xff1a;服务间通过消息通信&#xff0c;降低依赖 二、快速整合RabbitMQ 1. 环…...

代码随想录-04-字符串-03.替换数字

替换数字 题目 给定一个字符串 s&#xff0c;它包含小写字母和数字字符&#xff0c;请编写一个函数&#xff0c;将字符串中的字母字符保持不变&#xff0c;而将每个数字字符替换为number。 例如&#xff0c;对于输入字符串 “a1b2c3”&#xff0c;函数应该将其转换为 “anum…...

Tailwindcss开启黑夜模式

本篇讲述如何使用tailwindcss切换白天黑夜主题 tailwindcss自带的暗夜切换会比css自带的theme主体切换来得方便很多&#xff0c;学习成本也很低&#xff0c;只要求会用tailiwndcss 1&#xff0c;tailwindcss.config有两种暗夜模式切换&#xff0c;媒体查询和手动类切换。手动控…...

AI与人的智能,改变一生的思维模型【7】易得性偏差

目录 **易得性偏差思维模型&#xff1a;大脑的「热搜算法」与反操纵指南****病毒式定义&#xff1a;你的大脑正在被「热搜」劫持****四大核心攻击路径与史诗级案例****1. 信息过载时代的「认知短路」****2. 媒体放大器的「恐怖滤镜」****3. 个人经验的「数据暴政」****4. 社交茧…...

有序表--跳表

实现一种结构&#xff0c;支持如下操作&#xff0c;要求单次调用的时间复杂度O(log n) 1&#xff0c;增加x&#xff0c;重复加入算多个词频 2&#xff0c;删除x&#xff0c;如果有多个&#xff0c;只删掉一个 3&#xff0c;查询x的排名&#xff0c;x的排名为&#xff0c;比x小的…...

双指针---字符串替换数字(数字替换为“number“)

题目链接&#xff1a;替换数字 要求&#xff1a;时间复杂度为O(n) 思路&#xff1a; 1、先将字符串扩容到要输出串的长度。 2、从后向前替换数字字符&#xff0c;也就是双指针法&#xff0c;newIndex指向新长度的末尾&#xff0c;i指向旧长度的末尾。 #include<iostream&g…...

外星人入侵-Python-三

武装飞船 开发一个名为《外星人入侵》的游戏吧&#xff01;为此将使用 Pygame&#xff0c;这是一组功能强大而有趣的模块&#xff0c;可用于管理图形、动画乃至声音&#xff0c; 让你能够更轻松地开发复杂的游戏。通过使用Pygame来处理在屏幕上绘制图像 等任务&#xff0c;可将…...

JavaScript相关面试题

以下是150道JavaScript相关面试题及详细答案&#xff1a; JavaScript基础 1.JavaScript是什么&#xff1f; JavaScript是一种直译式脚本语言&#xff0c;主要用于网页开发&#xff0c;也可用于服务器端开发&#xff08;如Node.js&#xff09;。它是一种动态类型、弱类型、基于原…...

常见的数学模型

数学模型的基本原理 简单来说&#xff0c;数学模型就是用数学语言来描述现实世界中的现象或规律。它就像一个“翻译器”&#xff0c;把复杂的现实问题转化成我们可以用数学方法解决的问题。 核心思想&#xff1a; 简化现实&#xff1a;现实世界太复杂&#xff0c;模型会抓住最…...

计算机四级 - 数据库原理 - 第3章 「关系数据库系统概述」

3.1 关系数据库系统概述 关系数据模型的三大要素&#xff1a;关系数据结构、关系操作集合&#xff08;一次一个集合&#xff09;和关系完整性约束 1. 关系语言的特点是高度非过程化的, DBMS会自动帮用户选择存取路径&#xff0c;用户不需要依靠循环和递归完成数据的重复操作。…...

使用PHP进行自动化测试:工具与策略的全面分析

使用PHP进行自动化测试&#xff1a;工具与策略的全面分析 引言 随着软件开发的复杂性不断增加&#xff0c;自动化测试已成为确保软件质量的关键环节。PHP作为一种广泛使用的服务器端脚本语言&#xff0c;拥有丰富的生态系统和工具支持&#xff0c;使其成为自动化测试的理想选…...

discuz门户文章允许游客评论

discuz开启游客评论 1、进入后台&#xff0c;用户--用户组--系统用户组--游客--编辑 2、论坛相关 设置未允许发表回复 3、门户相关--文章评论字数(设置此用户组发表文章评论字数限制&#xff0c;设置为0将禁止此用户组发表评论) 4、验证游客回复 测试站 http://jinzhu.zhaowo.…...

AtCoder Beginner Contest 003(A - 社の給料、B -トランプ、C -プログラミング講座、D - 社の冬 )题目讲解

前言 又更新AtCoder Beginner Contes 的题目讲解啦!! 希望能给诸位带来帮助。 话不多说,开始讲解: A - 社の給料←题目翻译 为了解决这个问题,我们需要计算青木每月完成正好N个任务时的平均工资。通过分析,我们可以发现这个问题可以通过数学公式直接求解,而不需要复…...