我想定义一个像MACRO这样的函数.即
#define foo(x)\
#if x>32\
x\
#else\
(2*x)\
#endif
Run Code Online (Sandbox Code Playgroud)
那是,
if x>32, then foo(x) present x
else, foo(x) present (2*x)
Run Code Online (Sandbox Code Playgroud)
但我的海湾合作委员会抱怨:
int a = foo(31);
Run Code Online (Sandbox Code Playgroud)
我认为C预处理器应该正确处理.因为在编译时,它知道x=33.它可以代替foo(33)用(2*33)
说我刚刚实施了一些Python类,并想重载说" - "操作,但不记得,如果我需要使用__subtract__,__minus__或事实上正确的答案__sub__.有没有通过翻译快速找到这个的方法?我尝试过简单的事情,help(-)但没有成功.
有大量的在线资源可以提供可用运营商的最终列表,但我正在寻找一种快速离线方法.
对于普通操作员,可以快速记住它们,但是一些较少使用的操作员通常需要查找.
我想使用C中的regex.h库从表达式中提取子字符串.这是代码
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
regex_t preg;
char *string = "Random_ddName:cateof:Name_Random";
char *pattern = ".*Name:\\(.*\\):Name.*";
int rc;
size_t nmatch = 1;
regmatch_t pmatch[1];
if (0 != (rc = regcomp(&preg, pattern, 0))) {
printf("regcomp() failed, returning nonzero (%d)\n", rc);
exit(EXIT_FAILURE);
}
if (0 != (rc = regexec(&preg, string, nmatch, pmatch, 0))) {
printf("Failed to match '%s' with '%s',returning %d.\n",
string, pattern, rc);
}
else {
printf("With the whole expression, "
"a matched substring \"%.*s\" is found …Run Code Online (Sandbox Code Playgroud) 从我读到的,std::vector是与需要连续内存字节数组的c函数接口时使用的适当结构.但是我想知道如何在某些情况下确定阵列的大小
我写了一个小样本程序来说明我的意思.
int main(int argc, char *argv[])
{
std::vector<unsigned char>v;
unsigned char p[1024];
sprintf((char*)&p[0], "%10d", 10);
cout << "Size: " << v.size() << " Length: " << v.capacity() << endl;
v.reserve(30);
cout << "Size: " << v.size() << " Length: " << v.capacity() << endl;
memcpy(&v[0], &p[0], 20);
cout << "Size: " << v.size() << " Length: " << v.capacity() << endl;
v.reserve(50);
cout << "Size: " << v.size() << " Length: " << v.capacity() << endl;
v.reserve(0); …Run Code Online (Sandbox Code Playgroud)