C + + 1 | from C to C++

Posted by noisenet on Wed, 02 Feb 2022 17:21:29 +0100

1, From C to C++

1. Quote

Before talking about quotation, first talk about the familiar C language, and then transfer from C to C + +. This can not only consolidate C knowledge, but also easily understand C + +.

Example 1. Numerical exchange

Exchange the values of a and b

There are many ways to realize exchange in C language:

  1. Another variable c is introduced as an intermediary. First assign the value of a to c (c=a), then give the value of B to a (a=b), and finally get the value of a in c back to 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 fact that the addition and summation value is not changed before and after the two value exchange, it can be solved without introducing the third variable. b=a+b, a=b-a (equivalent to a=a+b-a, and finally a gets the value of b), b=b-a (equivalent to b=a+b-b)
#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 implement. 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 appear high-grade, it is encapsulated into a function

Example 2: optimized version of numerical 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/$

Here is a special classic function encapsulation, which requires pointers to be correct. If you don't use a pointer, you need to return the value. Otherwise, swap() is null and there is no value exchange, 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/$

No value is returned after the swap() function exits, so a and b will not exchange. The interview will take this.

Example 3: numerical exchange C + +

#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 C language mode, the expression here is more intuitive, and the idea does not have to compete with the pointer and address.
Int swap (int & A, int & B) here int & A and int & B are references& Is the meaning of reference, not the address of C language. For ex amp le, int a=100, int &b = a, where B refers to a. Not only the transmission of values, but also the consistency of addresses.

#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 the two numbers to get the size comparison of the number. For the size comparison of the string, you can use the strcmp() of the library function
(the header file string.h should be added when using the string function). I'm sure everyone will.

#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"));
}

Here is a point that I don't know whether it has attracted your attention: numerical comparison and string comparison are the same comparison functions, but they need different functions to implement. Can you just use a function? Yes, in C + +, function overloading 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, which reduces the number of function names and avoids the pollution of 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 ordinary debugging programs

If you want to check the execution position of the program, you often use string printing "------------" to determine the position, but occasionally you want to mark it, such as printing a date, explanation and other string. So I want to set the commonly used "-" ---------------- "as the default, and mark it separately in 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$ 

Topics: C C++ Ubuntu