请建议一些算法来查找树中的节点,该节点与所有节点之间的最远节点的距离最小.
它不是图表,也没有加权.
#include<stdio.h>
int main()
{
printf("He %c llo",65);
}
Run Code Online (Sandbox Code Playgroud)
输出:他A爱
#include<stdio.h>
int main()
{
printf("He %c llo",13);
}
Run Code Online (Sandbox Code Playgroud)
输出:llo.它不打印他.
我可以理解,65是A的ascii值,因此A在第一种情况下打印,但为什么llo在第二种情况下.
谢谢
我正在寻找一种语法来从辅助存储设备而不是从默认堆分配内存.
我该如何实现它?malloc()默认情况下使用将它从堆中取出...当然必须有另一种方法!
有什么方法可以找到从堆栈中实例化了多少对象以及Heap中有多少个对象.我不希望对Stack中的对象进行作用域限制.
如果我在构造函数和析构函数中使用静态计数器,它将在两种情况下被调用(来自堆栈和堆的对象).一种方法是利用这个想法,堆栈中的对象是作用域的,当析构函数超出范围时调用它,但我想要一个更好的解决方案,以便知道任何时间点的对象数量.
我正在尝试计算一棵树的高度.我对下面的代码不感兴趣.
#include<iostream.h>
struct tree
{
int data;
struct tree * left;
struct tree * right;
};
typedef struct tree tree;
class Tree
{
private:
int n;
int data;
int l,r;
public:
tree * Root;
Tree(int x)
{
n=x;
l=0;
r=0;
Root=NULL;
}
void create();
int height(tree * Height);
};
void Tree::create()
{
//Creting the tree structure
}
int Tree::height(tree * Height)
{
if(Height->left==NULL && Height->right==NULL)
{return 0;
}
else
{
l=height(Height->left);
r=height(Height->right);
if (l>r)
{l=l+1;
return l;
}
else
{ …Run Code Online (Sandbox Code Playgroud) #include <iostream.h>
using namespace std;
class A {
public:
virtual char* func()=0;
};
class B :public A {
public:
void show() {
char * a;
a = func();
cout << "The First Character of string is " << *a;
}
char * func();
};
char* B::func() {
cout << "In B" << endl;
char x[] = "String";
return x;
}
int main() {
B b;
b.show();
}
Run Code Online (Sandbox Code Playgroud)
这个问题是我正在修复本地varibale指针/引用.目前它是char x[]="String",但是当我使用指针时char *x="String",结果是"S" 但当数组引用时输出为(i)
#include<iostream>
#include<memory>
#include<stdio>
using namespace std;
class YourClass
{
int y;
public:
YourClass(int x) {
y= x;
}
};
class MyClass
{
auto_ptr<YourClass> p;
public:
MyClass() //:p(new YourClass(10))
{
p= (auto_ptr<YourClass>)new YourClass(10);
}
MyClass( const MyClass &) : p(new YourClass(10)) {}
void show() {
//cout<<'\n'<<p; //Was not working hence commented
printf("%p\n",p);
}
};
int main() {
MyClass a;
a.show();
MyClass b=a;
cout<<'\n'<<"After copying";
a.show();//If I remove copy constructor from class this becomes NULL(the value of auto_ptr becomes NULL but if …Run Code Online (Sandbox Code Playgroud) 为什么我们不能在构造函数括号({})中初始化Const和引用varibales,并且总是通过初始化列表完成
谢谢,Sandeep