简体   繁体   English

如何将2D向量中的对象设置为空指针[C ++]

[英]How to set objects in 2D vector to null pointer [C++]

Imagine 想像

std::vector<std::vector<Objekt>> m_pole;

We want to set every object to nullptr. 我们想将每个对象设置为nullptr。 But following code produces 2 errors. 但是以下代码会产生2个错误。 Vars row and col are not declared. 未声明Vars row和col。

    std::vector<std::vector<Objekt>>::std::iterator row;
    std::vector<Objekt>::std::iterator col;

    for(row = m_pole.begin(); row != m_pole.end(); row++) {
        for(col = row->begin(); col != row->end(); col++) {
            m_pole[row][col] = nullptr;
        }
    }

Any ideas how to solve this problem ? 任何想法如何解决这个问题? Thanks for answers, MK. 感谢您的回答,MK。

You seem to confuse multiple things. 您似乎混淆了很多事情。

First: you have a vector of vectors of objects. 第一:您有一个对象向量的向量。 That's good! 那很好! However, it's objects, not pointers (again that's good), therefor it can not be null, as only pointers can be null (one last time, this is good, no invalid memory access, null pointer exceptions…). 但是,它是对象,而不是指针(再次证明是好的),因此它不能为null,因为只有指针可以为null(最后一次,这很好,没有无效的内存访问,空指针异常…)。 If you want to handle optional elements, you have to decide between different strategies : 如果要处理可选元素,则必须决定不同的策略:

  1. Not inserting them in your structure 不将它们插入结构中
  2. using pointers and setting them to null (if you do that, please use smart pointers) 使用指针并将其设置为null(如果这样做,请使用智能指针)
  3. using boost::optional 使用boost :: optional

Secondly: you are confusing pointers and indexes 其次:您在混淆指针和索引

std::vector<std::vector<Objekt>>::iterator row_it; // where does this std came from ?
std::vector<Objekt>::iterator element_it; // the iterator points to an element

for(row_it = m_pole.begin(); row_it != m_pole.end(); ++row_it) { // Get the habit to pre-increment
    for(element_it = row_it->begin(); element_it != row->end(); ++element_it) {
        *element_it = nullptr; // This won't work, as it's an object, not a pointer
    }
}

Thirdly: c++11 has nice iterators (other wise use BOOST_FOREACH) : 第三:c ++ 11具有不错的迭代器(否则,请使用BOOST_FOREACH):

for(auto & row : m_pole) // reference is importante !
    for(auto & element : row)
        element = nullptr; // again, can't work because it's an object

You're mixing up iterators and indices. 您正在混合迭代器和索引。 Iterators are dereferenced using the * operator. 使用*运算符可取消对迭代器的引用。

for(row = m_pole.begin(); row != m_pole.end(); row++) 
{
  for(col = row->begin(); col != row->end(); col++)
    *col = nullptr;
}

This is assuming that Objekt is a typedef for a pointer or it has an assignment operator that can take nullptr . 假设Objekt是指针的typedef或它具有可以采用nullptr的赋值运算符。 Otherwise you'll need to use Objekt* 否则,您将需要使用Objekt*

But you can save yourself the trouble of writing an initialization loop and just use the std::vector constructor. 但是您可以省去编写初始化循环的麻烦,而只需使用std::vector构造函数即可。

std::vector<std::vector<Objekt> > m_poles(numrows, std::vector<Objekt>(numcols, nullptr));

You should use pointer: 您应该使用指针:

std::vector<std::vector<Objekt*>> m_pole;
std::vector<std::vector<Objekt*>>::std::iterator row;
std::vector<Objekt*>::std::iterator col;

for(row = m_pole.begin(); row != m_pole.end(); row++) {
    for(col = row->begin(); col != row->end(); col++) {
        *col = nullptr;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM