如何从二维数组中删除
How to delete from 2D array?
我有一个外部文件
1 4 12 3
13 3 22 5
14 22 2 34
222 11 3 31
,我想删除x列和y行。我怎样才能把它打印到外部文件中呢?
它是一个4行4列4元素的二维矢量。
#include<iostream>
#include<fstream>
using namespace std;
void main ()
{
int a[100][100],m,n;
ifstream f("mat.txt");
ofstream b("out.txt");
f>>n;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
f>>a[i][j];
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
b<<a[i][j]<<" ";
}
b<<endl;
}
}
在本例中,我已经硬编码了x、y和文件名的值。
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin("in.txt");
ofstream fout("out.txt");
int i, j, x = 1, y = 2;
if (fin.is_open() && fout.is_open()) {
for (i = 0; i < 16; ++i) {
fin >> j;
if (i % 4 != x && i / 4 != y) {
fout << j;
if (i % 4 == 3) {
fout << endl;
} else {
fout << " ";
}
}
}
fin.close();
fout.close();
}
return 0;
}
可能您需要使用一些容器而不是普通的2d数组,以防从中间推入和弹出。尝试使用此https://i.stack.imgur.com/G70oT.png通过关键字std,container,choose.
相关文章:
- 将值从二维数组输出到文本文件
- 在二维数组中查找最小值和最大值?
- 移动二维数组中的字符
- 如何正确填充在堆上分配的二维数组?
- 传递二维数组时出现问题
- 具有随机数的二维数组不会更改
- 如何在C++中获取二维数组中最少的一列数?
- 如何使用用户输入变量制作二维数组?
- 尝试从动态分配的二维数组 C++ 中读取值时出现分段错误,并在尝试删除它时给出 munmap_chunk():
- 如何删除这种二维数组?
- 如何正确删除具有已知二维大小的二维数组
- 如何删除二维数组中的元素
- 正在删除模板化的C++二维数组
- 删除二维数组的矢量
- 二维数组被函数修改后如何删除
- 初始化和删除二维数组
- 删除C++中的二维数组
- 删除动态创建的二维数组时出现访问冲突消息
- 删除二维数组
- 如何从二维数组中删除