我想将函数存储在一个列表中,然后在程序中从该列表调用这些函数。
这对我有用,但是,我不知道它是否正确:
#example functions, my functions would actually get user input etc.
def func1():
return 2
def func2():
return 3
options = [ func1, func2 ]
#call functions
options[0]()
Run Code Online (Sandbox Code Playgroud)
这是将函数存储在列表中并调用它的正确方法吗?或者有没有其他更好的方法来做到这一点?
对不起,这可能是简单而愚蠢的问题,但我无法在任何地方找到它.
我只是不知道如何获得std :: vector的字节大小.
std::vector<int>MyVector;
/* This will print 24 on my system*/
std::cout << "Size of my vector:\t" << sizeof(MyVector) << std::endl;
for(int i = 0; i < 1000; i++)
MyVector.push_back(i);
/* This will still print 24...*/
std::cout << "Size of my vector:\t" << sizeof(MyVector) << std::endl;
Run Code Online (Sandbox Code Playgroud)
那么我如何获得向量的大小?!也许将24(矢量大小)乘以项目数?
我的一些mp3文件似乎有非标准类型.当我遍历它们(我必须在我的程序中执行)时,我得到了大量的警告,如下所示:
eyed3.id3:WARNING: Non standard genre name: Rock - Punk/Pop-Punk , Rock - Alternative Rock
Run Code Online (Sandbox Code Playgroud)
如何防止eyed3打印出来?
编辑:我加载文件时收到此警告,例如:
mp3_file = eyed3.load( "path to file" ) #I get a warning when I open the 'wrong' file
Run Code Online (Sandbox Code Playgroud) 使用C++和Qt创建GUI时,您可以创建一个标签,例如:
QLabel* label = new QLabel("Hey you!", centralWidgetParent);
Run Code Online (Sandbox Code Playgroud)
这会在堆上创建对象并保持在那里,直到我手动删除它或者父进程被销毁.我现在的问题是为什么我需要一个指针呢?为什么不在堆栈上创建它?
//Create a member variable of Class MainWindow
QLabel label;
//Set parent to show it and give a text so the user can see it
QWidget* centralWidget = new QWidget(this); //Needed to add widgets to the window
this->setCentralWidget( centralWidget );
label.setParent(centralWidget);
label.setText( "Haha" );
Run Code Online (Sandbox Code Playgroud)
这工作正常,我可以看到标签,它没有消失.
我们在C++中使用指针让一些东西活得更长,这样我们就可以在各种范围内使用一个对象.但是当我创建一个成员变量时,它会不会停留直到对象被销毁?
编辑:也许我没有说清楚.这是MainWindow类:
class MainWindow : public QMainWindow
{
Q_OBJECT
QLabel label; //First introduced here...
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
//Constructor
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{ …
Run Code Online (Sandbox Code Playgroud) 这是MainWindow
我调用的类,并使用该函数show()
使其对用户可见.
class MainWindow : public QMainWindow
{
Q_OBJECT
QWidget *centralWidget;
QGridLayout* gridLayout;
QGridLayout* infoBoxLayout;
QHBoxLayout* buttonGroup;
QHBoxLayout* subCategoryLayout;
//... more widgets
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
void setupUi();
void setupConnections();
private slots:
void add();
void edit();
void remove();
void find();
void clearAll();
void screenshotDesktop();
void screenshotApp();
void currentSubCategoryChanged( const QString& );
void curretCategoryChanged( const int );
void keyPressEvent( QKeyEvent * );
};
Run Code Online (Sandbox Code Playgroud)
我为每个小部件(宏之后的那些指针Q_OBJECT
)创建了一个新的对象new
.但是,我没有在程序中的任何地方删除它们.这会导致Qt内存泄漏吗?或者Qt中的某些东西在销毁类时会自动删除它们吗?
I have read in many books & tutorials that I should avoid macros in c++. Fine, but why? I don't get it. They are very useful and often used in C.
Could someone explain (very) detailed, why I should avoid them in C++?
我在codecademy上学习python,我目前的任务是:
编写一个函数shut_down,它接受一个参数(你可以使用你喜欢的任何东西;在这种情况下,我们使用s作为字符串).当shut_down函数获得"是","是"或"是"作为参数时,应该返回"关闭...",并且"关闭已中止!" 当它变为"否", "否"或"否"时.
如果它得到的不是那些输入,该函数应该返回"抱歉,我不明白你."
看起来很容易,但不知怎的,我仍然无法做到这一点.
我用来测试函数的代码:
def shut_down(s):
if s == "Yes" or s == "yes" or s == "YES":
return "Shutting down..."
elif s == "No" or "no" or "NO":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
i = input("Do you want to shutdown?")
print(i) #was to test the input
print(shut_down(i)) #never returns "Sorry, I didn't understand you"
Run Code Online (Sandbox Code Playgroud)
它适用于no和yes',但不知何故,如果我在任何之前放置一个空格,或者即使我只输入"a",它也会打印出"Shutdown aborted!" …
我想学习如何在没有设计师的情况下手工制作gui.我尝试为我添加一个布局,MainWindow
但在运行时说
QWidget :: setLayout:试图在MainWindow上设置QLayout"",它已经有了一个布局
这是我的代码:
//Header
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QHBoxLayout *layout;
};
//Constructor in my *.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
layout = new QHBoxLayout;
this->setLayout(layout);
}
//The usual main function
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
怎么了?我做了我的书所说的.我甚至在互联网上查找了一些代码很难找到的代码,它仍然是相同的.我只是无法在窗口中添加布局.
基本上我的问题是单个地址需要/有多少字节?
我的意思是 achar
在我的平台上占用 1 个字节并有 1 个地址。但 anint
需要 4 个字节。这需要多少个地址int
?它仍然只有 1 个地址还是有 4 个地址?
例如 :
char c = 'A'; //Address at 0xdeadbeee
int i = 45846; //Address at 0xdeadbeef
int* iPtr = &i;
iPtr++; //Address at 0xdeadbef3 now
Run Code Online (Sandbox Code Playgroud)
0xdeadbeef
和之间的地址会发生什么情况0xdeadbef3
?它们都是预留的吗i
?当我指向0xdeadbeee
(应该是一个地址|字节或下面的任何内容i
)并更改它的值时, i 会发生什么?
编辑:对于那些仍然会回答的人,我不想知道整数有多大。我想知道当占用 4 个字节的内存时它是否也有 4 个地址,以及当更改这些地址之一的值时会发生什么(如果它有 4 个地址)。
我希望现在更清楚了。
我的实现strcat(char*, const char*)
似乎有效,但随后会导致核心转储。
strcat()
执行:
char* strcat(char* dest, const char* src)
{
char* tmp = dest;
while(*tmp) ++tmp ;
while( (*tmp++ = *src++ ) != '\0') ;
return (dest);
}
Run Code Online (Sandbox Code Playgroud)
代码在int main()
那里我打电话的strcat():
char arr3[] = "Mr. ";
char arr4[] = "Smith";
printf("Hello %s!", strcat(arr3, arr4));
Run Code Online (Sandbox Code Playgroud)
它实际上连接了两个字符串并将其打印出来,但仍然导致核心转储。
输出:你好,史密斯先生!中止(核心转储)
我做错了什么?