如何在c ++中打印给定桶号的所有元素unordered_set?
How to print all the elements of given bucket number in c++ unordered_set?
我在 c++ 中有一个unordered_set<string> ht
,其中给定的存储桶编号说 x,我必须打印 ht[x] 的给定存储桶中的所有元素,即我正在尝试打印给定存储桶编号 x 中的所有元素。 最后一个 else 块有它:
unordered_multiset<string>ht;
ll m;
cin>>m;
ht.reserve(m);
ll q;
cin>>q;
while (q--){
string cmd;
cin>>cmd;
if(cmd=="add"){
string s;
cin>>s;
if(ht.find(s)==ht.end()){
ht.insert(s);
}
}else if(cmd=="del"){
string s;
cin>>s;
if(ht.find(s)!=ht.end()){
auto it = ht.find(s);
ht.erase(it);
}
}else if(cmd=="find"){
string s;
cin>>s;
if(ht.find(s)!=ht.end()){
cout<<"yes"<<'n';
}else{
cout<<"no"<<'n';
}
}else{
ll x;
cin>>x;
auto it = ht.begin(x);
for(; it!=ht.end(x);it++){
cout<<*it<<' ';
}
cout<<'n';
}
begin(<bucket number>)
和end(<bucket number>)
成员函数可用于迭代特定存储桶中的元素。bucket_count()
成员函数返回存储桶数。所以:
#include <unordered_set>
int main() {
std::unordered_set<int> foo;
for(size_t bno = 0; bno < foo.bucket_count(); ++bno) {
for(auto bit = foo.begin(bno), end = foo.end(bno); bit != end; ++bit) {
const auto& element = *bit;
// do stuff with element
}
}
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 从控制台中删除最后打印的元素
- 擦除while循环中迭代的元素