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(s[i]) 传入一个字符数组可以返回他的长度

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);

相关推荐
Leinwin2 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
无极低码2 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
薛定谔的悦3 小时前
MQTT通信协议业务层实现的完整开发流程
java·后端·mqtt·struts
软件算法开发3 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
enjoy嚣士3 小时前
springboot之Exel工具类
java·spring boot·后端·easyexcel·excel工具类
Thera7773 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
罗超驿3 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
superior tigre4 小时前
22 括号生成
算法·深度优先
盐水冰4 小时前
【烘焙坊项目】后端搭建(12) - 订单状态定时处理,来单提醒和顾客催单
java·后端·学习