可能重复:
main()在C/C++中应该返回什么?
#include<stdio.h>
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面给出的代码片段中,main返回的返回0在哪里?或者换句话说,哪个函数在开始时称为主函数.
我正在使用qsort库函数对结构元素数组进行排序,而在Internet上搜索时我找到了一个资源:INFO:使用C qsort()函数 @support.microsoft 对结构进行排序.
我知道qsort函数需要通用指针进行类型转换.
但是我无法得到这一行:
typedef int (*compfn) (const void*, const void*);
Run Code Online (Sandbox Code Playgroud)
已宣布的内容及其随后的致电:
qsort((void *) &array, // Beginning address of array
10, // Number of elements in array
sizeof(struct animal), // Size of each element
(compfn)compare // Pointer to compare function
);
Run Code Online (Sandbox Code Playgroud)
typedef表现,我的意思是到底有没有我们typedeffed int (*compfn)或int (compfn)? (*compfn)吗? 我有一个 Python 类,有一些变量。该类的定义如下:
class student:
def __init__(self,name,rollno,DOB,branch):
self.name=name
self.rollno=rollno
self.DOB=DOB
self.branch=branch
self.books=[]
self.fines=[]
Run Code Online (Sandbox Code Playgroud)
我正在为学生添加新属性,并且还需要存储相应的值(以供将来使用)。这是使用 setattr 方法完成的,并且工作正常。
代码片段:
setattr(student,"date_of_join",date())
Run Code Online (Sandbox Code Playgroud)
现在我正在解决这个问题,如果用户添加一个新属性(例如“ date_of_join ”),那么我会更新一个列表(studattr),最初包含["name","rollno",DOB","branch", "books","fines"] 更新属性列表。这意味着更新的列表现在还将附加“date_of_join”。
现在,如果我想访问学生实例的属性列表,那么我该怎么做呢?(由于记录是动态更新的,并且让我们假设我必须访问x.date_of_join,那么我如何连接这两个字符串?有没有类似于python的os.path.join,或者更确切地说linux的系统调用,(合并两个字符串)字符串)?)
问题:
for attribute in studattr:
print attribute,x.attribute
{ This throws an Exception since the instance x has no variable or method named "attribute")
Run Code Online (Sandbox Code Playgroud)
PS:我尝试使用inspect,inspect.getmembers(x)以及dirs(x),vars(x)(在stackoverflow上提到),但它只给了我主类主体中的变量/方法列表,而不是在在里面。
我正在编写一个 Union find 数据结构,并试图用值 parent[i]=i 初始化父向量,在 C++ 中有没有办法像这样初始化向量,即声明一个大小为 N 的向量,并且不为每个元素分配固定值,而是为每个元素分配位置相关值。(不使用任何明显的 for 循环)
This is what I was looking for:
std::vector<int> parent(Initializer);
Run Code Online (Sandbox Code Playgroud)
其中 Initializer 是某个类或函数。
为了尝试一下我的手,我写了这个:
#include <iostream>
#include <vector>
using namespace std;
class Initializer {
private:
static int i;
public:
int operator() ()
{
return i++;
}
};
int main()
{
vector<int> parent(Initializer);
cout << parent[0];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,我认为我在这里把我的概念搞得一团糟,我不明白声明的意思,或者它在做什么。
请回答这两个问题,
(1) 如何用可变初始值初始化向量。
(2)我写的代码到底是做什么的?