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

相关推荐
故事和你91几秒前
洛谷-【图论2-4】连通性问题2
开发语言·数据结构·c++·算法·动态规划·图论
扫地的小何尚几秒前
掌握 Agentic AI 技术:AI Agent 定制方法全景与实践路径
大数据·人工智能·算法·ai·llm·agent·nvidia
Brilliantwxx1 分钟前
【C++】 二叉搜索树
开发语言·c++·算法
二哈赛车手8 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
吃好睡好便好9 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
栗子~~9 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
YDS8299 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
仰泳之鹅9 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
未若君雅裁10 小时前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
于小猿Sup10 小时前
VMware在Ubuntu22.04驱动Livox Mid360s
linux·c++·嵌入式硬件·自动驾驶