如何打印二维对向量的值? [英] How to print the values of two dimensional pair vector?

查看:87
本文介绍了如何打印二维对向量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个4行3列的文本文件

I have a text file with 4 rows and 3 columns

(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的值.该脚本不会引发任何错误,但不会打印值.知道怎么了吗?

I wrote following code to print the values of FFTfile. The script does not throw any errorm but it does not print the values. Any idea whats wrong?

#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);
}

推荐答案

如果文件成功打开,则似乎是以下几行:

If the file opened successfully, one issue seems to be the following set of lines:

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

在发出>>调用时尚未创建std::istringstream对象.而是创建了一个临时对象并立即将其销毁.

You have not created a std::istringstream object at the point where you are issuing the >> call. Instead a temporary object was created and immediately destroyed.

这应该是:

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

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

Having said this, I'm surprised the code compiled with no errors.

这篇关于如何打印二维对向量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