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

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

相关推荐
txzrxz1 小时前
二分图详解
数据结构·c++·算法·图论·深度优先搜索·二分图染色
jinyishu_1 小时前
C++ 继承全解:从基础到高级特性
开发语言·c++
gaosushexiangji2 小时前
数字图像相关DIC系统在结构振动模态测量中的应用
算法
是个西兰花2 小时前
Linux:死锁与生产者消费者模型解析
linux·运维·服务器·c++·死锁·生产者消费者模型
KaMeidebaby2 小时前
卡梅德生物技术快报|如何制备单克隆抗体:小众禽类靶点单抗制备实操流程:双载体抗原交叉筛选完整工艺记录
人工智能·python·深度学习·算法·机器学习
北域码匠3 小时前
Karatsuba乘法超详细解析(原理+流程+性能分析+纯原生C#无第三方库完整实现)
算法·c#·大数乘法·高精度计算·数据结构与算法·分治算法·karatsuba 乘法
exm-zem3 小时前
从C语言到C++:C++入门1
c语言·c++
fpcc3 小时前
数据结构和算法—数学的应用
数据结构·c++·算法
皓月斯语3 小时前
B4451 [GESP202512 四级] 建造 题解
数据结构·c++·算法·题解