c++ - 对象中 vector 中的 push_back

标签 c++ vector iterator push-back

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

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/

相关文章:

c++ - 拓扑排序不打印所有顶点

c++ - 用于将 weak_ptr 应用于 unique_ptr 的内容

c++ - 如何让 std::make_unique 成为我类的 friend

c++ - 高效共享预编译头文件

c++ - 带有映射迭代器的错误访问内存

java - 如何避免在列表迭代中使用 get(0)

c++ - 调用 std::map::find 时出现 "map/set iterators incompatible"

c++ - dlib matrix<float,0,1> 设置元素

具有可变长度项的 vector 的 C++ 内存管理

c++ - 使用 boost::bind 排序