【问题标题】:How to assign each row of a text file to a new vector?如何将文本文件的每一行分配给一个新向量?
【发布时间】:2017-08-30 22:44:12
【问题描述】:

我是 C++ 菜鸟,我有一个 4 行 3 列的文本文件,其中每一行对应一个传感器信号。如何将每一行加载到单独的vector<float>

(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) 

这是我目前所拥有的,但这段代码将数据加载为字符串。我想将我的最终数据加载为vector<vector<float>>

vector<vector<string> > input;    
ifstream fileFFT(Filename.c_str());
string line;
while(getline(fileFFT, line)){
    if(line.empty()){
        continue;
    }

    stringstream row(line);
    vector<string> values((istream_iterator<string>(row)),(istream_iterator<string>()));       //end

    input.push_back(values);

}

【问题讨论】:

  • 您是否需要字符串向量的向量?还是一个字符串向量就足够了?
  • 在 StackOverflow 中搜索“c++ 读取文件 CSV”。已经有很多类似的问题了。
  • @spandy,如果您知道元素的长度总是两个,那么使用std::pairstd::array 甚至您自己的类或结构可能会更好,而不是嵌套向量。跨度>
  • 不,你不需要矢量>。你想要std::vector&lt;std::vector&lt;Point&gt; &gt;std::vector&lt;std::vector&lt;std::pair&lt;float,float&gt;&gt;&gt;
  • 使用自定义结构、元组、对或数组。不要使用向量。向量用于动态内容,或者在编译时大小未知。使用更固定的数据结构,您的代码稍后会感谢您。上面的所有建议都强调了如何做到这一点。

标签: c++ text-files fstream


【解决方案1】:

这里有一些东西可以帮助你开始:

class Point
{
public:
  double x;
  double y;
  friend std::istream& operator>>(std::istream& input, Point& p);
};

std::istream& operator>>(std::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;
};

//...
std::string row_text;
std::vector<std::vector<Point>> matrix;
while (std::getline(my_file, row_text))
{
  std::vector<Point> row;
  std::istringstream(row_text);
  Point p;
  while (row_text >> p)
  {
    row.push_back(p);
  }
  matrix.push_back(row);
}

我创建了一个Point 类来表示这对浮点数。

我还重载了operator&gt;&gt; 以使阅读Point 更容易。

循环读取一个记录或文本行,然后从文本行创建一个Point 向量。
然后将记录或行附加到矩阵中。

【讨论】:

  • 如何打印matrixvector的元素?
  • 你可以使用matrix[row][column]
  • 我得到以下`错误:'operator}'和'Point')`跨度>
  • Point 类重载operator &lt;&lt; 打印到std::ostream。互联网上有大量关于如何超载operator&lt;&lt; 的示例。
  • 感谢您的帮助,
【解决方案2】:

您已经有了一半的答案 - 使用 std::getline() 读取每一行,然后使用 std::(i)stringstream 处理每一行。

现在,您缺少的是另一半 - 解析每一行。既然你已经知道如何使用std::istream_iterator,我会做这样的事情:

typedef std::pair<float, float> SensorValue;
typedef std::vector<SensorValue> SensorValues;

std::istream& operator>>(std::istream &in, SensorValue &out)
{
    float f1, f2;
    char ch1, ch2, ch3;

    if (in >> ch1 >> f1 >> ch2 >> f2 >> ch3)
    {
        if ((ch1 == '(') && (ch2 == ',') && (ch3 == ')'))
            out = std::make_pair(f1, f2);
        else
            in.setstate(std::ios_base::failbit);
    }

    return in;
}

...

std::vector<SensorValues> input;

std::ifstream fileFFT(Filename.c_str());
std::string line;

while (std::getline(fileFFT, line))
{
    if (line.empty())
        continue;

    std::istringstream row(line);

    SensorValues values;
    std::copy(std::istream_iterator<SensorValue>(row), std::istream_iterator<SensorValue>(), std::back_inserter(values));

    input.push_back(values);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多