无法推进与基础列表排队

时间:2016-07-28 00:27:39

标签: c++ list graph queue pass-by-reference

example from my professor之后,使用基于列表的队列来遍历图表:

data <- testdata1

dataStandard <- testdata2  # this contains the 'nice-looking/separated uniformly' time measurements

# this function takes a 'standardTime' from the dataStandard data.frame and returns the 'result' (closest to standardTime, if there exists such value in the +/- 15 min window)
result <- function(standardTime, data){
    # create search window
  low <- standardTime - minutes(15)
  high <- standardTime + minutes(15)

  # take subset of data that lies in the search window
  results <- data[low < data$datetime & data$datetime < high,]  

  # find the closest one, if it exists
  if (nrow(results) > 0) {
    result <- results$result[which.min(abs(difftime(results$datetime, standardTime)))]
  } else {
    result <- NA
  }
  return(result) 
  }


  listResults <- unlist(lapply(dataStandard$datetime, FUN = function(x) result(x, data)))

# add the results vector to the dataStandard 

dataStandard$result_value <- listResults

同样:

void breadthFirst (Point* root)
{
   queue<list<Point*> > q;
   q.push (root);
 }

error: no matching function for call to ‘std::queue<std::list<Point*> >::push(Point*&)’
    q.push (root);

我已经回顾了关于指针和引用的材料,并尝试了传递变量的不同方法。

根据文档,似乎构造函数可能不正确void breadthFirstTraversal (const Point& start) { queue<list<Point> > q; q.push (start); } error: no matching function for call to ‘std::queue<std::list<Point> >::push(const Point&)’ q.push (start);

1 个答案:

答案 0 :(得分:0)

Looking at reliable documentation:

template<
    class T,
    class Container = std::deque<T>
> class queue;

我们发现std::queue需要定义为

std::queue<type of element, type of container to base queue on> nameOfQueue;

有两种方法可以解决这个问题:

queue<Point, list> q;

指定queue Point queuePoint用于存储list的容器为 queue<list<Point>>

queue

我们的list包含PointPoint s。我现在忽略了指针,因为它们只会让人分心。

OP和他们的教师代码没有明确使用哪个变体。正如OP发现的那样,由于queue<list<Point>>不是Point,因此无法将list<Point>推送到void breadthFirst (TreeNode* root) { queue<list<TreeNode*> > q; q.push (root); // impossible. root is not list<TreeNode*> while (!q.empty()) { v = q.front (); // type of v not specified. Based on usage, assuming TreeNode * q.pop (); for (int i = 0; i < v->numChildren(); ++i) { TreeNode* w = v->child[i]; if (w != 0) q.push (w); } } }

让我们停下来看看提供给OP的例子:

void breadthFirst(std::list<std::list<std::list<...>*>*>* root)

我可以看到教练的头向后扭曲了。我想他们想要

struct TreeNode
{
    std::list <TreeNode* > children;
};

那......代表一个无限的模板递归。祝你好运。导师明智地避免摔倒这个兔子洞。

用结构提取这个混乱

std::queue<TreeNode*> q;

是正确的想法。教练只需要完成它。

现在我们可以定义

q.push(root);

#include <list>
#include <queue>
#include <iostream>

void breadthFirst(TreeNode* root)
{
    std::queue<TreeNode*> q;
    q.push(root);
    while (!q.empty())
    {
        TreeNode*v = q.front();
        q.pop();

        std::cout << v->name; // added to visually demonstrate. Do stuff here

        for (TreeNode * w: v->children) // visit all children in order
        {
            // no need to check for nulls. list doesn't need to contain empty slots
            // this was likely a left over from a previous C implementation
            q.push(w);
        }
    }
    std::cout << std::endl;
}

尝试根据对树遍历和现代C ++的预先理解来解决这个问题(并且在这样的例子中没有预先存在的知识会很糟糕),我们得到像

这样的东西。
int main()
{
    TreeNode A('A');
    TreeNode B('B');
    TreeNode C('C');
    TreeNode D('D');
    TreeNode E('E');

    A.children.push_back(&B);
    A.children.push_back(&D);
    B.children.push_back(&E);
    E.children.push_back(&C);

    breadthFirst(&A);
}

快速测试员:

 A
B D
C
E

五个节点A包含B和D. B包含C. C包含E.

ABDCE

首先,遍历访问root,然后按顺序访问所有root的孩子,然后按顺序访问孩子的孩子,所以

std::list

附注:根据您的运行方式,import UIKit class ViewController: UITableViewController { var animals = [Animal]() override func viewDidLoad() { super.viewDidLoad() self.animals = [Animal(name: "개"),Animal(name: "강아지"),Animal(name: "고양이"),Animal(name: "멍멍이"),Animal(name: "물어")] // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) var animal = Animal.self animal = animals[indexPath.row] //1 cell.textLabel?.text = animal.name //2 return cell //3 } } 可能不是最好的容器,因为现代处理器能够预测和缓存。例如,使用std :: vector,处理器总是知道哪个是要访问的下一个元素以及在哪里查找它,因此CPU可以在读取第一个项目时预加载下一个或下一个项目。有了列表,这些项目通常会被扰乱,使CPU通过内存反弹,在这里稍微读一点,从来没有真正有机会超越自己。