假设我有一个结构.
struct node {
string info;
node link*;
}
Run Code Online (Sandbox Code Playgroud)
有什么区别
node k;
node b;
k.info = "string";
b.info = "string";
k.link = &b;
Run Code Online (Sandbox Code Playgroud)
和
node *k;
node *b;
k = new node;
b = new node;
k->info = "string";
b->info = "string";
k->link = b;
Run Code Online (Sandbox Code Playgroud)
在内存分配方面?两个示例都是正确的并创建一个正确的链表?补充:在大多数书籍中使用了第二个例子,为什么?使用第一个例子有不足之处吗?
从一开始C++用户的角度来看,我看到单例类有用的唯一原因与我想要使用静态变量时类似.你们知道什么时候最适合使用单身人士课程吗?
例如,
Number operator+(Number a, Number b) {
return Number(a.x + b.x);
}
Run Code Online (Sandbox Code Playgroud)
这会导致某种"无法访问私有成员错误".据我所知,如果我没有通过引用传递,则数字a和数字b将被复制到堆栈中并在函数体中使用.但是,我不明白为什么他们不允许访问原件的私人会员.我怎么误解对象的概念?为什么朋友和会员功能不需要通过引用传递?
我的课程说明"C++需要在一个块中使用之前声明,并且在类型之间但不在一个类型中."
这是什么意思?
int f() {
if (i)
return i;
int i = 1; //allowed?
return 0;
}
//not allowed?
int g() {
if (i)
return i;
return 0;
}
int i = 1;
Run Code Online (Sandbox Code Playgroud) 假设我有一个整数,如二进制的109,1101101.如何迭代这个数字的位,例如:[64,32,8,4,1]?在lisp中这样做的好方法是什么?我应该通过添加一个案例来修改for宏,还是应该将整数转换为位向量或列表?
假设我有一段字符串" abcd"我想要一个函数a从' " 获取char' ' abcd而另一个函数bcd从" abcd" 获得" ".
文件a.cc
int a = 0;
Run Code Online (Sandbox Code Playgroud)
文件b.cc
#include "a.cc"
Run Code Online (Sandbox Code Playgroud)
文件main.cc
#include "b.cc"
extern int a;
int main() {
}
g++ -c a.cc
g++ -c b.cc
g++ main.cc a.o b.o
error: multiple definitions of a
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
在我的课程笔记中,给出了这两个例子.显然第一个是不被允许的,是否有技术原因导致我无法在堆栈上分配?或者这是C++标准?
// Constructor may force dynamic allocation when initializating an array of objects.
Complex ac[10]; // complex array initialized to 0.0
for ( int i = 0; i < 10; i += 1 ) {
ac[i] = (Complex){ i, 2.0 } // disallowed
}
// MUST USE DYNAMIC ALLOCATION
Complex *ap[10]; // array of complex pointers
for ( int i = 0; i < 10; i += 1 ) {
ap[i] = new Complex( i, 2.0 ); // allowed
}
Run Code Online (Sandbox Code Playgroud) 我需要一些东西来表示一对序列的序列,如下所示:
[((1,2) (1,3)) ((1,2) (1,4) (1,5))].
我还需要自由地附加对的序列来制作一对对的序列,像这样append.[((1 2)(3 4)) ((5 6))] = ((1 2)(3 4)(5 6)).在C++中有什么简单易用的东西可以让我像这样操作我的数据吗?
这个警告意味着什么?我有一个我在下面使用的例子,有这个警告.我在某处做错了吗?
(defvar B_00 0)
(defvar B_000 0)
(defvar w_000 0)
(defvar w_00 0)
(defconstant white 0)
(defclass board ()
((blocker :accessor blocker :initarg :blocker :initform 0)
(friends :accessor friends :initarg :friends :initform (make-array '(2)))
(kings :accessor kings :initarg :kings :initform (make-array '(2)))
(boards :accessor boards :initarg :boards :initform (make-array '(2 7) :initial-element 0))
(enpassant :accessor enpassant :initarg :enpassant :initform -1)
(color :accessor color :initarg :color :initform WHITE)
(castling :accessor castling :initarg :castling :initform (logior B_000 B_00 W_000 W_00))
(hasCastled :accessor …Run Code Online (Sandbox Code Playgroud)