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

How to print the values of two dimensional pair vector?

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

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

    istringstream(raw_text);
    Point p;
    while( raw_text >> p )

您尚未在发出 >> 调用时创建 std::istringstream 对象。相反,一个临时对象被创建并立即销毁。

这应该是:

    istringstream strm(raw_text);
    Point p;
    while( strm >> p )

话虽如此,我很惊讶代码编译没有错误。

您没有加载文件。您对 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;
    }
}

这些headers你需要:

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

您可以删除其他的。

您正在使用 raw_text(字符串)作为 istream 类型的输入参数。我不认为这样的转换是可能的。 尝试使用 istringstream return 值作为“<<”的参数或更改代码以便从文件直接复制到矩阵。

编辑:上面的评论更好地解释了它