c++基本语法

1.读入数据

cpp 复制代码
#include<iostream>
#include<string>
using namespace std;
int main(){
    string s;
    //读入一行
    getline(cin,s);
    //读入数值
    int n;
    cin>>n;
    //输出 这里会把n和空格还有s的值拼接起来都输出,endl是换行等价于\n
    cout<<n<<" "<<s<<endl;
    return 0;
}

2.排序算法(直接记数组的版本可能更好一点,但是需要知道数组的长度)

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int arr[100]={3,2,1};
    int cnt=3;//数组的长度
    sort(arr,arr+cnt);
    for(int i=0;i<cnt;i++) {
        cout<<arr[i]<<" ";
    }
    return 0;
}

3.常见的字符串处理函数

c和c++中字符串有什么区别?

1.c中的字符串是数组,在字符数组最后添加了一个'\0'而c++中的字符串更加健全,更加好用,使用上有以下区别:

cpp 复制代码
string s;//c++
char s[25];//c

6-12

stlen(si) 传入一个字符数组可以返回他的长度

cpp 复制代码
int max_len(char *s[], int n) {
    int res = strlen(s[0]);
    for(int i=1;i<n;i++){
        int current = strlen(s[i]);
        if(current>res){
            res = current;
        }
    }
    return res;
}

6-15

strcpy(s,t) 将t的值赋值给s

cpp 复制代码
#include<string.h>
#include<stdlib.h>
void strmcpy( char *t, int m, char *s ){
    m--;  
    while(m--){
          t++;
      }
    strcpy(s,t);
}

6-14:

在字符串s里找ch1字符,找到返回指向ch1的指针,否则返回NULL

begin = strchr(s, ch1);

相关推荐
To_OC4 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
金銀銅鐵8 小时前
[Python] 扩展欧几里得算法
python·数学·算法
狼爷8 小时前
吃透 Java Function 接口,搞定 99% 的 Stream 场景
java·函数式编程
To_OC10 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode
祎雪双十Gy12 小时前
从 DataX 的配置加载说起:我用 FastJson2 做了一个轻量级动态配置管理库
java·后端
小锋java123412 小时前
分享一套锋哥原创的SpringBoot4+Vue3宠物领养网站系统
java
郝学胜_神的一滴16 小时前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
考虑考虑16 小时前
Java实现hmacsha1加密算法
java·后端·java ee
掉鱼的猫16 小时前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·spring boot
plainGeekDev16 小时前
广播接收器 → Flow + Lifecycle
android·java·kotlin