简体   繁体   中英

c++ vectors with ostream and istream

I've had this one booger of a code that I haven't been able to wrap my head around. The premise is to input a set of numbers: 3 4 5 6 7 and it will output: 4x^(3)+5x^(2)+6x^(1)+7x^(0) using istream and ostream. I'm using a vector for the numbers and the issue that I'm having is that the vector isn't filling properly.

For example if the vector is called vec1 the above input is giving out:

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

but I want it to output:

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

I wasn't able to find any tutorials of examples of using istream with a vector, so I was hoping someone could help me with the basics of using istream with a vector? Just a general example would be absolutely great!

PS: I'm new to c++, so I'm sorry if my use of terminology is wrong anywhere.

edit: (here's my istream code currently):

    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]);

    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM