我有4行3列如何打印二維對向量的值?
(0.165334,0) (0.166524,-0.0136064) (-0.144899,0.0207161)
(0.205171,0) (0.205084,-0.0139042) (-0.205263,0.0262445)
(0.216684,0) (0.215388,-0.0131107) (-0.193696,0.0251303)
(0.220137,0) (0.218849,-0.0135667) (-0.194153,0.025175)
我寫了下面的代碼打印的FFTfile
值的文本文件。該腳本不會引發任何錯誤,但不會打印值。任何想法什麼是錯的?
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <dirent.h>
#include <string>
#include <sstream>
using namespace std;
class Point
{
public:
double x;
double y;
friend istream& operator>>(istream& input, Point& p);
friend ostream& operator<<(istream& s, Point& p);
double getX(void);
double getY(void);
};
double Point::getX(void){
return x;
}
double Point::getY(void){
return y;
}
istream& operator>>(istream& input, Point& p)
{
char c;
input >> c; // Read open parenthesis
input >> p.x;
input >> c; // Read comma
input >> p.y;
input >> c; // Read closing parenthesis
return input;
};
ostream& operator<<(ostream& s, Point& p)
{
s << p.getX() << ", " << p.getY();
return s;
}
vector<vector<Point> > LoadFFT(string path){
string Filename;
vector<vector<Point> > matrix;
Filename.append(path);
Filename.append("....txt");
ifstream fileFFT(Filename.c_str());
string raw_text;
while(getline(fileFFT, raw_text)){
vector<Point> row;
istringstream(raw_text);
Point p;
while(raw_text >> p){
row.push_back(p);
}
matrix.push_back(row);
}
return(matrix);
}
int main(){
vector<vector<Point> > FFTfile=LoadFFT("...");
for (int i = 0; i < FFTfile.size(); i++)
{
for (int j = 0; j < FFTfile[i].size(); j++){
cout << FFTfile[i][j];
}
}
return(0);
}
你永遠不會測試文件是否成功打開。你怎麼能確定該文件實際打開? – Fureeish
我只是一個小菜人!用C/C++經驗還不到一個月,你能幫上忙嗎? – Spandy
你缺乏經驗並不能讓你受到不同的待遇。我建議檢查文件打開是否成功。你有沒有試過檢查你的程序是否適用於它? – Fureeish