对象中向量中的push_back

发布于 2024-12-23 18:43:27 字数 716 浏览 3 评论 0原文

我有一个具有此属性的教授类

vector<int> hDisponibles;

如果我有此类的向量,

set<profesor> profesores;

我会尝试此操作

set<profesor>::iterator itP;
itP = profesores.begin();    
while ( itP != profesores.end() ){                
    (*itP).hDisponibles->push_back(0);                                                    
    itP++;
}

,但会出现此错误

utils.cpp:138: error: passing ‘const std::vector<int, std::allocator<int> >’ as ‘this’     argument of ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]’ discards qualifiers

I have a profesor class with this atribute

vector<int> hDisponibles;

If I have a vector of this class

set<profesor> profesores;

I try this

set<profesor>::iterator itP;
itP = profesores.begin();    
while ( itP != profesores.end() ){                
    (*itP).hDisponibles->push_back(0);                                                    
    itP++;
}

but this errors

utils.cpp:138: error: passing ‘const std::vector<int, std::allocator<int> >’ as ‘this’     argument of ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]’ discards qualifiers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她说她爱他 2024-12-30 18:43:27

std::set 仅提供 const 迭代器,因为修改集合中的对象会破坏集合不变性。因此,您无法使用 std::set 执行您想要的操作,您需要重新添加新对象或使用不同的容器。

std::set only offers const iterators, because modifying the objects while they're in the set would break set invariant. So, you can't do what you want with std::set, you need to either readd the new object, or use a different container.

属性 2024-12-30 18:43:27

您的问题是您尝试调用 push_backvectorconst。这是因为(自 C++11 起)std::set 的元素是通过 const_iterators 访问的,因为更改它们可能会改变它们的比较方式,从而破坏 代码>设置。要解决这个问题,您有两个选择:

  1. 如果 hDisponiblesprofesor 的比较方式没有影响,并且不会对您的整体结构造成太大损害,您可以声明它 可变像这样:可变向量; hDisponibles;mutable 成员可以更改,即使它们所在的结构是 const
  2. 我这不是一个选项,您必须从设置(到临时),执行 push_back 并重新插入它(小心迭代器失效)。当然,这是相当昂贵的。

您发布的代码有一个额外的错误,因为 hdisponsibles 是一个对象,但 push_back 被调用,就好像它是一个指针一样。从您的编译器消息来看,该错误似乎并不存在于您的实际代码中,但以防万一它应该是:

(*itP).hDisponibles.push_back(0);    

编辑:在标准草案中找到该部分(值类型与setmultiset 的键:N3290 §23.2.4/6

关联容器的迭代器是双向迭代器
类别。对于值类型相同的关联容器
作为键类型,iterator 和 const_iterator 都是常量
迭代器。未指定是否使用 iterator 和 const_iterator
是同一类型。 [注意:iterator 和 const_iterator 具有相同的
在这种情况下的语义,并且 iterator 可转换为 const_iterator。
用户可以通过始终使用来避免违反单一定义规则
const_iterator 在其函数参数列表中。 ——尾注]

Your problem is that the vector you are trying to call push_back on is const. This is because (since C++11) the elements of a std::set are accessed through const_iterators, since changing them might change how they compare and therefore corrupt the set. To solve that you have two options:

  1. If hDisponibles has no effect on how profesor compare and it doesn't do to much damage to your overall structure you can declare it mutable like this: mutable vector<int> hDisponibles;. mutable members can be changed even if the structure they reside in is const
  2. I that is not an option, you have to remove the profesor from the set (into a temporary), do your push_back and reinsert it (be careful about iterator invalidation). Of course this is quite costly.

Your code as posted has an additional bug, since hdisponsibles is an object, but push_back is called as if it was a pointer. From your compilermessage it doesn't seem like that bug is in your actual code, but just in case it should be:

(*itP).hDisponibles.push_back(0);    

Edit: Found the section in the standard draft (the value type is the same as the key for set and multiset: N3290 §23.2.4/6

iterator of an associative container is of the bidirectional iterator
category. For associative containers where the value type is the same
as the key type, both iterator and const_iterator are constant
iterators. It is unspecified whether or not iterator and const_iterator
are the same type. [Note: iterator and const_iterator have identical
semantics in this case, and iterator is convertible to const_iterator.
Users can avoid violating the One Definition Rule by always using
const_iterator in their function parameter lists. —endnote ]

甜`诱少女 2024-12-30 18:43:27

您的代码的明显错误是您在这一行中使用 -> 而不是 .

(*itP).hDisponibles->push_back(0);  // should be itP->hDisponibles.push_back(0);

但我感觉某个地方有更多与语法相关的错误。请发布更完整的代码摘要和您遇到的错误。

The obvious error with your code is that you're using -> instead of . on this line:

(*itP).hDisponibles->push_back(0);  // should be itP->hDisponibles.push_back(0);

But I have a feeling there are more errors related to syntax somewhere. Please post a more complete summary of the code and the errors you're getting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文