带有ostream和istream的c ++向量

时间:2017-11-13 05:38:14

标签: c++ vector istream

我有一个代码的鼻屎,我无法解决这个问题。前提是输入一组数字: 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]);

    }

1 个答案:

答案 0 :(得分:1)

Ah, I understand. How about something like this:

#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;
}

Proper naming of things helps, and make sure you loop through things properly. The input stream will properly log error if something is wrong, excepting only if the degree is negative, for which we need a special case.

I have used a few standard objects instead of a loop; you can use whichever you find more convenient: just remember that there are N+1 doubles following your first integer value.

Finally, remember to use your Polynomial's functions justly: if you can use a vector to set all the coefficients in a single pass, just do that.

(BTW, this code was just typed-in off the top of my head. Typos and stupid errors may have occurred.)

edit modified as per Caleth’s comment.