小编זאב*_*כהן的帖子

从另一个 C++ 中删除 1 个列表中出现的所有元素

假设我有两个列表,l1 和 l2。我想执行 l1 - l2,它返回 l1,同时删除也是 l2 元素的所有元素。

我可以想到一种简单的循环方法来做到这一点,但这确实效率很低。在 C++ 中执行此操作的有效方法是什么?

例如,如果我有 l1 = [1,2,6,8] 和 l2 = [2,8],则 l1 - l2 应该返回 [1,6]

谢谢你们

c++ list

4
推荐指数
1
解决办法
3893
查看次数

Lisp - 如何检查列表是否为点对?

如何检查lisp中的列表是否为点对?

CL-USER 20 : 3 > (dotted-pair-p (cons 1 2))
T

CL-USER 20 : 3 > (dotted-pair-p '(1 2))
NIL

CL-USER 20 : 3 > (dotted-pair-p '(1 2 3))
NIL
Run Code Online (Sandbox Code Playgroud)

我试过检查是否length=2但有错误:

CL-USER 28 : 1 > (= (length (cons 2 3)) 2)
Error: In a call to LENGTH of (2 . 3), tail 3 is not a LIST.
Run Code Online (Sandbox Code Playgroud)

lisp predicate list common-lisp

4
推荐指数
1
解决办法
3710
查看次数

Lisp - 仅当符号不是字符串时才将符号转换为字符串

有没有办法将符号转换为字符串,只要它不是lisp中的字符串?

它应该像这样工作:

(only-if-convertion'ABC)=>"ABC"

(only-if-convertion"ABC")=>"ABC"

lisp string recursion symbols

2
推荐指数
1
解决办法
2711
查看次数

如何将字符串转换为符号以用作Lisp"assoc"函数中的键?

我在Common Lisp中有这个关联列表:

(defvar base-list (list (cons 'a 0) (cons 2 'c)))
Run Code Online (Sandbox Code Playgroud)

assoc当我的论证属于类型时,我必须打电话string.

对于该对,(A . 0)我必须将"a"转换为符号,对于该对,(2 . C)我必须将"2"转换为符号.我怎样才能做到这一点?

这应该是这样的:

CL-USER 28 : 1 > (assoc (convert-string-to-symbol "a") base-list)
(A . 0)
CL-USER 28 : 1 > (assoc (convert-number-to-symbol "2") base-list)
(2 . C)
Run Code Online (Sandbox Code Playgroud)

我试过用intern但得到了NIL:

CL-USER 29 : 1 > (assoc (intern "a") base-list)
NIL
Run Code Online (Sandbox Code Playgroud)

lisp symbols common-lisp

2
推荐指数
2
解决办法
2127
查看次数

使用std :: list编译错误

我有这个简单的函数来检查值是否在列表中:

template <class T>
bool IsinList(list<T> l, T x)
{
    for(list<T>::iterator it=list.begin(); it != list.end(); it++)
    {
        if (*it == x)
            return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我在同一个.cpp文件中使用了这个函数,如下所示:

if (!IsinList (words, temp))   
    goodwords.push_back(temp);
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

'std::list' : use of class template requires template argument list
Run Code Online (Sandbox Code Playgroud)

我无法弄清问题是什么.我检查过以前的问题,但没有帮助.你能解释一下我做错了什么吗?

c++ templates

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

LISP - 无法使用可选参数调用函数

我在LISP中有这个函数,带有常规参数和可选的paremater n:

(defun lastplus (x &optional (n 0)) //default value for n is 0
    ( if (listp x) //if x is a list
        (
            (list (length x) (n)) //return list that contains length(x) and n
        )
        (n) //else return n
    )
)
Run Code Online (Sandbox Code Playgroud)

我试图在侦听器文件中使用该函数,但它给了我这个错误:

CL-USER 13 : 4 > (lastplus 2 8) 

Error: Undefined function N called with arguments ().
Run Code Online (Sandbox Code Playgroud)

我使用LispWorks 6.0.1

你知道为什么我会收到这个错误吗?

lisp function optional-parameters

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