小编Luv*_*Luv的帖子

执行'mv A B':'inode'会被更改吗?

如果我们执行一个命令:

mv A B
Run Code Online (Sandbox Code Playgroud)

那么文件A的inode中的字段会发生什么?它会改变吗?

我不认为它应该只是通过更改文件的名称来改变,但我不确定.

unix inode mv

5
推荐指数
1
解决办法
5862
查看次数

什么是转换构造函数

class Complex
{
private:
double real;
double imag;

public:
// Default constructor
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// A method to compare to Complex numbers
bool operator == (Complex rhs) {
   return (real == rhs.real && imag == rhs.imag)? true : false;
}
};

int main()
{
// a Complex object
Complex com1(3.0, 0.0);

if (com1 == 3.0)
   cout << "Same";
else
   cout << "Not Same";
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:相同

为什么这段代码输出为Same,转换构造函数如何在这里工作,请解释一下,非常感谢提前

c++ constructor

4
推荐指数
1
解决办法
2691
查看次数

找到第n个count-and-say序列元素

Given a problem, the count-and-say sequence is the sequence of integers beginning as     follows:
1, 11, 21, 1211, 111221, ...
Run Code Online (Sandbox Code Playgroud)

读出1作为"1"或11读出11作为"两个1"或21读出21作为"一个2,然后一个1"或1211.给定整数n,产生第n个序列.

我通过扫描每个字符串并相应地生成下一个字符串来制定解决方案需要时间为O(nm)其中m是最大字符串的长度,n是给定的数字

这是代码

void countnsay(char str[][1000],int n)
{
int i=1,k;
int j=0;
while(i<=(n-1))
{
//scan str[i-1]
j=0;
k=0;        //for building the new string array 
while(j<strlen(str[i-1]) )
    {
    char c=str[i-1][j];
    int cnt=1;

    while(c==str[i-1][++j])
    cnt++;

    str[i][k++]=cnt+48;
    str[i][k++]=c;

    }
str[i][k]='\0';
i++;

}
printf("%s",str[n-1]);  
}




int main()
{
int n=5;
char str[1000][1000];
strcpy(str[0],"1");
countnsay(str,n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的解决方案来解决这个问题?请提供一些建议/提示.Thanx提前

c string algorithm

3
推荐指数
1
解决办法
3343
查看次数

找到数字N之和的所有唯一组合

Print all combinations of a number N, as a sum of positive integers?
They should be unique
Run Code Online (Sandbox Code Playgroud)

3 =
1 2

1 1 1
Run Code Online (Sandbox Code Playgroud)

.

4=
3 1

2 2

1 1 2

1 1 1 1
Run Code Online (Sandbox Code Playgroud)

我已经使用回溯为此做了一个解决方案,但问题是它也提供重复,例如3

我正进入(状态

1 1 2

2 1 1
Run Code Online (Sandbox Code Playgroud)

如何才能获得独特的组合?

许多人提前感谢

algorithm backtracking

3
推荐指数
1
解决办法
2730
查看次数

更改外部变量的值

我们在File1.c中

int arr[10];
Run Code Online (Sandbox Code Playgroud)

在File2.c中

extern int *arr;

int main()

{
   arr[0]=10;
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

这有什么问题可以解决?为什么?

c extern

3
推荐指数
1
解决办法
1501
查看次数

C中的变量定义忽略

码:

int main()  
{
  int a=1;
  switch(a)
  {
    int b=20;

    case 1:
    printf("b is %d\n",b);
    break;

    default:
    printf("b is %d\n",b);
    break;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:当b的声明发生在这里时,它为b打印一些垃圾值为什么b在这里没有用20初始化?

c variables

2
推荐指数
1
解决办法
336
查看次数

使用1 malloc调用为2d矩阵分配内存

We can allocate memory for 2d matrix using 1 malloc call as
int (*a)[5];
int i,j;
Run Code Online (Sandbox Code Playgroud)

a = malloc(sizeof(int*)*5); //分配5个指针,每个指针指向5个整数的数组

我们怎样才能释放成功分配的内存?使用free(a)会产生运行时错误

使用for(i = 0; i <5; i ++)free(a [i]);

免费的(a);

这也会产生运行时错误

memory malloc free runtime

2
推荐指数
1
解决办法
1746
查看次数

在C中实现Realloc

int getmin(int a, int b)
{
    return a<b?a:b;
}


void *reallocation(void *ptr, size_t size) //size_t in bytes
{

    void *newptr;


    int msize;
    msize = getsize(ptr);

    msize = getmin(msize, size);

        printf("msize = %d", msize);

    newptr = malloc(size);
    newptr = memcpy(newptr, ptr, msize);
    free(ptr);


    return newptr;

}
Run Code Online (Sandbox Code Playgroud)

我已经实现了自己的realloc,并且为了使用malloc获得分配的内存的大小(但是我知道在c中没有任何方法).

我的重新分配功能在我的系统上正常工作我们如何获得malloc()分配的内存大小.

如果先前分配的内存大小大于新的所需内存,我们也可以进行内部重新分配吗?

c malloc free realloc

2
推荐指数
1
解决办法
1万
查看次数

什么时候调用构造函数?

假设我们有一个C++类

class X {
    int i;
public:
    X(int y): i(y) {}
};

X r(10);

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我们有一个全局对象r.我的问题是,在编译时或运行时调用全局或静态对象的构造函数?因为程序的执行从主函数开始.在此之前,所有全局对象都应该初始化.

c++ constructor global

2
推荐指数
1
解决办法
677
查看次数

编译器何时在C++中创建复制构造函数?

例如,在下面的代码中:

class HowMany {
    static int objectCount;
    public:
        HowMany() { 
            objectCount++; 
        }
        static void print(const string& msg = "") {
             if(msg.size() != 0) 
                 cout << msg << ": ";

             cout << "objectCount = " << objectCount << endl;
        }
        ~HowMany() {
            objectCount--;
            print("~HowMany()");
        }
};

int HowMany::objectCount = 0;

// Pass and return BY VALUE:
HowMany f(HowMany x) {
    x.print("x argument inside f()");
    return x;
}

int main() {
    HowMany h;
    HowMany::print("after construction of h");
    HowMany h2 = f(h);
    HowMany::print("after …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor

2
推荐指数
1
解决办法
1347
查看次数