C/C++/Python基本知识总结

2023-04-04  本文已影响0人  ShowMeCoding

const关键字

#include <iostream>
int main() {
    //1-常量指针
    int tmp1 = 10;
    int tmp2 = 11;
    const int* a = &tmp1;  //const修饰指针
    //*a = 9; //错误:更改对象值
    tmp1 = 9; //正确:在原对象上修改
    a = &tmp2; //正确:指针的指向可以修改,指向别的对象

    //2-指针常量
    int tmp3 = 12;
    int tmp4 = 14;
    int* const p = &tmp3;  //const修饰指针指向的值
    *p = 9;        //正确,可以修改指针指向的对象的值
    //p = &tmp4;   //错误:不可以修改指针指向的对象
    return -1;
}

C/C++中内存泄漏以及解决方法?

C/C++中野指针的概念?

C/C++中面向对象和面向过程的区别?

C/C++中常用容器功能汇总

C/C++中指针和引用的区别

int a, b, *p, &r = a;   //正确
r = 3; //正确,等价于 a = 3
int &rr; //出错,引用必须初始化
p = &a;  //正确,p中存储a的地址,即p指向a
*p = 4;  //正确:p中存储a的地址,a对应的存储空间存入值 4
p = &b;  //正确:p可以多次赋值,p存储b的地址

C/C++中宏定义的相关知识

//define 宏名
#define WeThinkIn 6666589
//define 宏名(参数) 文本
#define R(a, b) (a/b)

C/C++中typedef关键字的相关知识

//1-为基本数据类型定义新的类型名
typedef unsigned int WeThinkIn_int;
typedef char* WeThinkIn_point;
//2-为自定义数据类型(结构体、共用体和枚举类型)定义简洁的类型名称
typedef struct target_Object{
    int x;
    int y;
} WeThinkIn_Object;

C/C++中面向对象的相关知识

C/C++中struct的内存对齐与内存占用计算?

//struct的内存占用为40bytes
#include <stdio.h>
#pragma pack(8)
int main()
{
  struct Test
  {
    int a;
    //long double大小为16bytes
    long double b;         
    char c[10];
  };
  printf("%d", sizeof(Test));
  return 0;
} 

//struct的内存占用为48bytes
#include <stdio.h>
#pragma pack(16)
int main()
{
  struct Test
  {
    int a;
    //long double大小为16bytes
    long double b;         
    char c[10];
  }
  printf("%d", sizeof(Test));
  return 0;
}

C/C++中智能指针的定义与作用?

C/C++中程序的开发流程?

C/C++中数组和链表的优缺点?

C/C++中的new和malloc有什么区别?

C/C++中结构体的区别?

C++中的结构体和类的异同

C/C++中的namespace

#include <iostream>
using namespace std;

//第一个命名空间
namespace first_space {
    void func() {
        cout << "Inside first_space" << endl;
    }
    //第二个空间
    namespace second_space {
        void func() {
            cout << "Inside second_space" << endl;
        }
    }
}

using namespace first_space::second_space;

int main()
{
    //调用第二个命名空间中的函数
    func();
    return 0;
}

Python中assert(断言)的作用?

assert True
assert False
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_3860/2576994315.py in <module>
      1 assert True
----> 2 assert False


AssertionError: 
assert 1 == 1
assert 1 != 2
assert 1 == 2
---------------------------------------------------------------------------

AssertionError                            Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_3860/2410673052.py in <module>
      1 assert 1 == 1
      2 assert 1 != 2
----> 3 assert 1 == 2


AssertionError: 

Python中互换变量有不用创建临时变量的方法吗?

a, b = 1, 2
a, b = b, a
print(a, b)
2 1

Python中的主要数据结构都有哪些?

Python中的可变对象和不可变对象?

Python中的None代表什么?

print(None == 0)
print(None == ' ')
print(None == False)
print(None == None)
False
False
False
True

Python中∗args和∗∗kwargs的区别?

def test_var_args(f_arg, *args):
    print("first normal arg: ", f_arg)
    for arg in args:
        print("another arg through *args: ", arg)
test_var_args("hello", "python", "ddd", "test")
first normal arg:  hello
another arg through *args:  python
another arg through *args:  ddd
another arg through *args:  test
def test_var_kwargs(**kwargs):
    for key, value in kwargs.items():
        print("{0} == {1}".format(key, value))
test_var_kwargs(name = "kkwww", age = 18, weight = "60kg")
name == kkwww
age == 18
weight == 60kg

Python中Numpy的broadcasting机制?

import numpy as np
a = np.array([1, 2, 3])
b = np.array([6, 6, 6])
print(a + b)

