I have this vector in an external file
1 4 12 3
13 3 22 5
14 22 2 34
222 11 3 31
and I want to delete x column and y row. How can I do it and print it in an external file?
It's a 2D vector with 4 rows and columns with 4elements.
#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;
}
}
In this example, I've hardcoded values of x, y, and the file names.
#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;
}
Probably you need to use some container instead of a plain 2d array in case of pushing and poping from a middle of it. Try to use this http://i.stack.imgur.com/G70oT.png for picking up right or search images by keywords std,container,choose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.