c++读取csv文件并操作数据然后打印错误

标签 c++ c matlab csv import

这是我的问题的延续:c++ reading in text file into vector<vector> then writing to vector or array depending on first word in internal vector .我正在读取一个文件并使用节点坐标的值来计算单元中心,并希望使用标题打印单元中心文件:ID、X、Y、Z,其中 Z 全部为 0。

到目前为止的代码:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>

std::vector<double> GetValues(const std::vector<std::string>& src, int start, int end)
{
    std::vector<double> ret;
    for(int i = start; i <= end; ++i)
    {
      ret.push_back(std::strtod(src[i].c_str(), nullptr));
    }
    return ret;
}

std::vector<double> polycentre(const std::vector<double>&  x,const std::vector<double>&  y,int ID)
{
  std::vector<double> C(3, 0);
  std::vector<double> x1(x.size(),0);
  std::vector<double> y1(y.size(),0);
  int sizx = x.size();
  int sizy = y.size();
  if(sizy != sizx)
    {
      std::cerr << "polycentre inputs not equal length";
    }
  double x0 = x[0];
  double y0 = y[0];
  for(int aa = 1; aa < sizx; ++aa)
    {
      if(x[aa] < x0){x0 = x[aa];}
      if(y[aa] < y0){y0 = y[aa];}
    }
  double A = 0.0;
  double B = 0.0;
  for(int aa = 0; aa < sizx; ++aa)
    {
      x1[aa] = x[aa] - x0;
      y1[aa] = y[aa] - x0;
      if(aa != sizx-1)
    {
      A = A + (x1[aa]*y1[aa+1] - x1[aa+1]*y1[aa]);
      B = B + ((x1[aa]+x1[aa+1])*(x1[aa]*y1[aa-1]-x1[aa-1]*y1[aa]));
    }
      else if(aa == sizx-1)
    {
      A = A + (x1[aa] - y1[aa]);
      B = B + ((x1[aa]+1)*(x1[aa]*1-1*y1[aa]));
        }
    }
  A = A*0.5;
  C[0] = ID;
  C[1] = ((1/6/A)*B)+x0;
  C[2] = ((1/6/A)*B)+y0;
  return C;
}

