C转C++速成

基础框架

cpp 复制代码
 #include<bits/stdc++.h>//万能头文件
 using namespace std;
 ​
 int main(){
 ​
     return 0;
 }

c的库函数去掉后缀.h,再在最前面加c可用在c++里,不过有万能头文件的话也不太需要去记了

输入输出

cin相当于scanf

复制代码
 cin>>n;

cout相当于printf

复制代码
 cout<<n;

endl相当于换行

复制代码
 cout<<n<<end1;

多个数据

复制代码
 cout<<"want to sleep"<<" "<<n<<" "<<s<<endl;
字符串

定义字符串 用string ,并且可以直接相加拼接

复制代码
 string a = "hello";
 string b = " world!";
 string c = a + b;//c="hello world"

getline 相当于fgts但无需指定最大读取长度,也不用处理换行符

复制代码
 getline(cin,a);

获取字符串长度:s.length()或s.size()

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int main() {
    string s = "Hello";
    cout << s.length() << endl;  // 输出 5
    cout << s.size() << endl;    // 同样输出 5

    string empty = "";
    cout << empty.length() << endl; // 输出 0
    return 0;
}

截取子串:s.substr(pos, len)

cpp 复制代码
string sub = s.substr(pos);        // 从 pos 开始,截取到字符串末尾
string sub = s.substr(pos, len);  // 从 pos 开始,截取最多 len 个字符

// 用 substr 复制整个字符串(等价于 strcpy 的效果)
string dest = s.substr(0);  // 从位置 0 截取到末尾 → 完整复制

cout << dest << endl;  // 输出: Hello, world!
STL

vector(动态数组)

头文件:#include <vector>

创建数组:

cpp 复制代码
vector<int> v; // 空 vector,存储 int
vector<int> v(10); // 定义长度为10的数组,初始值为默认值0
vector<int> v(10, 2); // 定义长度为10的数组并全部初始化为2
vector<string> words; // 定义字符串数组,记得用resize分配大小

字符串数组赋值:

cpp 复制代码
vector<string> words = {"apple", "banana", "cherry"};

或者

cpp 复制代码
words[0] = "apple";
words[1] = "banana";
words[2] = "cherry";

查看数组大小:

cpp 复制代码
cout << v.size();

分配数组大小:

cpp 复制代码
v.resize(length);
cpp 复制代码
vector<int> v;
v.reserve(20); // 预分配 20 个元素的内存(物理空间)

访问数组元素: v[i],与C语言一致

删除数组末尾数据:

cpp 复制代码
v.pop_back();

末尾添加新的数据:

cpp 复制代码
v.push_back(data); // 这也是动态的体现之处

迭代器:

cpp 复制代码
 for(auto p=v.begin();p!=v.end();p++){
 ​
     cout<<*p<<" ";
 ​
 }
//遍历整个数组,这样的话如果数组扩容,也不用更改边界条件
  //v.begin在数组第一个元素位置,v.end在最后一个逻辑元素的下一个位置

set(集合)

map(键值对)

stack

头文件:include<stack>

创建栈:stack<int> s;

压栈:s.push(i);

出栈:s.pop()

访问栈顶:s.top();

获取长度:s.size();

queue

头文件:include<queue>

创建队列:queue<int>s;

入队:s.push(i);

出队:s.pop();

访问队首:s.front();

访问队尾:s.back();

获取长度:s.size();

sort排序函数

数组升序

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int main(){
    int arr[]={5,2,8,1,9};
    int n=5;
    //对于c风格数组从小到大排序(默认)
    sort(arr,arr+5);
    //但如果是vector容器,则是sort(arr.begin(),arr.end())
    for(int i=0;i<n;i++){
        printf("%d",arr[i]);
    }
    return 0;
}

数组降序

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

bool cmp(int a,int b){
    return a>b;   //(a排在前面,并且a>b,即降序)
}
int main(){
    int arr[]={5,2,8,1,9};
    int n=5;
    //从大到小排序(使用自定义cmp)
    sort(arr,arr+5,cmp);
    for(int i=0;i<n;i++){
        printf("%d",arr[i]);
    }
    return 0;
}

结构体排序

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

typedef struct{
    char name[20];
    int score;
}Student;

//按分数从高到低排序,分数相同按名字字典序排序  'A'<'B'
bool cmp(Student a,Student b){
    if(a.score!=b.score){
        return a.score>b.score;
    }
    return strcmp(a.name,b.name)<0;
    //其实C++ 的强大特性:std::string(或 char[])可以直接用 <, >, == 等运算符进行字典序比较,而不需要手动调用 strcmp!
    //所以可以写成a.name<b.name,前提是字符是string定义,而不是c中的char,c风格必用strcmp,c++可以直接比较,不能混了
}
int main(){
    Student stu[3];
    sort(stu, stu + n, cmp);
    for(int i=0;i<3;i++){
        scanf("%s %d",stu[i].name,stu[i].score);
    }
    
    return 0;
}

字符串排序

1.多个字符串

  • 按字典序排序(默认升序,且大写字母永远比小写字母小)
cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int main(){
    vector<string>s={"apple", "banana", "cherry"};
    sort(s.begin(),s.end());
    return 0;
}
  • 按字符串长度排序
cpp 复制代码
bool cmp(const string& a, const string& b) {              
    return a.length() < b.length();
}
sort(words.begin(),words.end(),cmp);
  • 按字典序升序排序,不区分大小写
cpp 复制代码
bool cmp(string a, string b) {
    for(int i=0;i<a.length();i++){
        a[i]=tolower(a[i]);
    }
    for(int i=0;i<b.length();i++){
        b[i]=tolower(b[i]);
    }
    return a<b;
}
sort(words.begin(),words.end(),cmp);
  • 按字典序降序排序,不区分大小写
cpp 复制代码
bool cmp(string a,string b) {              
    for(int i=0;i<a.length();i++){
        a[i]=tolower(a[i]);
    }
    for(int i=0;i<b.length();i++){
        b[i]=tolower(b[i]);
    }
    return a>b;
}
sort(words.begin(),words.end(),cmp);

2.单个字符串内按字典序排序

cpp 复制代码
string str = "dcba";
// 对字符串内部的字符进行排序
sort(str.begin(), str.end());

根据具体问题修改排序方式

相关推荐
郝学胜-神的一滴10 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
Tongzhi202611 小时前
从部署到运维:通芝科技无感考勤一体机的全流程效率解析
运维·数据结构·科技·算法·贪心算法
Wang's Blog11 小时前
AI Agent白手起家30: 动态示例选择器之根据长度选择 Few Shot 示例
算法
oyguyteggytrrwwwrt12 小时前
自制交叉线路识别算法
算法
手写码匠12 小时前
华为云Flexus+DeepSeek征文|Dify 多智能体协同编排实战:R1 规划 + V3 执行,构建企业 Agent 团队
人工智能·深度学习·算法·aigc
晓天衡宇•评测社区13 小时前
FSR-Bench 前沿科学推理榜单发布:GPT-5.5 居首,“会推理”未必“能答对”
算法
程序喵大人15 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
hanlin0315 小时前
动态规划专练:力扣第121、122题
笔记·算法·leetcode
To_OC15 小时前
LC 74 搜索二维矩阵:换皮的二分查找,我居然一开始没看出来
javascript·算法·leetcode
文心快码BaiduComate15 小时前
文心快码荣获“中国优秀软件产品”,实力再获国家级认可
算法·代码规范