我有一个具有这个属性的教授类
vector<int> hDisponibles;
如果我有这个类的 vector
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
最佳答案
std::set
仅提供 const 迭代器,因为在对象处于集合中时对其进行修改会破坏集合不变性。所以,你不能用 std::set
做你想做的事,你需要重新添加新对象,或者使用不同的容器。
关于c++ - 对象中 vector 中的 push_back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719505/