简体   繁体   English

如何在C++中删除动态数组的列

[英]How to delete columns of dynamic array in C++

I need help tring to delete columns of 2D array in C++.我需要帮助来删除 C++ 中的二维数组列。 I've tried the operation by making a copy of the array.我已经通过制作数组的副本来尝试该操作。 It succeeded only from the start to middle or the middle to the end of array.它只从数组的开始到中间或中间到末尾成功。 witch algorithm to jump unwanted columns in the array when making copy.在复制时跳过数组中不需要的列的女巫算法。

由于是动态数组,因此数组的变量是第一个值的指针,因此您无法获取大小,甚至无法使用 sizeof 来获取大小,但是在不知道大小的情况下使用动态数组是不可能的,无论是固定的还是在运行时抛出变量

This probably isn't the best way to do this but its' a potential solution这可能不是最好的方法,但它是一个潜在的解决方案

int removeColumn(int a){
vector<vector<int>> ret(M);
int i = 0;
for(vector<int> temp : Arr){
        vector<int> tempp(N-1);
        copy(temp.begin(), temp.begin()+a, tempp.begin());
        copy(temp.begin()+a+1,temp.end(),tempp.begin()+a);
        ret[i] = tempp;
        i++;
}
Arr = ret;
return 0;

} }

Essentially you iterate over each row and use two copy statements to copy from the beginning to an index and from said index + 1 to the end.本质上,您遍历每一行并使用两个复制语句从开头复制到索引,然后从所述索引 + 1 复制到结尾。 I'm sure there's a way to do this more elegantly, you could try looking at for_each loops however I doubt you could do it exclusively with copy.我确信有一种方法可以更优雅地做到这一点,您可以尝试查看 for_each 循环,但我怀疑您是否可以仅使用 copy 来完成。 Tested it with this code.使用此代码对其进行了测试。

https://repl.it/repls/PrettyTerrificOmnipage https://repl.it/repls/PrettyTerrificOmnipage

Hope this helps希望这可以帮助

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

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