C++ Part 1 | From C to C++

1. From C to C++

1. Quote

Before talking about quoting, let's talk about the C language that everyone is very familiar with, and then transfer from C to C++. This will not only consolidate C knowledge, but also make it easy to understand C++.

Example 1, value exchange

swap the values ​​of a and b

There are many ways to implement swapping in C language:

  1. Introduce another variable c as an intermediary, first assign the value of a to c (c=a), then assign the value of b to a (a=b), and finally retrieve the value of a in c and put it in b (b =c), so as to realize the conversion.
#include <stdio.h>
int main()
{
    
    
	int a = 100;
	int b = 10;
	int c;

	printf("a = %d, b = %d\n", a, b);
	c=a;
	a=b;
	b=c;
	printf("a = %d, b = %d\n", a, b);
}

result:

@ubuntu:/mnt/hgfs/ub2/$ gcc swap1.c 
@ubuntu:/mnt/hgfs/ub2/$ ./a.out 
a = 100, b = 10
a = 10, b = 100
@ubuntu:/mnt/hgfs/ub2/$ 
  1. According to the value of addition and summation does not change before and after the exchange of two values, so it can be solved without introducing a third variable. b=a+b, a=ba (equivalent to a=a+ba and finally a gets the value of b), b=ba (equivalent to b=a+bb)
#include <stdio.h>
int main()
{
    
    
	int a = 100;
	int b = 10;

	printf("a = %d, b = %d\n", a, b);
	b=a+b;
	a=b-a;
	b=b-a;
	printf("a = %d, b = %d\n", a, b);
}

result:

@ubuntu:/mnt/hgfs/ub2/$ gcc swap2.c
@ubuntu:/mnt/hgfs/ub2/$ ./a.out 
a = 100, b = 10
a = 10, b = 100
@ubuntu:/mnt/hgfs/ub2/$
  1. Use the logical symbol XOR (^) to achieve. a^=b, b^=a, a^=b.
#include <stdio.h>
int main()
{
    
    
	int a = 100;
	int b = 10;

	printf("a = %d, b = %d\n", a, b);
	a^=b;
	b^=a;
	a^=b;
	printf("a = %d, b = %d\n", a, b);
}

result:

@ubuntu:/mnt/hgfs/ub2/$ gcc swap3.c
@ubuntu:/mnt/hgfs/ub2/$ ./a.out 
a = 100, b = 10
a = 10, b = 100
@ubuntu:/mnt/hgfs/ub2/$

In order to look more upscale, it is encapsulated into a function

Example 2. Optimized version of value exchange

#include <stdio.h>
void swap(int *p, int *q)
{
    
    
	*p ^= *q;
	*q ^= *p;
	*p ^= *q;
}

int main()
{
    
    
	int a = 100;
	int b = 10;

	printf("a = %d, b = %d\n", a, b);
	swap(&a, &b);
	printf("a = %d, b = %d\n", a, b);
}

result:

@ubuntu:/mnt/hgfs/ub2/$ gcc swap4.c
@ubuntu:/mnt/hgfs/ub2/$ ./a.out 
a = 100, b = 10
a = 10, b = 100
@ubuntu:/mnt/hgfs/ub2/$

This is a particularly classic function encapsulation, which is correct only when pointers are used. If you don't use pointers, you need to return the value, otherwise swap() is useless, and no value exchange is performed, as shown below:

#include <stdio.h>
void swap(int p, int q)
{
    
    
	p ^= q;
	q ^= p;
	p ^= q;
}

int main()
{
    
    
	int a = 100;
	int b = 10;

	printf("a = %d, b = %d\n", a, b);
	swap(a, b);
	printf("a = %d, b = %d\n", a, b);
}

result:

@ubuntu:/mnt/hgfs/ub2/$ gcc swap4.c
@ubuntu:/mnt/hgfs/ub2/$ ./a.out 
a = 100, b = 10
a = 100, b = 10
@ubuntu:/mnt/hgfs/ub2/$

There is no value returned after the swap() function exits, so a and b will not be exchanged. The interview will test this.

Example 3. Value exchange C++ version