c = a + 5
print(c)

d = np.arange(3).reshape(3, 1)
print(a + d)
[7 8 9]
[6 7 8]
[[1 2 3]
 [2 3 4]
 [3 4 5]]

Python中的实例方法、静态方法和类方法三者区别?

Python中常见的切片操作

example = [1, 2, 3, 4, 5]
print(example[:3])
print(example[3:])
print(example[::-1])
print(example[2::-1])
[1, 2, 3]
[4, 5]
[5, 4, 3, 2, 1]
[3, 2, 1]

Python中如何进行异常处理?

try:
    6688 / 0
except:
    '''异常的父类,可以捕获所有的异常'''
    print("0不能被除")
else:
    '''保护不抛出异常的代码'''
    print("没有异常")
finally:
    print("最后总是要执行我")
0不能被除
最后总是要执行我

Python中remove,del以及pop之间的区别?

a = [i for i in range(10)]
a.remove(1)
print(a)
del a[0]
print(a)
a.pop(2)
print(a)
[0, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 5, 6, 7, 8, 9]

Python中迭代器的概念?

from collections import Iterable
print(isinstance("abcsde", Iterable))
print(isinstance([1, 2, 3, 4, 5, 6], Iterable))
print(isinstance(123456, Iterable))

x = [1, 2, 3]
y = iter(x)
print(type(x))
print(type(y))

print(next(y))
print(next(y))
print(next(y))
print(next(y))
True
True
False
<class 'list'>
<class 'list_iterator'>
1
2
3

---------------------------------------------------------------------------

StopIteration                             Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_3860/363111041.py in <module>
     12 print(next(y))
     13 print(next(y))
---> 14 print(next(y))


StopIteration: 

Python中生成器的相关知识

a = [x*x for x in range(10)]
print(a)
b = (x*x for x in range(10))
print(type(b))

def spam():
    yield"first"
    yield"second"
    yield"third"
for x in spam():
    print(x)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
<class 'generator'>
first
second
third

Python中装饰器的相关知识

def foo():
    print("I am foo")
def foo():
    print("foo is running")
    print("I am foo")

# 改写
import logging
def use_log(func):
    logging.warning("%s is running" % func.__name__)
    func()
def bar():
    print("I am bar")
    
#将函数作为参数传入新定义函数
use_log(bar)
WARNING:root:bar is running


I am bar
import logging 
def use_log(func):
    def wrapper(*args, **kwargs):
        logging.warning("%s is running" % func.__name__)
        return func(*args, **kwargs)
    return wrapper

def bar():
    print("I am bar")

bar = use_log(bar)
bar()
WARNING:root:bar is running


I am bar
import logging 
def use_log(func):
    def wrapper(*args, **kwargs):
        logging.warning("%s is running" % func.__name__)
        return func(*args, **kwargs)
    return wrapper

@use_log
def bar():
    print("I am bar")

@use_log
def haha():
    print("I am haha")
    
bar()
haha()
WARNING:root:bar is running
WARNING:root:haha is running


I am bar
I am haha

Python的深拷贝与浅拷贝?

# 变量赋值
a = [1, 2, 3, 4, 5]
b = a
print(id(a), id(b))
print(a == b)
1868670559168 1868670559168
True
#浅拷贝
a = [4, 3, 2]
b = list(a)
print(id(a), id(b))      #a和b的地址不同
print("------------------")
for x, y in zip(a, b):
    print(id(x), id(y))  #他们的子对象地址相同
1868672945792 1868690687168
------------------
1868592736656 1868592736656
1868592736624 1868592736624
1868592736592 1868592736592
import copy
#深拷贝
a = [[1, 2],
    [3, 4],
    [5, 6]]
b = copy.copy(a)  #浅拷贝
c = copy.deepcopy(a) #深拷贝
print(id(a), id(b), id(c))
print("---------------------")
for x, y in zip(a, b):
    print(id(x), id(y))
print("----------------------")
for x, y in zip(a, c):
    print(id(x), id(y))
1868690646400 1868670536832 1868668928256
---------------------
1868690461952 1868690461952
1868690720896 1868690720896
1868690647872 1868690647872
----------------------
1868690461952 1868670467968
1868690720896 1868690461824
1868690647872 1868690720384

Python是解释语言还是编译语言?

Python的垃圾回收机制

Python里有多线程吗?

Python中range和xrange的区别?

Python中列表和元组的区别?

Python中dict(字典)的底层结构?

常用的深度学习框架有哪些,都是哪家公司开发的?

PyTorch动态图和TensorFlow静态图的区别?

上一篇下一篇

猜你喜欢

热点阅读