如何打印二维对向量的值?

时间:2017-08-31 22:02:24

标签: c++ operator-overloading fstream

我有一个包含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);
}

3 个答案:

答案 0 :(得分:2)

如果文件成功打开,则一个问题似乎是以下一行:

for y in range(0, height + 1):
    for x in range(0, width + 1):

您尚未在发出for y in range(0, height - 1): for x in range(0, width - 1): 来电时创建 istringstream(raw_text); Point p; while( raw_text >> p ) 个对象。而是创建了一个临时对象并立即销毁。

这应该是:

std::istringstream

话虽如此,我对编译时没有错误的代码感到惊讶。

答案 1 :(得分:1)

您没有加载文件。您对LoadFFT("...")函数的调用会导致文件名为: ....... txt ,这不是有效的文件名。 stringstream变量(重新)定义是错误的,不需要中间字符串,也不需要它们的c_str()对应物。蒸馏的LoadFFT()函数是:

vector<vector<Point>> LoadFFT(const char* path){
    vector<vector<Point> > matrix;
    ifstream fileFFT(path);
    string raw_text;
    while (getline(fileFFT, raw_text)){
        vector<Point> row;
        istringstream iss(raw_text);
        Point p;
        while (iss >> p){
            row.push_back(p);
        }
        matrix.push_back(row);
    }
    return(matrix);
}

修改后的main()函数允许每行后面的新行:

int main(){
    vector<vector<Point> > FFTfile = LoadFFT("myfile.txt");
    for (size_t i = 0; i < FFTfile.size(); i++){
        for (size_t j = 0; j < FFTfile[i].size(); j++){
            cout << FFTfile[i][j];
        }
        cout << std::endl;
    }
}

您需要的这些标题:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

您可以删除其他人。

答案 2 :(得分:1)

您使用raw_text(string)作为istream类型的输入参数。我不认为这种转换是可能的。 尝试使用istringstream返回值作为“&lt;&lt;”的参数或更改您的代码,以便您从文件直接复制到矩阵。

编辑:上面的评论做得更好,解释它

相关问题