如何在c++中清空/清除向量和数组
how do I empty/clear vector and array in c++
假设我有一个类,
Class A {
int x[100];
vector<int> y;
Fill(x);
Fill(y.begin());
B(x);
B(y.begin());
}
Class Fill (pointer) {
*pointer = 0;
++pointer;
*pointer = 1;
++pointer
}
Class B(container) {
//how do I clear/empty the array and the vector passed by class A given only the pointers to them?
//I must clear an array and a vector in THIS class.
//I DO NOT want to fill them with 0s.
//x and y.begin are POINTERS to the first element of the container, not containers
}
dsfsdakfgnsdfgsfdgsdfgsdfghsdfgsdfgersgs
提前谢谢。
对于矢量:
some_a_pointer->y.resize(0);
你不能只使用迭代器(y.begin()
)。
数组的大小永远不会改变,所以最好用0
填充它。
std::vector
有一个名为clear
的方法,它将清除所有元素。
因此my_vector.clear();
将清除所有内容。但是,您不能对数组执行同样的操作。这是不可能的。最多你可以用零来填充它们,或者走错误的路,动态分配数组,然后删除它。不过,我宁愿不处理内存问题,所以我只用零来填满它们。
C++11有一个名为std::array<T,N>
的类,用于编译时大小的静态数组,它有一个称为fill
的方法,可以轻松地将所有内容填充为零(la循环)。你可以用my_array.fill(0);
来称呼它。
相关文章:
- 清除结构向量时会发生什么?
- 在不同的线程中调用向量析构函数或清除
- 向量:清除它与删除它
- 在函数返回时,清除了添加到一对int和字符串向量的矢量中的元素
- C++:清除嵌套向量会导致奇怪的结果
- 将原始指针向量清除给由独特指针拥有的对象
- 当程序输入主要功能时,为什么要清除此std ::向量
- 当负载/清除大量数据时,STD ::向量越慢
- 为什么在向量的向量上使用 clear() 不会清除这些向量的值?
- 在c++中清除对的向量
- 是否可以在析构函数中清除向量
- 清除向量时,stl 向量中的 stl 向量是否超出范围?
- 向量:清除/pop_back不会运行析构函数
- 如何在c++中清空/清除向量和数组
- 如何在<Mat>opencv c++中有效地清除向量
- 清除向量空间
- 清除向量会影响它的容量吗?
- C++ 复制构造函数清除 向量<基*> 派生*
- 清除向量<pointer>,然后重用它
- 清除向量并让一个向量等于另一个向量