#include <stdio.h>
int swap(int &a, int &b)
{
	a ^= b;
	b ^= a;
	a ^= b;
}
int main()
{
	int a = 100;
	int b = 10;

	printf("a = %d, b = %d\n", a, b);
	swap(a, b);
	printf("a = %d, b = %d\n", a, b);
}

Compared with the above-mentioned C language method, the expression here is more intuitive, and the idea does not need to compete with pointers and address fetching.
int swap(int &a, int &b) where int &a and int &b are references. & is the meaning of reference, not the address of C language. For example, int a=100, int &b=a, where b refers to a. Not just the transfer of values, but the addresses are consistent .

#include <stdio.h>
int main()
{
    int a =100;
    int &b = a;
    printf("a = %d, b = %d\n", a, b);
    
    printf("addr: a = %p, b = %p\n", &a, &b);
}
@ubuntu:/mnt/hgfs/ub2/$ g++ test.cpp
@ubuntu:/mnt/hgfs/ub2/$ ./a.out 
a = 100, b = 100
addr: a = 0xbfb02ae8, b = 0xbfb02ae8
@ubuntu:/mnt/hgfs/ub2/$

2. Function overloading

Example 4. Value/String Comparison

This is very simple, you can directly return the difference between two numbers to get the size comparison of the value, if you compare the size of the string, you can use the library function strcmp() (
add the header file string.h when using the string function), I believe everyone will be.

#include <stdio.h>
#include <string.h>
int intcmp(int a, int b)
{
    
    
	return a-b;
}
int scmp(const char *str1, const char *str2)
{
    
    
	return strcmp(str1, str2);
}
int main()
{
    
    
	printf("%d\n", intcmp(1, 2));
	printf("%d\n", scmp("aaaaaa", "bbbb"));
}

There is a point here that I don’t know if it has attracted everyone’s attention: numerical comparison and string comparison are both comparison functions, but they require different functions to implement. Is it possible to use a function? Yes, function overloading in C++ can solve this problem.

#include <stdio.h>
#include <string.h>
int cmp(int a, int b)
{
	return a-b;
}

int cmp(const char *str1, const char *str2)
{
	return strcmp(str1, str2);
}
int main()
{
	printf("%d\n", cmp(1, 2));
	printf("%d\n", cmp("aaaaaa", "bbbb"));
}

Overloaded functions are usually used to name a group of functions with similar functions. This reduces the number of function names and avoids the pollution of the namespace, which is of great benefit to the readability of the program.

3. Heap memory allocation (new/delete)

Example 5-1. Memory allocation in C language (malloc/free)

#include <stdio.h>
#include <malloc.h>
#include <string.h>
int main()
{
    
    
	int *intp = (int*)malloc(sizeof(int));
	*intp = 100;
	printf("*intp = %d\n", *intp);
    free(intp);
    
	char *p = (char *)malloc(10);
	strcpy(p, "hello");

	printf("p: %s\n", p);

	free(p);
}

Example 5-2, memory allocation in C++ (new/delete)

#include <stdio.h>
#include <malloc.h>
#include <string.h>
int main()
{
	int *intp = new int;//(int*)malloc(sizeof(int));
	*intp = 100;
	printf("*intp = %d\n", *intp);
	delete intp;
    
	char *p = new char[10];
	strcpy(p, "hello");
	printf("p: %s\n", p);
	delete [] p;
}

4. Set default parameters

Example 6. Functions of common debuggers

If you want to check the execution location of the program, you often use the string printing "------------" to determine the location, but occasionally you want to mark it, such as printing a date, explanation and other strings. So I want to set the commonly used "------------" as the default, and mark it separately for other occasional cases.

#include <stdio.h>

void debug(const char *ptr = "---------------")
{
	printf("%s\n", ptr);
}

int main()
{
	debug();
	debug();
	debug("hello");
	debug();
	debug();
	debug();
	debug("world");
	debug();
}

result:

@ubuntu:/mnt/hgfs/ub2$ g++ debug.cpp 
@ubuntu:/mnt/hgfs/ub2$ ./a.out 
---------------
---------------
hello
---------------
---------------
world
---------------
@ubuntu:/mnt/hgfs/ub2$ 

Guess you like

Origin blog.csdn.net/weixin_44035986/article/details/122773110