对象中向量中的push_back
我有一个具有此属性的教授类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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 withstd::set
, you need to either readd the new object, or use a different container.您的问题是您尝试调用
push_back
的vector
是const
。这是因为(自 C++11 起)std::set
的元素是通过const_iterators
访问的,因为更改它们可能会改变它们的比较方式,从而破坏代码>设置。要解决这个问题,您有两个选择:
hDisponibles
对profesor
的比较方式没有影响,并且不会对您的整体结构造成太大损害,您可以声明它可变
像这样:可变向量; hDisponibles;
。mutable
成员可以更改,即使它们所在的结构是const
push_back
并重新插入它(小心迭代器失效)。当然,这是相当昂贵的。您发布的代码有一个额外的错误,因为
hdisponsibles
是一个对象,但push_back
被调用,就好像它是一个指针一样。从您的编译器消息来看,该错误似乎并不存在于您的实际代码中,但以防万一它应该是:编辑:在标准草案中找到该部分(值类型与
set
和multiset
的键:N3290 §23.2.4/6Your problem is that the
vector
you are trying to callpush_back
on isconst
. This is because (since C++11) the elements of astd::set
are accessed throughconst_iterators
, since changing them might change how they compare and therefore corrupt theset
. To solve that you have two options:hDisponibles
has no effect on howprofesor
compare and it doesn't do to much damage to your overall structure you can declare itmutable
like this:mutable vector<int> hDisponibles;
.mutable
members can be changed even if the structure they reside in isconst
profesor
from the set (into a temporary), do yourpush_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, butpush_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:Edit: Found the section in the standard draft (the value type is the same as the key for
set
andmultiset
: N3290 §23.2.4/6您的代码的明显错误是您在这一行中使用
->
而不是.
:但我感觉某个地方有更多与语法相关的错误。请发布更完整的代码摘要和您遇到的错误。
The obvious error with your code is that you're using
->
instead of.
on this line: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.