一些思考
非零即为真与返回值
链接 出错的情况很多,可以表示非零表示,0是很唯一的一个数,成功也是只有一种情况,所以和0关联起来了。
返回值用int,那就是0表示成功 返回值用bool,那就是true表示成功
// true 1
// false 0
// 返回值0一般表示true,other表示false
void test() {
if (strcmp("aaa", "bbb")) {
printf("same\n");
} else {
printf("not same\n");
}
if (strcmp("aaa", "bbb") == 0) {
printf("same\n");
} else {
printf("not same\n");
}
}
防御式编程与契约式编程
这种思想可以用来优化这个代码。 下一节的do while0也可以。
// 如何防止出现
void fun1(int a, int b, int c) {
if (a > 0) {
if (b > 0) {
if (c > 0) {
printf("do something\n");
}
}
}
}
// 可以这么写
void fun2(int a, int b, int c) {
if (a <= 0) {
return;
}
if (b <= 0) {
return;
}
if (c <= 0) {
return;
}
printf("do something\n");
}
推箱子的思考
小时候不会玩,现在依旧不会玩,推箱子的本质是什么
do while(0)
可以代替goto
void fun1(int a, int b, int c) {
char *mem = (char *)malloc(100);
if (a > 0) {
if (b > 0) {
if (c > 0) {
mem[0] = 10;
printf("do something\n");
}
}
}
free(mem);
}
void fun2(int a, int b, int c) {
char *mem = (char *)malloc(100);
int ret = 0;
do {
if (a <= 0) {
ret = -1;
break;
}
if (b <= 0) {
ret = -1;
break;
}
if (c <= 0) {
ret = -1;
break;
}
} while(0);
if (ret == 0) {
mem[0] = 10;
printf("do something\n");
free(mem);
}
}
字符创匹配的感受
思维方式的改变
int find_sub_str(const char *str, const char *substr) {
for (int i = 0; str[i] != '\0'; i++) {
int j = 0;
for (j = 0; substr[j] != '\0'; j++) {
if (str[i+j] != substr[j]) {
break;
}
}
if (substr[j] == '\0') {
return i;
}
}
return -1;
}
int find_sub_str_2(const char *str, const char *substr) {
int i = 0;
int j = 0;
while (str[i] != '\0' && substr[j] != '\0') {
if (str[i] == substr[j]) {
i++;
j++;
} else {
i = i - j + 1;
j = 0;
}
if (substr[j] == '\0') {
return i - j;
}
// printf("i = %d, j = %d\n", i, j);
}
return -1;
}
yolo安装过程的思考
- pkg-config,linux的这套机制懂了,编译出了问题才知道怎么解释
- OpenCV mat和IplImage的不兼容,OpenCV1.x、2.x与OpenCV3.x以及以上
下列代码为什么会报错
namespace NAMEA {
class A {
public:
virtual ~A();
virtual int Init(const char* config_file) {
return 0;
}
};
}
namespace NAMEB {
class NAMEA : public A {
public:
virtual ~NAMEA() {}
int Init(const char* config_file) override {
return 0;
}
};
}
/*
$ g++ test.cpp
test.cpp:190:7: error: ‘int NAMEB::NAMEA::Init(const char*)’ marked ‘override’, but does not override
int Init(const char* config_file) override {
^~~~
test.cpp: In function ‘void test_ptr(char**)’:
test.cpp:208:41: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char*’ [-Wformat=]
printf("in test_ptr ptr = 0x%x\n", *ptr);
~~~~^
test.cpp: In function ‘int main()’:
test.cpp:330:36: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char*’ [-Wformat=]
printf("in main ptr = 0x%x\n", ptr);
*/
左闭右开原则
int array[10] = { 0 };
for (int i = 0; i < 10; i++) {
array[i] = 100;
}
五子棋的设计
走一步分析一步与全局分析
递归的应用
打印字符串与求和
#include <iostream>
#include <cstring>
int sum(int *arr, int n) {
//return n == 0 ? arr[0] : arr[n] + sum(arr, n-1);
if (n == 0) {
return arr[0];
}
return arr[n] + sum(arr, n-1);
}
int print_str_reverse(const char *str, int n) {
if (n == 0) {
printf("%c\n", str[0]);
return 0;
}
printf("%c", str[n]);
return print_str_reverse(str, n - 1);
}
int print_str(const char *str, int n = 0) {
if (str[n] == '\0') {
printf("\n");
return 0;
}
printf("%c", str[n]);
return print_str(str, n + 1);
}
int main()
{
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int ret = sum(arr, 9);
std::cout << "ret = " << ret << std::endl;
print_str("0123456789");
print_str_reverse("0123456789", strlen("0123456789") - 1);
return 0;
}
#include <iostream>
class ISubject {
public:
virtual void Request() = 0;
};
class RealSubject: public ISubject {
public:
virtual void Request() {
std::cout << "RealSubject::Request" << std::endl;
}
};
class ProxySubject: public ISubject {
public:
ProxySubject(ISubject *subject): subject_(subject) {}
virtual void Request() {
std::cout << "ProxySubject::Request" << std::endl;
subject_->Request();
}
private:
ISubject *subject_;
};
class ProxySubject2: public ISubject {
public:
ProxySubject2(): subject_(nullptr) {}
virtual void Request() {
if (!subject_) {
subject_ = new RealSubject();
}
std::cout << "ProxySubject2::Request" << std::endl;
subject_->Request();
}
private:
ISubject *subject_;
};
int main() {
ISubject *subject = new RealSubject();
ProxySubject *proxy_subject = new ProxySubject(subject);
proxy_subject->Request();
ProxySubject2 *proxy_subject2 = new ProxySubject2();
proxy_subject2->Request();
delete subject;
delete proxy_subject;
return 0;
}