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

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

相关推荐
天天喝旺仔3 分钟前
Go 泛型实战:从类型参数、约束到可复用泛型容器与函数
数据结构·算法·容器·go
郝学胜_神的一滴5 分钟前
C++20模板元编程 04:变量模板 别名模板与模板Lambda实战
c++·visual studio
可爱的小小小狼7 分钟前
【无标题】
java·算法
dtq042415 分钟前
数据结构 - 栈
c语言·数据结构·学习
Methy16 分钟前
一次线上死锁排查:std::list::size () 居然是 O (n)?
c++·后端
2401_8685347817 分钟前
利弊比较类 社会生活类
c++·需求分析
wuminyu29 分钟前
纯轻量级锁体系下C2编译器锁粗化和消除机制剖析
java·linux·c语言·jvm·c++
weilx123444 分钟前
C++笔记-mutex
c++
beijixinghe1 小时前
第15节 指针作为函数参数的工程实战用法
开发语言·c++·c++基础·c++入门·几何引擎c++
j7~1 小时前
【C++微服务项目开发脚手架】(准备篇)从 0 到跑通:Docker 容器开发环境 + 宿主机用户 + Trae 远程连接 全链路踩坑实战
linux·c++·学习·xshell·docker容器·trae