这两个关于STL内部实现的区别是什么.性能有什么不同?我想当我们在"只读明智"中遍历矢量时,我们更喜欢const_iterator,对吧?
谢谢.
template <typename T> 和之间的区别是什么template <class T>.对我来说,两者都产生了相同的结果.
例如
template <class T>
T Average(T *atArray, int nNumValues)
{
T tSum = 0;
for (int nCount=0; nCount < nNumValues; nCount++)
tSum += atArray[nCount];
tSum /= nNumValues;
return tSum;
}
Run Code Online (Sandbox Code Playgroud)
如果我把它改成它 template <typename T>就是一样的
int operator==(const AAA &rhs) const;
Run Code Online (Sandbox Code Playgroud)
这是一个运算符重载声明.为什么要放在const最后?谢谢
我们知道预订,有序和后期遍历.什么算法会重建BST?
class CBase { };
class CDerived: public CBase { };
CBase b;
CBase* pb;
CDerived d;
CDerived* pd;
pb = dynamic_cast<CBase*>(&d); // ok: derived-to-base
pd = dynamic_cast<CDerived*>(&b); // wrong: base-to-derived
Run Code Online (Sandbox Code Playgroud)
我知道"衍生的基础"演员是错误的.但它的内在原因是什么?内在的逻辑原因是什么?我想,如果没有更多的解释,很难记住这一点.谢谢!
struct leaf
{
int data;
leaf *l;
leaf *r;
};
struct leaf *p;
void tree::findparent(int n,int &found,leaf *&parent)
Run Code Online (Sandbox Code Playgroud)
这是BST的一段代码.我想问一下.为什么
leaf *&parent
Run Code Online (Sandbox Code Playgroud)
为什么我们需要"参考标记"?
父母也是一片叶子,为什么我不能用leaf* parent呢?
以下代码供您参考.谢谢!
void tree::findparent(int n,int &found,leaf *&parent)
{
leaf *q;
found=NO;
parent=NULL;
if(p==NULL)
return;
q=p;
while(q!=NULL)
{
if(q->data==n)
{
found=YES;
return;
}
if(q->data>n)
{
parent=q;
q=q->l;
}
else
{
parent=q;
q=q->r;
}
}
}
Run Code Online (Sandbox Code Playgroud) 运行此代码.你可以将"长寿和繁荣"转变为"繁荣和万岁".
#include <stdio.h>
#include <string.h>
void rev(char *l, char *r);
int main(int argc, char *argv[])
{
char buf[] = "live long and prosper";
char *end, *x, *y;
// Reverse the whole sentence first..
for(end=buf; *end; end++);
rev(buf,end-1);
// Now swap each word within sentence...
x = buf-1;
y = buf;
while(x++ < end)
{
if(*x == '\0' || *x == ' ')
{
rev(y,x-1);
y = x+1;
}
}
// Now print the final string....
printf("%s\n",buf);
return(0);
}
// Function …Run Code Online (Sandbox Code Playgroud) c++ ×6
algorithm ×3
binary-tree ×1
c ×1
const ×1
dynamic-cast ×1
iterator ×1
primes ×1
stl ×1
templates ×1