可能重复:
C++静态虚拟成员?
我们可以拥有静态虚函数吗?如果没有,那么为什么?
class X
{
public:
virtual static void fun(){} // Why we cant have static virtual function in C++?
};
Run Code Online (Sandbox Code Playgroud) 我们可以像命名空间一样对类名进行别名吗?
例如:
namespace longname{ }
namespace ln = longname;// namespace aliasing
class LONGNAME {};
class LN = LONGNAME; // how to do class name aliasing, if allowed?
Run Code Online (Sandbox Code Playgroud) 我有两个文件Sample.cpp和Main_file.cpp.Sample.cpp只有一个名称空间n1,其中包含int变量的定义x.我想x在main_file.cpp中打印这个变量.我该怎么做呢?
//Sample.cpp_BEGINS
namespace n1
{
int x=10;
}
//Sample.cpp_ENDS
//Main_FILE_BEGINS
void main()
{
print x;
}
//MAIN_FILE_ENDS
Run Code Online (Sandbox Code Playgroud)
感谢您提供任何帮助.
我不确定下面的代码有多快.如果有人知道比这更快/更优化的代码,请告诉我.
int xstrcmp(char *s1, char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2-1));
}
Run Code Online (Sandbox Code Playgroud) 如果两个数字的乘法大于ULONG_MAX限制,我需要编写一个返回true的函数。否则返回false。
我尝试了以下方法:
bool isGtThanULONG_MAX(double A, double B) {
double result = A * B;
if (result > ULONG_MAX)
return true;
else
{
//If this could be due to overflow, then again check:
double temp = result / A;
if (A != 0 && (temp != B)) {
// overflow handling
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
}
输出:下面 cout 语句中的第 1 行和第 4 行给出了 OVERFLOW(这显然是错误的输出),其余的在输出中给出了 NO-OVERFLOW(这是正确的)。为什么会失败?我错过了什么吗?请帮忙。
cout << (isGtThanULONG_MAX(10, 0.0000000000000000001) == true ? …Run Code Online (Sandbox Code Playgroud) 我有一个很长的.txt文件(LONG.txt).在那个txt文件中,我想搜索3种类型的模式,然后我想将grep结果捕获到一个新的txt文件(SHORT.txt)中.
模式:
AAAAA
BBBBB
CCCCC
注意:
当找到图案AAAAA或时BBBBB,我想只打印包含AAAAA或的那一行BBBBB.
当CCCCC找到模式时,我想打印包含CCCCC+下一行的那一行.
例:
LONG.txt:
bla bla
bla bla
bla bla
something something AAAAA something something
bla bla
bla bla
something something CCCCC something something
bla bla
bla bla
bla bla
bla bla
bla bla
bla bla
something something BBBBB something something
bla bla
bla bla
bla bla
something something AAAAA something something
bla bla
something something …Run Code Online (Sandbox Code Playgroud) 我想编写一个函数,它接受一个数组作为输入参数.并且该函数应该打印数组的所有元素.
Run Code Online (Sandbox Code Playgroud)print_array(arr) { //print all the elemnts of arr. }
我不知道该怎么做.
我想首先我们需要找出传递的数组是1-D还是2-D还是3-D等等......数组
因为,打印以下元素:
Run Code Online (Sandbox Code Playgroud)1-D array, you need only 1 for loop. 2-D array, you need only 2 for loop. 3-D array, you need only 3 for loop.
但是,我不知道你是如何判断它的1-D,2-D还是ND阵列.请帮忙.
C++表示,要为使用合成的类创建复制构造函数,编译器会递归调用所有成员对象的复制构造函数.我在下面的代码中尝试了同样的事情:
class A
{
public:
A(){cout<<"A constructor called"<<endl;}
A(const A&){cout<<"A copy constructor called"<<endl;}
};
class B
{
public:
B(){cout<<"B constructor called"<<endl;}
B(const B&){cout<<"B copy constructor called"<<endl;}
};
class C
{
A a;
B b;
public:
C(){cout<<"C constructor called"<<endl;}
C(const C&){cout<<"C copy constructor called"<<endl;}// If you comment this line, you will get output: Case 1 (see below) and if you don't comment, you will get o/p: case 2(see below)
};
void main()
{
C c;
cout<<endl;
C c2 = c;
}` …Run Code Online (Sandbox Code Playgroud) 我想知道给定的二叉树是否是二叉搜索树.
我不知道该怎么办?
我唯一知道的是BST的inorder遍历将为您提供升序输出.
那么,这是我们需要验证的唯一条件,还是我们要检查的其他内容.
如果还有其他一些必要条件要检查,它们是什么?为什么需要检查这些条件?因为,我认为,如果给定的树是BST,INORDER遍历本身可以很容易地告诉你.
例如,如果字符串是:
XYZ :: [1] [20 BB EC 45 40 C8 97 20 84 8B 10]
输出应该是:
20 BB EC 45 40 C8 97 20 84 8B 10
int main()
{
char input = "XYZ ::[1][20 BB EC 45 40 C8 97 20 84 8B 10]";
char output[500];
// what to write here so that i can get the desired output as:
// output = "20 BB EC 45 40 C8 97 20 84 8B 10"
return 0;
}
Run Code Online (Sandbox Code Playgroud)