c++ - 带有 vector 的重载流插入运算符

标签 c++ vector operator-overloading insertion

我正在尝试为一个只有 vector 成员的类编写一个重载的流插入运算符。它是 Point 的 vector s,这是一个 struct包含两个 double秒。 我想我想要的是将用户输入(一堆 double s)插入到流中,然后将其发送到修改器方法。我正在处理其他流插入示例,例如:

std::ostream& operator<< (std::ostream& o, Fred const& fred)
 {
   return o << fred.i_;
 }

但是当我尝试类似的事情时:

 istream & operator >> (istream &inStream, Polygon &vertStr)
     {
        inStream >> ws;
        inStream >> vertStr.vertices;
        return inStream;
     }

我收到错误消息“不匹配 operator >> 等等。”如果我离开 .vertices ,它编译,但我认为这是不对的。 (顺便说一句,vertices 是我的 vector <Point> 的名称。)即使它是正确的,我实际上也不知道在我的程序中使用什么语法来使用它,而且我也不是 100%确定我的修改器方法需要是什么样子。

这是我的 Polygon类:

//header

#ifndef POLYGON_H
#define POLYGON_H
#include "Segment.h"
#include <vector>
class Polygon
{
 friend std::istream & operator >> (std::istream &inStream, Polygon &vertStr);
 public:
   //Constructor
    Polygon(const Point &theVerts);
    //Default Constructor
    Polygon();
    //Copy Constructor
    Polygon(const Polygon &polyCopy);
    //Accessor/Modifier methods
    inline std::vector<Point> getVector() const {return vertices;}
    //Return number of Vector elements
    inline int sizeOfVect() const {return (int) vertices.capacity();}
    //add Point elements to vector
    inline void setVertices(const Point &theVerts){vertices.push_back (theVerts);}

 private:
   std::vector<Point> vertices;
};
#endif

//Body

using namespace std;
 #include "Polygon.h"
// Constructor
Polygon::Polygon(const Point &theVerts)
    {
        vertices.push_back (theVerts);
        }
//Copy Constructor
Polygon::Polygon(const Polygon &polyCopy)
{
    vertices = polyCopy.vertices;
}
//Default Constructor
Polygon::Polygon(){}

istream & operator >> (istream &inStream, Polygon &vertStr)
 {
    inStream >> ws;
    inStream >> vertStr;
    return inStream;
 }

抱歉这么含糊;一位讲师只是给了我们一个流插入的简短示例,然后就让我们自己动手了。

最佳答案

问题是没有用于 vector 的标准提取 运算符(插入到流中,从流中提取),因此您需要定义自己的运算符。此外,流默认会跳过空格,因此您通常不需要使用 std::ws .您没有定义 vector 输入的终止方式,所以我假设换行符表示 vector 的结尾。

既然是学校的问题,我不能给你太多。首先,这里有一些声明供您填写,其中一些可能有用。导入命名空间 std 是错误的形式, 所以下面没有。

#include <list>

// returns true if ch is a horizontal space. Locales are a little tricky,
// so you could skip them for now and instead start with the functions defined 
// in header <ctype>
bool ishs(char ch, std::locale loc=std::locale::global());
// return true if ch is a vertical space
bool isvs(char ch);
// return true if the next character in stream 'in' is a vertical space.
bool eol(std::istream& in);

// reads & discards horizontal spaces
std::istream& hs(std::istream& in);

class Point {
public:
  // Scalar is so you can use 'Point::Scalar' rather than 'double', 
  // making it easy should you which to change the type that a Point holds.
  // When you've covered templates, this will make it trivial to templatize Point.
  typedef double Scalar;
  ...
};

class Polygon {
public:
     // adds pt as the last of this polygon's vertices
     // Note: this is basically your "setVertices" with a different name
   Polygon& append(const Point& pt);
     // adds the points from 'start' to 'end' to this polygon
   template <typename _Iter>
   Polygon& append(_Iter start, _Iter end);
     // remove all points in this polygon
   void erase();
     // returns the number of sides on this polygon, 
     // which is also the number of vertices
     // Note: this is different from your "sizeOfVect"; see below for more
   int sides() const { return vertices.size(); }
     // set aside space for polygon to have 's' sides.
   voids sides(int s) { vertices.resize(s); }
}

/* reads the next two numbers on the current line into pt.
   Throws an exception if there is only one number.
 */
std::istream& operator>>(std::istream& in, Point& pt);
/* reads numbers on the current line into points on 'poly'.
   Throws an exception if there is only one number. Preferably,
   won't alter 'poly' if there are an odd amount of numbers.

   you could also put the contents of this operator into >>(istream&, Polygon&)
 */
std::istream& operator>>(std::istream& in, std::vector<Point>& vertices) {
    std::list<Point::Scalar> points;
    Point pt;
    // while not at eol(in), read into pt and add it to points
    // After that, empty vertices, then add the Points in points to vertices
    ...
}

作为字符相关函数( ishsisvshseol )的替代方法,您可以使用 getline 将下一行读入字符串中,然后进入一个 istringstream 并从中读取要点。当 istringstream 时 vector 结束达到 eof (或 strin >> pt is false )。

你需要做的是把operator>>(istream&, vector<Point>&)中的任务转过去进入方法和函数调用,然后:

  1. 声明新的方法和函数。
  2. 写下任务描述(如 >> 评论中所做的那样)。
  3. 将描述转化为方法和函数调用。
  4. 重复,直到没有更多的新方法或函数可写。

为什么我的Polygon::sides()与您的 Polygon::sizeOfVect() 不同: 请注意 vector::capacity返回 vector 在不调整大小的情况下可以容纳的元素数;也就是说,它基本上是 sizeof(vect)/typeof(element)。 vector::size是当前存储在 vector 中的元素数。如果您为元素预分配空间,这两者可能会有所不同,例如 Polygon::sides(int)确实如此,或者如果您从背面弹出元素。然而无论如何,vector::capacityvector::size .

关于c++ - 带有 vector 的重载流插入运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2564625/

相关文章:

c++ - (c++) 需要帮助为我的类 Polynomial 重载 operator+

c++ - 让 printf 忽略零值上的负号

c++ - 虚拟多接口(interface)

python - 如何使用 swig 修改类构造函数以保留对构造函数参数之一的引用?

c++ - 获取 out_of_range : vector error for c++ but can't figure out why

c++ - 类指针类和 ->* 运算符

c++ - 为什么 myClassObj++++ 不会产生编译错误 : '++' needs l-value just as buildin type do?

c++ - 当我尝试输入值时程序崩溃

c++ - 将 vector 推回不同的位置

string - 用字符串数组折叠