清空向量的特定元素

发布于 2024-11-24 23:04:31 字数 226 浏览 0 评论 0原文

我想将向量的特定位置处的元素设置为空,因此可以按如下方式执行

vector<IXMLDOMNodePtr> vec1;// filled somehow
vec1[i] = nullptr;// some specific position i

:我想保留为空的条目,它的作用就像占位符,所以我想也许 vec[i] = 0 可以吗?

I want to set an element at a specific position of a vector to null, so can do it as the following:

vector<IXMLDOMNodePtr> vec1;// filled somehow
vec1[i] = nullptr;// some specific position i

ps. I want to keep the entry that is nulled, which acts like a place holder, so I think maybe vec[i] = 0 will do?

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

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

发布评论

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

评论(6

静谧幽蓝 2024-12-01 23:04:31

要删除元素(如果这就是 NULL 的意思),您可以使用 vector::erase

vec1.erase(vec1.begin() + i);

或者,创建一个 IXMLDOMNodePtr 的虚拟对象,根据您的编码标准,该对象被视为 NULL,并设置该对象:

const IXMLDOMNodePtr ixNULL;  // this dummy is specially created as NULL
vec1[i] = ixNULL;
// ...
if(vec1[j] == ixNULL)
 //...

为此你需要超载IXMLDOMNodePtr::operator ==

For removal of the element (if that's what you mean by NULL) you can use vector::erase

vec1.erase(vec1.begin() + i);

Or else, create a dummy object of IXMLDOMNodePtr, which is considered a NULL according to your coding standard and set that object:

const IXMLDOMNodePtr ixNULL;  // this dummy is specially created as NULL
vec1[i] = ixNULL;
// ...
if(vec1[j] == ixNULL)
 //...

For that you need to overload IXMLDOMNodePtr::operator ==.

谈情不如逗狗 2024-12-01 23:04:31

可能 erase() 会有所帮助,例如

vec1.erase(vec1.begin() + i);

,当您迭代下一个时,该条目将不存在...

现在我们知道您想要做什么,这样的事情应该可以工作..

vector<boost::optional<IXMLDOMNodePtr> > vec1;
// populate
// to erase
vec1[i] = boost::none; // now effectively null.

这不知道这个 IXMLDOMNodePtr 是什么(如果它是顾名思义的指针),那么只需将其设置为 0 就可以了。

vec1[i] = 0;

注意:如果动态分配此对象,将其设置为 0 不会像在 Java 中将某些内容设置为 null 那样清除内存 - 在 C++ 中,您必须显式清理它首先,即

delete vec1[i];
vec1[i] = 0;

May be erase() will help, e.g.

vec1.erase(vec1.begin() + i);

When you iterate through next, that entry will not be there...

Now that we know what you want to do, something like this ought to work..

vector<boost::optional<IXMLDOMNodePtr> > vec1;
// populate
// to erase
vec1[i] = boost::none; // now effectively null.

This is without knowing what this IXMLDOMNodePtr is (if it is as the name implies a pointer), then simply setting it to 0 ought to work.

vec1[i] = 0;

NOTE: If you dynamically allocated this object, setting it to 0 does not clear up the memory as setting something to null does in Java - in C++ you have to explicitly clean it up first, i.e.

delete vec1[i];
vec1[i] = 0;
顾北清歌寒 2024-12-01 23:04:31

不会。向量在设计上是连续的。这意味着 .size()==5 的向量具有 [0][4] 元素。

您可能需要一个 std::map

std::map<int, IXMLDOMNodePtr> vec1 = foo();
vec1.erase(i);

No. A vector is contiguous by design. That means that a vector with .size()==5 has elements [0] to [4].

You may want a std::map<int, IXMLDOMNodePtr>:

std::map<int, IXMLDOMNodePtr> vec1 = foo();
vec1.erase(i);
红尘作伴 2024-12-01 23:04:31

使用vec1.resize(whatever_size_you_need);,它将扩展向量并将每个元素默认为0。然后您可以根据需要使用[]

Use vec1.resize(whatever_size_you_need); and it will expand the vector and default each element to 0. Then you can use [] as much as you want.

我一直都在从未离去 2024-12-01 23:04:31

您可以使用 vec1.erase((vec1.begin()+i); 删除向量的第 i 个元素。之后 vec1.size() 减 1,所以如果您必须编写一个循环来擦除向量的某些元素,一个好的做法是使用向后循环;)

You can use vec1.erase((vec1.begin()+i); to erase the i-th element of the vector. After that vec1.size() is reduced by 1, so if you have to write a cycle that erases some elements of the vector, a good practice is to use a backwards cycle ;)

┼── 2024-12-01 23:04:31

不,你不能。您需要初始化向量以包含元素

vector<IXMLDOMNodePtr> vec1(size, NULL);

或推回空元素

vec1.push_back(null);

no you can't. you need to initialize the vector to have element

vector<IXMLDOMNodePtr> vec1(size, NULL);

or push back null elements

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