【问题标题】:Read the contents of a file and store them in a vector using std::copy读取文件的内容并使用 std::copy 将它们存储在向量中
【发布时间】:2014-02-06 16:47:10
【问题描述】:

我有以下代码:

    std::istream_iterator<std::string> eos;              
    std::istream_iterator<std::string> iit (File);
    std::vector<std::string> result;
    std::copy(iit,eos,std::back_inserter(result));
    for(auto it=result.begin(); it!=result.end(); ++it )
        std::cout << *it << std::endl;

我正在使用迭代器读取File 的内容并将结果存储到我的向量中,我不想使用任何whilefor 循环,我只想使用任何@ 987654325@函数,所以我尝试了copy。文件内容格式如下:

Pork bone:4
Pigs trotters:4
Loin of pork:4

问题是前面的代码读取每一行直到空格或换行符,所以我的向量看起来像:

Pork
bone:4
Pigs
trotters:4
Loin
of
pork:4

我希望每一行都成为我向量中的一个新元素,知道如何解决这个问题吗?

【问题讨论】:

    标签: c++ stl


    【解决方案1】:

    看起来How do I iterate over cin line by line in C++?已经有一个堆栈溢出示例

    引用相关位:“

    class line {
        std::string data;
    public:
        friend std::istream &operator>>(std::istream &is, line &l) {
            std::getline(is, l.data);
            return is;
        }
        operator std::string() const { return data; }    
    };
    

    例如,要将文件中的所有行读入字符串向量,您可以使用以下内容:

    std::vector<std::string> lines;
    
    std::copy(std::istream_iterator<line>(std::cin), 
              std::istream_iterator<line>(),
              std::back_inserter(lines));
    

    " 免责声明:我自己没有尝试过,我只是根据 Jerry Coffin 的声誉等来信任他。

    【讨论】:

      【解决方案2】:

      您可以创建自己的基于行的迭代器:

      template<typename T>
      class line_iterator
          : public std::iterator<std::input_iterator_tag, T>
      {
      public:
          typedef std::istream istream_type;
          typedef T value_type;
      
          line_iterator(istream_type& is) : ptr(&is) { getInput(); }
          line_iterator() : ptr(nullptr) { }
      
          T const& operator*() const
          {
              return value;
          }
      
          line_iterator& operator++()
          {
              getInput();
              return *this;
          }
      
          T* operator->()
          {
              return &value;
          }
      
          line_iterator operator++(int)
          {
              line_iterator copy(*this);
              ++*this;
              return copy;
          }
      
          friend bool operator==(const line_iterator& lhs, const line_iterator& rhs)
          {
              return lhs.ptr == rhs.ptr;
          }
      
          friend bool operator!=(const line_iterator& lhs, const line_iterator& rhs)
          {
              return !(lhs == rhs);
          }
      private:
          T value;
          istream_type* ptr;
      
          void getInput()
          {
              if (ptr)
              {
                  if (!std::getline(*ptr, value)) ptr = nullptr;
              }
          }
      };
      

      现在你可以简单地做:

      std::copy(line_iterator<std::string>(File),
                line_iterator<std::string>(), std::back_inserter(result));
      

      【讨论】:

        【解决方案3】:

        似乎除了使用 std::getline 和相同的 while 循环之外别无他法。

        【讨论】:

          【解决方案4】:

          最好不要使用 istream_iterator,最好使用 istream::getline 函数。

          getline vs istream_iterator

          http://www.cplusplus.com/reference/istream/istream/getline/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-06-17
            • 1970-01-01
            • 1970-01-01
            • 2023-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多