小编Jac*_*ack的帖子

SICP中`flatmap`的意义是什么?

(define (accumulate op initial sequence) 
  (if (null? sequence) 
   initial 
   (op (car sequence) 
     (accumulate op initial (cdr sequence))))) 
      
(define (flatmap proc seq) 
  (accumulate append nil (map proc seq)))
Run Code Online (Sandbox Code Playgroud)

以上是来自 SICP 的代码片段,在 Scheme 中。为什么flatmap需要这个程序?flatmap和 和有map什么区别?

scheme sicp flatmap map-function

3
推荐指数
1
解决办法
983
查看次数

无法修改数组中的对象队列

#include<iostream>
#include<deque>
struct testStr {
    std::deque<int> queue;
};

testStr arr[1] = {
    testStr()
};

testStr t = testStr();

void func() {
    testStr s = arr[0];
    s.queue.push_back(5);
    t.queue.push_back(6);
}

void func2() {
    std::cout << arr[0].queue.empty() << ' ';
    std::cout << t.queue.empty() << '\n';
}

int main() {
    func();
    func2();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

1 0
Run Code Online (Sandbox Code Playgroud)

我花了一个小时来追踪这个"错误"并且不明白为什么会出现这种行为以及如何克服它.感谢这里的任何和所有帮助.

c++ c++11

0
推荐指数
1
解决办法
42
查看次数

标签 统计

c++ ×1

c++11 ×1

flatmap ×1

map-function ×1

scheme ×1

sicp ×1