
牛客网
牛客网错题解析
yergen
Talk is cheap, show me the code!
展开
-
统计二进制中0或1的个数
#include <iostream>using namespace std;int fun(unsigned int x) { int n = 0; while (x + 1) { n++; x = x | (x + 1); } return n;}int fun2(unsigned int x) { int n = 0; while (x) { n++; x = x & (x - 1); } return n;}int main.原创 2022-05-15 11:28:32 · 437 阅读 · 0 评论 -
【错题本】函数内malloc
#include <iostream>using namespace std;void fun(int* p1, int* p2, int* t){ t = (int*)malloc(sizeof(int)); cout << "fun:t,address: " << &t << endl; *t = *p1 + *(p2++);}int main(){ int a[2] = { 1,2 }; int b[2] = { 10.原创 2022-05-15 11:12:38 · 106 阅读 · 0 评论 -
dynamic_cast和static_cast
1. 概念dynamic_cast: 该运算符用于将基类的指针或引用安全的转换成派生类的指针或引用,具有运行时类型识别(run-time type identification,RTTI)的功能。static_cast:任何明确定义的类型转换,只要不包含底层const,都可以使用static_cast.2.举例#include <stdio.h>using namespace std;struct A1{ virtual ~A1(){}};struct A2{原创 2022-05-15 10:38:31 · 352 阅读 · 0 评论