C++ unordered_set XXX in read-only object

本文探讨了如何在C++中使用unordered_set来存储包含计数信息的元素,并解决了一个关键问题:如何更新这些元素的计数字段而不违反unordered_set的规则。通过将计数字段标记为mutable或者使用unordered_map作为替代方案,文章提供了实用的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

还是使用struct作为unordered_set的数据结构,假设我们有如下代码:

struct SentimentWord {
  string word;
  int count;
};


size_t SentimentWordHash::operator () (const SentimentWord &sw) const {
  return hash<string>()(sw.word);
}


bool operator == (SentimentWord const &lhs, SentimentWord const &rhs){
  if(lhs.word.compare(rhs.word) == 0){
    return true;
  }
  return false;
} 

int main()
{
//...
//...
    unordered_set<SentimentWord, SentimentWordHash> positiveWords;
    unordered_set<SentimentWord, SentimentWordHash>::iterator iter;
    iter = positiveWords.find(temp);
    if(iter != positiveWords.end()){
      iter->count++;
    }
}

理论上说,unordered_set中的key一旦插入是不可修改的:

In an unordered_set, the value of an element is at the same time its key, that identifies it uniquely. Keys are immutable, therefore, the elements in an unordered_set cannot be modified once in the container - they can be inserted and removed, though.

考虑到底层红黑树的结构,修改key将会改变整个红黑树的构造。不过,我们已经将unordered_set的hasher设置为用户自定义,并且只使用其中的string成员作为hash函数的输入。

为了解决这个问题,可以做如下修改:

struct SentimentWord {
  string word;
  mutable int count;//使用mutable修饰
};

也可以使用unordered_map把count作为value。

参考

https://stackoverflow.com/questions/16452378/how-to-make-an-iterator-to-a-read-only-object-writable-in-c

unordered_setC++标准库中的一个容器类,它提供了一种存储唯一元素的无序集合。 unordered_set使用哈希表来实现元素的存储和查找,因此它具有快速的插入、删除和查找操作的特点。通过使用哈希函数,unordered_set可以将元素分布在不同的存储桶中,从而实现高效的查找。 在unordered_set中,每个元素被视为键值(key),并且它们是唯一的,不能重复。 unordered_set中的元素的顺序是不确定的,它们不按照插入的顺序进行存储。 引用提供了一些unordered_set的常用成员函数和迭代器的使用方法。可以使用begin()和end()函数来获取unordered_set的迭代器的起始位置和结束位置。cbegin()和cend()函数返回的是const迭代器,它们不允许修改unordered_set中的元素。local_iterator则用于遍历unordered_set中某个特定存储桶中的元素。 总结来说,unordered_set是一个存储唯一元素的无序集合的容器类,它使用哈希表来实现元素的存储和查找。在使用unordered_set时,可以利用迭代器来访问和操作其中的元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++常用语法——unordered_set](https://blog.csdn.net/LiuXF93/article/details/120899401)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++ STL 之 unordered_set 使用(包括unordersd_map)](https://blog.csdn.net/qq_32172673/article/details/85160180)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值