【问题标题】:c++ vectors with ostream and istream带有ostream和istream的c++向量
【发布时间】:2018-04-25 17:51:59
【问题描述】:

我遇到了一个我无法理解的代码的鼻屎。前提是输入一组数字:3 4 5 6 7,会输出:4x^(3)+5x^(2)+6x^(1)+7x^ (0) 使用 istream 和 ostream。我正在为数字使用向量,而我遇到的问题是向量没有正确填充。

例如,如果向量被称为 vec1,则上面的输入给出:

    `vec1[0]==4
    vec1[1]==5
    vec1[2]==6
    vec1[3]==4
    vec1[4]==4`

但我希望它输出:

    `vec1[0]==3
    vec1[1]==4
    vec1[2]==5
    vec1[3]==6
    vec1[4]==7`

我找不到任何有关将 istream 与矢量一起使用的示例教程,所以我希望有人可以帮助我了解将 istream 与矢量一起使用的基础知识吗?只是一个一般的例子绝对很棒!

PS:我是 C++ 新手,所以如果我在任何地方使用的术语有误,我深表歉意。

编辑:(这是我目前的 istream 代码):

    istream& operator>>(istream& left, Polynomial& right) //input
    {
        int tsize, tmp;

        while (!(left >> tsize))
        {
            left.clear();
            left.ignore();
        }

        if (tsize < 0)
        {
            tsize *= -1;
        }

        vector<double>tmp1;
        for (int i = 0; i < tsize; i++)
        {
            tmp1.push_back(0);
        }

        right.setPolynomial(tmp1);

        for (int i = 0; i < tsize; i++)
        {
            while (!(left >> tmp))
            {
                left.clear();
                left.ignore();
            } 

        right[i]=tmp;

        }
        //return a value
        return left;
    }

`

    void Polynomial::setPolynomial(vector<double>vec1)
    {
        for (int i = 0; i < vec1.size(); i++)
            polynomial.push_back(vec1[i]);

    }

【问题讨论】:

  • 向我们展示代码。我怀疑你有一些简单而常见的错误。
  • vector&lt;double&gt;tmp1;for (int i = 0; i &lt; tsize; i++) { tmp1.push_back(0); } -- 不需要这样做。仅此而已——vector &lt;double&gt; tmp2(tsize);

标签: c++ vector istream


【解决方案1】:

啊,我明白了。像这样的东西怎么样:

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

// A polynomial is represented as a single non-negative integer N representing 
// the degree, followed by N+1 floating-point values for the coefficients in
// standard left to right order. For example:
//   3 4 5 6 7
// represents the polynomial
//   4x**3 + 5x**2 + 6x + 7

std::istream& operator >> ( std::istream& ins, Polynomial& p )
{
  // You could set p to something invalid/empty here
  // ...

  // Get the degree of the polynomial
  int degree;
  ins >> degree;
  if (degree < 0) ins.setstate( std::ios::failbit );
  if (!ins) return ins;

  // Get the polynomial's coefficients
  std::vector <double> coefficients( degree + 1 );
  std::copy_n( 
    std::istream_iterator <double> ( ins ), 
    degree + 1, 
    coefficients.begin()
  );
  if (!ins) return ins;

  // Update p
  p.setPolynomial( coefficients );
  return ins;
}

正确命名事物会有所帮助,并确保您正确地循环遍历事物。如果出现错误,输入流将正确记录错误,除非度数为负,对此我们需要特殊情况。

我使用了一些标准对象而不是循环;你可以使用任何你觉得更方便的方法:只要记住你的第一个整数值后面有 N+1 个双精度数。

最后,记住要公正地使用多项式函数:如果您可以使用向量一次性设置所有系数,那就这样做吧。

(顺便说一句,这段代码只是我在脑海中输入的。可能出现了错别字和愚蠢的错误。)

edit根据 Caleth 的评论进行了修改。

【讨论】:

  • 您可以使用vector(size_t) 构造函数和coefficients.begin() 而不是back_inserter 以避免在copy_n 中重新分配
猜你喜欢
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 2020-02-10
  • 2015-07-18
  • 2016-07-28
  • 1970-01-01
相关资源
最近更新 更多