编程语言的共性比较

本文介绍了在Bash、C/C++、Python和QT中进行字符串操作的方法,如长度计算、查找、替换、拼接等,并涵盖了变量、数据类型的基本概念,包括各种类型的声明和使用。此外,还讨论了循环、分支结构以及函数的定义与调用。
摘要由CSDN通过智能技术生成

字符串处理

处理BashCC++PythonQT
字符串长度${#str}strlen()size()len()
访问字符${string:start:1}a[12]a[12],at()a[12]
查找子串位置${string/substring}strstr()find()s.find()
切片${string:start:length}substr()
替换${string/substring/replacement}replace()s.replace()
删除${string//substring/}a[12]=‘’erase()
拼接${string1}${string2}strcat()+=s.join()
转成为大写${string^^}toupper()toupper()s.upper()
转换为小写${string,}tolower()tolower()s.lower()
转换为整数atoi()stoi()
转换为字符sprintf()to_string()str()
判断为空if [ “$str” == “” ]if(a==“”)if(a==“”)
相等性判断if [ “$str1” == “$str2” ]strcmp()if(a==b)
包含子串判断if [[ $str == *hello* ]]strstr()find()s.find(substring)
分割字符串cut -F: {print “$2”}strtok()getline(ss,s,‘;’)s.split()
去除首位空白s.strip()
二进制转十进制
十六进制转十进制
十进制转二进制
十进制转十六进制

变量、数据类型

Bash:

# 字符串
a="hello"

# 布尔
a=true

# 整形

C/C++:
变量类型:

# 读写
int a=3;

# 只读
const a=3;

# 静态
static a=3;

基本数据类型:

// 整形: int、short、long、unsigned int、unsigned short、unsigned long
int a=0; 
int a=0x80; 
int a=0b10010;
int a=5ull;
int a=5e3; //科学计数法,5乘以10的3次幂

// 字符型: char
char data='a';

// 浮点型: float、double
float a=1.0;

// 布尔类型: bool
bool a=true

// 空类型: void

// 指针类型:(int *)等

自定义数据类型:

// 结构体: struct, class
struct Name{
  int a;
  int b;
};

// 枚举: enum
enum{red,yellow,green};

// 共用体: union
union Name{
    int a;
    char b;
};

Python
基本数据类型:

# 整形
a=0
a=0x80
a=0b10010
a=2.5e6
a=1_000

// 字符型
data='a';
data="abc"

// 浮点型
a=1.0;

// 布尔类型: bool
bool a=True

容器

Bash:

# 字符数组
a="1 2 3"

C:

// 数组
TYPE a[num];

C++:

// 动态数组
vector<TYPE> a;

// 双向链表
list<TYPE> a;

// 散列表,二叉平衡树
map<TYPE1,TYPE2> a;

// 字典,二叉平衡树
set<TYPE> a;

// 哈希散列表
unordered_map<TYPE> a;

// 哈希字典
unordered_set<TYPE> a;

// 队列
queue<TYPE> a;

// 栈
stack<TYPE> a;

Python:

# 列表,list
data=[1,'a']

# 元祖
data=(1,2,3)

# 字典
data={'a':1,'b':2}

# 集合
a=set()

循环、分支

Bash:

# if分支
if <conf> 
then
    command1
else
    command2
fi

# case分支
case <variable> in
    pattern1)
        command1
        ;;
    pattern2)
        command2
        ;;
    *)
        command3
        ;;
esac

# for循环
for variable in values
do
    command1
    command2
    ...
done

# while循环
while condition
do
    command1
    command2
    ...
done

C/C++/QT:

// if分支
if (condition)
{
    command1;
    command2;
    ...
}
else
{
    command3;
    command4;
    ...
}

// switch-case分支
switch (variable)
{
    case value1:
        command1;
        break;
    case value2:
        command2;
        break;
    ...
    default:
        command3;
        break;
}

// for循环
for (initialization; condition; increment)
{
    command1;
    command2;
    ...
}

// while循环
while (condition)
{
    command1;
    command2;
    ...
}

python:

# if分支
if condition:
    command1
    command2
    ...
else:
    command3
    command4
    ...

# for循环
for variable in sequence:
    command1
    command2
    ...

# while循环
while condition:
    command1
    command2
    ...

函数

Bash:

# 函数定义
function_name() {
    commands
    return value
}

# 函数调用
function_name arg1 arg2 ...

C/C++/QT:

// 函数定义
int hello(int a,int b) {
    /* 函数体 */
    return value;
}

// 函数调用
int result = hello(1, 2);

Python:

# 函数定义
def add(a, b):
    """将两个数字相加并返回结果"""
    result = a + b
    return result

# 函数调用
add(a,b)

Bash:
无。

C++:

class Name{
private:
    int a;
public:
    Name();
    int hello(int,int);
};

Python:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

IO

Bash:

# 输入
read <变量>

# 输出
echo <内容>

C:

// 输入
scanf()
fread()

// 输出
printf()
fwrite()

C++:

// 输入
cin>>
istream

// 输出
cout<<
ostream

Python:

# 输入
input()
read()

# 输出
print()
write()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多弗朗强哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值