【代码】c++几个常用的函数

Hello!大家好,我是@学霸小羊,今天讲讲c++函数库里面的几个基本函数。

1.sort()

sort()是大名鼎鼎的排序函数,以前起码一两个循环的排序,用这个函数一行代码就可以解决。

格式:

sort(数组名"+"开始下标,数组名"+"开始下标,数组前后两个数需要保持的条件(函数,可省略))
例:
bool cmp(int x,int y) return x>y;
sort(a+1,a+10+1,cmp);

2.sqrt()

sqrt()函数是平方根函数,这可以用于勾股定理。

先去学一下勾股定理,待会我附下代码。

【数学】勾股定理https://blog.csdn.net/yangyanbin_sam/article/details/138959059?spm=1001.2014.3001.5501代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c;
	cin>>a>>b;
	c=sqrt(a*a+b*b);
	cout<<c<<endl;
	cout<<a<<"×"<<a<<"+"; 
	cout<<b<<"×"<<b<<"=";
	cout<<c<<"×"<<c;
	return 0;
}

3.abs()

abs()函数用于计算绝对值,这对于负数有一些帮助,可以去学一下负数:

【数学】负数https://blog.csdn.net/yangyanbin_sam/article/details/139769603?spm=1001.2014.3001.5501来看一道题:

时限: 1s **空间:**256m

题目描述

小明是一名热爱体育的OIER,这天她在进行折返跑的练习,练习的规则如下:

在开始的时候,小明站在x=0处,有n个路标分别坐落于x1,x2,x3...xn。小明想在T单位时间内 访问尽可能多的路标,她每跑一个单位长度的距离,需要一个单位时间。

小明按照一个特殊的规则来访问路标,距离原点越近的路标,对小明越重要。

她总是会朝未访问过的距离原点最近的路标跑。没有两个路标距离原点的距离相等。

请你帮助计算一下,小明在日落之前能够访问多少个路标?

输入格式

第一行输入两个整数T、n,

随后n行,每行一个整数,代表路标 的位置xi

数据规模

对于20%数据: T ≤ 20,n ≤ 15

对于40%数据: n ≤ 3000

对于100%数据: 1 ≤ n ≤ 50000 , 100000 ≤

i ≤ 100000 ,1 ≤ T ≤ 1000000000

输出格式

输出一行一个整数,表示小明在日落之前能够访问到的路标的个数。

输入/输出例子1

输入:

25 5

10

-3

8

-7

1

输出:

4

样例解释

样例解释#1

小明将先前往 1,再去-3,再去-7,然后去8,共花费1+4+4+15=24 ,本来下一步应该去 10 ,但时间不够了,于是输出4

这道题就需要用负数的绝对值来比较每一个点距离原点的距离了,需要用结构体储存绝对值。

代码:

#include<bits/stdc++.h>
using namespace std;
long long t,n,s,xa;
struct stu
{
	int a=0,b=0;
}a[500005];
bool cmp(stu x,stu y)
{
	return x.b<y.b;
}
int main(){
    cin>>t>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].a;
        a[i].b=abs(a[i].a);
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n&&xa+abs(a[i].a-a[i-1].a)<=t;i++)
    {
    	xa+=abs(a[i].a-a[i-1].a);
        s++;
	}
	cout<<s;
    return 0;
}

4.字符串函数

对于字符串,也有很多函数:

cpp 复制代码
// C++标凇库提供了丰富的字符串操作函数,下面介绍一些常用的函数。
// 备注:位置可以看成是字符串的下标,从0开始
// 获取字符串长度
// 使用length或size函数来获取字符串的长度。
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
 
using namespace std;
 
int main() {
    string str = "Hello, World!";
    size_t length = str.length(); // or str.size()
    cout << "Length of the string: " << length << endl;
 
    // 拼接字符串
    // 使用+操作符或append函数来拼接字符串。
    string str1 = "Hello";
    string str2 = "World";
    string str3 = str1 + ", " + str2 + "!";
    cout << str3 << endl;
 
    str1.append(", ").append(str2).append("!");
    cout << str1 << endl;
 
    // 查找子字符串
    // 使用find函数来查找子字符串的位置。没找到则返回-1
    string str4 = "Hello, World!";
    size_t pos = str4.find("World");
    if (pos != -1) {
        cout << "Found 'World' at position: " << pos << endl;
    } else {
        cout << "'World' not found" << endl;
    }
 
    // 替换子字符串
    // 使用replace函数来替换子字符串。
    string str5 = "Hello, World!";
    str5.replace(7, 5, "C++"); // 从位置7开始,替换长度为5的子字符串(这5个字符将被删除)
    cout << str5 << endl;// "Hello, C++!"
 
    // 子字符串提取
    // 使用substr函数提取子字符串。
    string substr = str5.substr(7, 5); // 从位置7开始,提取长度为5的子字符串
    cout << substr << endl;
 
    // 清空字符串
    // 使用clear函数来清空字符串。
    string str6 = "Hello, World!";
    str6.clear();
    cout << "After clear: " << str6 << endl;
 
    // 字符串是否为空
    // 使用empty函数检查字符串是否为空。
    string str7 = "";
    if (str7.empty()) {
        cout << "The string is empty" << endl;
    } else {
        cout << "The string is not empty" << endl;
    }
 
    // 访问字符
    // 使用索引操作符[]或at函数来访问字符串中的字符。
    string str8 = "Hello, World!";
    char ch1 = str8[0]; // 访问第一个字符
    char ch2 = str8.at(1); // 访问第二个字符
 
    cout << "First character: " << ch1 << endl;
    cout << "Second character: " << ch2 << endl;
 
    // 插入字符串
    // 使用insert函数在指定位置插入子字符串。
    string str9 = "Hello, World!";
    str9.insert(7, "C++ ");// 在s[7]位置插入字符串
    cout << str9 << endl; // 输出: Hello, C++ World!
 
    // 删除字符串
    // 使用erase函数删除指定位置的子字符串。
    string str10 = "Hello, C++ World!";
    str10.erase(7, 4); // 从位置7开始删除长度为4的子字符串
    cout << str10 << endl; // 输出: Hello, World!
 
 
    // 反转字符串
    // 虽然没有直接的函数,但可以使用标准库算法reverse来反转字符串。
    string str17 = "Hello, World!";
    reverse(str17.begin(), str17.end());
    cout << str17 << endl; // 输出: !dlroW ,olleH
 
    return 0;
}

今天就讲到这啦,拜拜!

相关推荐
职略2 小时前
负载均衡类型和算法解析
java·运维·分布式·算法·负载均衡
A22742 小时前
LeetCode 196, 73, 105
java·算法·leetcode
阿里巴巴P8资深技术专家3 小时前
Java常用算法&集合扩容机制分析
java·数据结构·算法
zengson_g3 小时前
当需要对大量数据进行排序操作时,怎样优化内存使用和性能?
java·数据库·算法·排序算法
爱上电路设计4 小时前
有趣的算法
开发语言·c++·算法
studyForMokey4 小时前
kotlin 函数类型接口lambda写法
android·开发语言·kotlin
窜天遁地大吗喽4 小时前
每日一题~ (判断是否是合法的出栈序列)
c++
2401_858120264 小时前
探索sklearn文本向量化:从词袋到深度学习的转变
开发语言·python·机器学习
与墨学长5 小时前
Rust破界:前端革新与Vite重构的深度透视(中)
开发语言·前端·rust·前端框架·wasm
虫小宝5 小时前
Java中的软件架构重构与升级策略
java·开发语言·重构