void PrintValues(const std::string& title, std::vector<std::vector<double>>& v)
{
    std::cout << title << std::endl;
    for(size_t line = 0; line < v.size(); ++line)
    {
        for(size_t val = 0; val < v[line].size(); ++val)
        {
            std::cout << v[line][val] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

int main()
{
    std::vector<std::vector<std::string>> values;
    std::ifstream fin("example.2dm");
    for (std::string line; std::getline(fin, line); )
    {
        std::istringstream in(line);
        values.push_back(
            std::vector<std::string>(std::istream_iterator<std::string>(in),
            std::istream_iterator<std::string>()));
    }

    std::vector<std::vector<double>> cells;
    std::vector<std::vector<double>> nodes;
    for (size_t i = 0; i < values.size(); ++i) 
    {
        if(values[i][0] == "E3T")
        {
      cells.push_back(GetValues(values[i], 1, 5));
        }
        else if(values[i][0] == "E4Q")
        {
      cells.push_back(GetValues(values[i], 1, 6));
        }
        else if(values[i][0] == "ND")
        {
      nodes.push_back(GetValues(values[i], 1, 4));
        }
    }
    std::vector<std::vector<double>> cell_centres;
    for (size_t aa = 0; aa < cells.size(); ++aa)
      {
    if(cells[aa].size() == 4)
      {
        std::vector<double> xs = {nodes[cells[aa][1]][1], nodes[cells[aa][2]][1], nodes[cells[aa][3]][1]};
        std::vector<double> ys = {nodes[cells[aa][1]][2], nodes[cells[aa][2]][2], nodes[cells[aa][3]][2]};
        cell_centres.push_back(polycentre(xs,ys,aa));
      }
      }

    PrintValues("Cell Centres", cell_centres);
    PrintValues("Cells", cells);
    PrintValues("Nodes", nodes);

    return 0;
}

当我运行该程序时,我没有得到关于细胞中心的任何输出:

$ ./a.exe
Cell Centres

Cells
1 19 20 14 16 2
2 17 16 15 23 2
3 22 15 14 21 2
4 4 3 21 20 1
5 6 20 19 7 1
6 18 17 10 9 1
7 17 23 12 11 1
8 7 19 18 8 1
9 22 1 13 23 1
10 14 20 21 2
11 21 2 22 1
12 21 3 2 1
13 22 2 1 1
14 5 20 6 1
15 20 5 4 1
16 16 14 15 2
17 23 13 12 1
18 22 23 15 2
19 17 11 10 1
20 17 18 16 2
21 8 18 9 1
22 18 19 16 2

Nodes
1 -325.811 77.0286 0
2 -324.209 76.0395 0
3 -323.012 74.4784 0
4 -322.754 72.5327 0
5 -323.617 70.8079 0
6 -325.162 69.8134 0
7 -327.129 69.876 0
8 -329.096 69.9385 0
9 -330.301 71.4668 0
10 -330.787 73.3242 0
11 -330.836 75.2916 0
12 -329.587 76.5401 0
13 -327.743 77.227 0
14 -326.109 73.2068 0
15 -327.041 74.207 0
16 -327.35 73.1717 0
17 -329.154 74.0024 0
18 -328.659 71.9967 0
19 -326.846 71.4063 0
20 -325 72.0535 0
21 -324.701 73.9639 0
22 -326.168 75.3361 0
23 -328.06 75.4195 0

谁能告诉我哪里出错了??

顺便说一下,MATLAB 代码是:

函数cell_centres(infil,outfil)

% read 2DM file
MESH = RD2DM(infil);

% get cell centres
if (isfield(MESH,'E3T'))
    ne3 = length(MESH.E3T);
else
    ne3 = 0;
end
if (isfield(MESH,'E4Q'))
    ne4 = length(MESH.E4Q);
else
    ne4 = 0;
end

ne = ne3 + ne4;
ctrd = zeros(ne,2);
id = zeros(ne,1);
z = zeros(ne,1);
k = 1;

if (isfield(MESH,'E3T'))
    for i = 1:length(MESH.E3T)
        pts = MESH.E3T(i,2:4);
        x = MESH.ND(pts,2);
        y = MESH.ND(pts,3);
        z(k) = mean(MESH.ND(pts,4));
        ctrd(k,:) = polycentre(x,y);
        id(k) = MESH.E3T(i,1);
        k = k+1;
    end
end

if (isfield(MESH,'E4Q'))
    for i = 1:length(MESH.E4Q)
        pts = MESH.E4Q(i,2:5);
        x = MESH.ND(pts,2);
        y = MESH.ND(pts,3);
        z(k) = mean(MESH.ND(pts,4));
        ctrd(k,:) = polycentre(x,y);
        id(k) = MESH.E4Q(i,1);
        k = k+1;
    end
end

% order cell ids
[id i] = sort(id,'ascend');
ctrd = ctrd(i,:);
z = z(i);

% write .csv file
fid = fopen(outfil,'w');
fprintf(fid,'%s\n','ID,X,Y,Z');

for aa = 1:ne
    fprintf(fid,'%i,%.7f,%.7f,%.7f\n',id(aa),ctrd(aa,1),ctrd(aa,2),z(aa));
end

fclose(fid);

display('done & done :-)')

干杯

最佳答案

删除下面两行的&

    std::vector<double> xs = {&nodes[cells[aa][1]][1], &nodes[cells[aa][2]][1], &nodes[cells[aa][3]][1]};
    std::vector<double> ys = {&nodes[cells[aa][1]][2], &nodes[cells[aa][2]][2], &nodes[cells[aa][3]][2]};

您需要 double 值而不是 integers(& 运算符获取整数类型的地址)。初始化列表不会为您进行隐式转换。

关于c++读取csv文件并操作数据然后打印错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18907620/

相关文章:

c - 将 -1 返回给用户定义的函数会导致程序终止,退出代码为 0

matlab - 拉格朗日法

c++ - 线程化传递字符串参数的 bool 函数

c++ - 我完全不能包含 boost header

c++ - 我应该以什么顺序将顶点发送到 OpenGL 进行剔除

calloc vs malloc 和时间效率

c++ - bool 版本的 boost::condition_variable::wait_until 使用谓词的行为如何?

c - signal() 调用未触发

matlab - 在 Matlab 中,如何划分多元高斯分布以分离高斯分布?

matlab - .m 文件作为 MATLAB/Octave for Sublime Text