无法为自定义PriorityQueue重载提取运算符

时间:2016-03-14 20:12:10

标签: c++ operator-overloading data-stream extraction-operator

我正在尝试为{I}编写的自定义operator>>课程重载PriorityQueue,代码如下:

/**
 * @brief Overloaded stream extraction operator.
 *
 * Bitshift operator>>, i.e. extraction operator. Used to write data from an input stream
 * into a targeted priority queue instance. The data is written into the queue in the format,
 *
 * \verbatim
 [item1] + "\t" + [priority1] + "\n"
 [item2] + "\t" + [priority2] + "\n"
 ...
 * \endverbatim
 *
 * @todo Implement functionality for any generic Type and PriorityType.
 * @warning Only works for primitives as template types currently!
 * @param inStream Reference to input stream
 * @param targetQueue Instance of priority queue to manipulate with extraction stream
 * @return Reference to input stream containing target queue data
 */
template<typename Type, typename PriorityType> std::istream& operator>>(std::istream& inStream, PriorityQueue<Type, PriorityType>& targetQueue) {

    // vector container for input storage
    std::vector< std::pair<Type, PriorityType> > pairVec;
    // cache to store line input from stream
    std::string input;

    std::getline(inStream, input);

    if (typeid(inStream) == typeid(std::ifstream)) {
        inStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    // loop until empty line
    while (!input.empty()) {
        unsigned int first = 0;
        // loop over input cache
        for (unsigned int i = 0; i < input.size(); ++i) {
            // if char at index i of cache is a tab, break from loop
            if (input.at(i) == '\t')
                break;
            ++first;
        }
        std::string data_str = input.substr(0, first);
        // convert from std::string to reqd Type
        Type data = atoi(data_str.c_str());

        std::string priority_str = input.substr(first);
        // convert from std::string to reqd PriorityType
        PriorityType priority = atof(priority_str.c_str());

        pairVec.push_back(std::make_pair(data, priority));

        // get line from input stream and store in input string
        std::getline(inStream, input);
    }

    // enqueue pairVec container into targetQueue
    //targetQueue.enqueueWithPriority(pairVec);

    return inStream;

}

目前适用于stdinstd::cin输入,但它不适用于fstream输入 - 第一个getline始终从while输入空行输入使得inStream.ignore()循环永远不会被触发,我似乎无法跳过它(我尝试使用int,如上所示,但这不起作用。

修改

目前我只想让文件输入正常工作,而忽略了它现在仅适用于double数据类型和targetQueue优先级类型的事实 - 这些都不相关(也不是实际操纵3 5.6 2 6.3 1 56.7 12 45.1 对象本身。)

目前我只关心在尝试流式传输文件时解决空行问题。

要传递的示例文件:

\t

每行的数字#include "PriorityQueue.h" #include <sstream> #include <iostream> #include <fstream> int main(void) { // create pq of MAX binary heap type PriorityQueue<int, double> pq(MAX); std::ifstream file("test.txt"); file >> pq; std::cout << pq; } 分开。

测试示例:

#include <iostream>
#include <fstream>
#include <vector>

class Example {

public:

    Example() {}

    size_t getSize() const { return vec.size(); }

    friend std::istream& operator>>(std::istream& is, Example& example);

private:

    std::vector< std::pair<int, double> > vec;

};

std::istream& operator>>(std::istream& is, Example& example) {

    int x;
    double y;
    while (is >> x >> y) {
        std::cout << "in-loop" << std::endl;
        example.vec.push_back(std::make_pair(x, y));
    }
    return is;
}

int main(void) {

    Example example;

    std::ifstream file("test.txt");

    file >> example;

    file.close();

    std::cout << example.getSize() << std::endl;

    return 0;

}

其中&#34; test.txt&#34;是上面的示例文件的格式。

编辑:更简单的示例

代码:

{{1}}

1 个答案:

答案 0 :(得分:1)

对于许多类型,操作员已经过载 - 并且应该过载。让这些功能完成他们的工作:

template<typename Type, typename PriorityType>
std::istream& operator>>(std::istream& inStream, PriorityQueue<Type, PriorityType>& targetQueue)
{
  std::vector< std::pair<Type, PriorityType> > pairVec;
  Type data;
  PriorityType priority;

  while(inStream >> data >> priority)
    pairVec.push_back(std::make_pair(data, priority));

  targetQueue.enqueueWithPriority(pairVec);

  return inStream;
}