c++一些基础知识(本篇长期更新)

1.strcmp函数:extern int strcmp(const char *s1,const char *s2);

针对char类型的字符数组而言
C/C++函数,比较两个字符串
设两个字符串为str1,str2,
若str1=str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。

2. 字符串的复制:

针对string类型的字符串数组:
string s1;
string s2;
s1=s2;//完成复制

3.swap函数:

既可以交换数组中的位置,也可以交换结构体,结构体的例子如下:

#include
#include
#include//sort函数包含的头文件
using namespace std;
//定义一个学生类型的结构体
typedef struct student
{
string name; //学生姓名
int achievement; //学生成绩
} student;

//用来显示学生信息的函数
void show(student *stu,int n)
{
for(int i = 0; i < n; i++)
{
cout<<“姓名:”<<stu[i].name<<’\t’<<“成绩:”<<stu[i].achievement<<endl;
}
}
int main()
{
student stu[] = { {“张三”,99},{“李四”,87},{“王二”,100} ,{“麻子”,60}};
cout<<“交换前:”<<endl;
show(stu,4);
swap(stu[0],stu[3]);
cout<<“交换后:”<<endl;
show(stu,4);
return 0;
}`

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值