如何迭代到一个较小的容器(即步幅!= 1)

时间:2011-03-24 18:57:27

标签: c++ stl iterator stl-algorithm

在精神上here有一个非常相似的问题。不幸的是,这个问题没有引起太多反应 - 我想我会问一个更具体的问题,希望可以提出另一种方法。

我正在将二进制文件写入std::cintar --to-command=./myprog)。 二进制文件恰好是一组浮点数,我想将数据放入std::vector<float> - 理想情况下是c ++方式。

我可以很好地生成std::vector<char>(感谢this answer

#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>

int
main  (int ac, char **av)
{
  std::istream& input = std::cin;
  std::vector<char> buffer;
  std::copy( 
        std::istreambuf_iterator<char>(input), 
           std::istreambuf_iterator<char>( ),
           std::back_inserter(buffer)); // copies all data into buffer
}

我现在想要将std::vector<char>转换为std::vector<float>,大概使用std::transform和执行转换的函数(char[2]float ,说)。然而,我正在努力,因为std::vector<float>的元素数量将是std::vector<char>的一半。如果我能以2的步幅进行迭代,那么我认为我会没事的,但从上一个问题来看,似乎我不能这样做(至少不是优雅的)。

3 个答案:

答案 0 :(得分:5)

我会编写自己的类,读取两个字符并将其转换为float。

struct FloatConverter
{
    // When the FloatConverter object is assigned to a float value
    // i.e. When put into the vector<float> this method will be called
    //      to convert the object into a float.
    operator float() { return 1.0; /* How you convert the 2 chars */ }

    friend std::istream& operator>>(std::istream& st, FloatConverter& fc)
    {
        // You were not exactly clear on what should be read in.
        // So I went pedantic and made sure we just read 2 characters.
        fc.data[0] = str.get();
        fc.data[1] = str.get();
        retun str;
    }
    char   data[2];
 };

基于GMan的评论:

struct FloatConverterFromBinary
{
    // When the FloatConverterFromBinary object is assigned to a float value
    // i.e. When put into the vector<float> this method will be called
    //      to convert the object into a float.
    operator float() { return data }

    friend std::istream& operator>>(std::istream& st, FloatConverterFromBinary& fc)
    {
        // Use reinterpret_cast to emphasis how dangerous and unportable this is.
        str.read(reinterpret_cast<char*>(&fc.data), sizeof(float));
        retun str;
    }

    float  data;
};

然后像这样使用它:

int main  (int ac, char **av)
{
  std::istream& input = std::cin;
  std::vector<float> buffer;

  // Note: Because the FloatConverter does not drop whitespace while reading
  //       You can potentially use std::istream_iterator<>
  //
  std::copy( 
           std::istreambuf_iterator<FloatConverter>(input), 
           std::istreambuf_iterator<FloatConverter>( ),
           std::back_inserter(buffer));
}

答案 1 :(得分:0)

在我看来,最好的答案是编写一对自己的迭代器,以你想要的方式解析文件。您可以将std::vector<char>更改为std::vector<float>并使用相同的streambuf迭代器,前提是输入格式化的值之间至少有一个空格。

答案 2 :(得分:0)

使用增强范围适配器:

boost::copy(istream_range(input)|stride(2),back_inserter(buffer));

您可能需要编写自己的istreambuf_iterator,即trivial