c++中常用库函数

简介: c++中常用库函数

大小写转换

islower/isupper函数

char ch1 = 'A';
char ch2 = 'b';
//使用islower函数判断字符是否为小写字母
if(islower(ch1)){
    cout << ch1 << "is a lowercase letter." << end1;
} else{
    cout << ch1 << "is not a lowercase letter." << end1;
}
//使用isupper函数判断字符是否为大写字母
if(isupper(ch2)){
    cout << ch2 << "is a uppercase letter." << end1;
} else{
    cout << ch2 << "is not a uppercase letter." << end1;
}

tolwer/toupper函数

char ch1 = 'A';
char ch2 = 'b';
//使用tolower函数将字符转换为小写字母
char lowercaseCh1 = tolwer(ch1);
cout << "tolower of" << ch1 << "is" << lowercaseCh1 << end1;
//使用toupper函数将字符转换为大写字母
char uppercaseCh2 = toupper(ch2);
cout << "uppercase of" << ch2 << "is" << uppercaseCh1<<end1;

ascii码


大小写转换


实例

#include<bits/stdc++.h>
using namespace std;
char covertedCh(char ch){
  if(islower(ch)ch = toupper(ch));
  else if(isupper(ch)ch = tolower(ch));
}
int main()
{
    string s; getline(cin,s);
    for(auto &i : s)
        i = covertedCh(i);
    cout << s << end1;
    return 0;
}

二分查找

二分查找的前提

  • 只能对数组进行二分查找
  • 数组元素单调

binary_seach函数

vertor<int> numbers = {1, 3, 5, 7, 9};
int target = 5;
//使用binary search查找目标元素
bool found = binary_seach(numbers.begin(),number.end(),target());
if(found){
    count << "Target element" << target << "found." << end1;
} else{
    cout << "Target element" << target << "not founf."<< end1;
}

lower_bound和upper_bound

前提:数组必须是非降序

如果要在非降序的数组中使用,可以通过修改比较函数实现(方法与sort自定义比较函数类似)。

loower_bound(st,ed,x)返回地址[st,ed)中第一个大于等于x的元素的地址

upper_bound(st,ed,x)返回地址[st,ed)中第一个大于x的元素的地址

如果不存在则返回最后一个元素的下一个位置,在vector中即end()

//初始化v
vector<int> v = {5, 1, 7, 3, 10, 18, 9};
sort(v.begin(),v.end());
for(auto &i : v) cout << i << ' ';
cout << '\n';
//找到数组中第一个大于等于8的元素的位置
cout << (lower_bound(v.begin(),v.end(),8) - v.begin()) << endl;

排序

sort的用法

*sort(起始地址, 结束地址的下一位, 比较字符);

vector<int> v = {5, 1, 3, 9, 11}; 
sort(v.begin(), v.end());//默认升序
for(int i = 1; i <= v.size(); ++i) cout << a[i] << ' ';
子定义比较函数
bool cmp(const int &u, const int &v)
{
    return u > v;
}
int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  vector<int> v = {5, 1, 3, 9, 11};
    sort(v.begin(), v.end(),cmp);//降序排序
    for(int i = 0; i < v.size(); ++i)
        cout << v[i] << ' ';
}
vertor<int> v = {5, 1, 3, 9, 11};
sort(v.begin(), v.end(), [](const int &u, const int &v))
{//降序排序
    return u > v;
};
for(int i = 0; i < v.size(); ++i)
        cout << v[i] << ' ';

结构体可以将小于号重载后进行排序,当然用前面的方法也是可行的

struct Node{
    int u, v;
    bool operator < (const Node &m) const{
        return u == m.u? v < m.v : u < m.u;
    }
};

例题


#include<bits/stdc++.h>

全排列

next_permutation()函数

next_permutation 函数用于生成当前序列的下一个排序。它按照字典序对序列进行重新排序,如果存在下一个排列,则将当前序列更改为下一个排列,并返回true; 如果当前序列已经是最后一个排序,则将序列更改为第一个排列,并返回false.

vertor<int> nums = {1, 2, 3};
cout << "Initial permutation: ";
for(int num : nums){
    cout << num << " ";
} cout << endl;
//生成下一个排列
while(nevt_permutation(nums.begin(), nums.end()){
    cout << "Next permutation: ";
    for(int num : nums){
        cout << num << " ";
    } 
    cout << endl;
}
_ _
123
132
213
312
321

prev_permutation() 函数

prev_permutation 函数与 next_permutation 函数相反,它用于生成当前序列的上一个排序。它按照字典对序列进行重新排序,如果存在上一个排列,则将当前序列更改为上一个排序,并返回true; 如果当前序列已经是第一个排序,则将序列更改为最后一个排序,并返回false.

vertor<int> nums2 = {3, 2, 1};
cout << "Initial permutation: ";
for(int num : nums2){
    cout << num << " ";
} cout << endl;
//生成上一个排列
while(prev_permutation(nums2.begin(), nums2.end()){
    cout << "Prevous permutation: ";
    for(int num : nums2){
        cout << num << " ";
    } 
    cout << endl;
}
321
312
231
213
132
123

实例

题目


#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    string str;
    getline(cin, str);
    int len = str.size();
    sort(str.begin(), str.end());
    bool tag = true;
    while (tag) {
        cout << str << endl;
        tag = next_permutation(str.begin(), str.end());
    }
    return 0;
}

最值查找

min和max函数

eg

min(3,5) = 3;

min{1, 2, 3, 4} = 1;

max(5, 7) = 7;

max{1, 2, 3, 4} = 4;

min_element 和 max_element

min_element(st, ed)返回地址[st, ed)中最小的那个值的地址(迭代器),传入参数为两个地址或迭代器。

max_element(st, ed)返回地址[st, ed)中最大的那个值的地址(迭代器),传入参数为两个地址或迭代器。

时间复杂度均为O(n),n为数组大小(由传入的参数决定)。

vetor<int> v = {5, 1, 3, 9, 11}; // 初始化V
//输出最大的元素,*表示解引用,即通过地址(迭代器)得到值
cout << *max_element(v.begin(), v.end()) << '\n';

nth_element 函数

nth_element(st,k,ed)

进行部分排序,返回值为void()

传入参数为三个地址或迭代器。其中第二个参数位置的元素将处于正确位置,其他地理位置的顺序可能是任意的,但前面的都比它小,后面的都比它大。

时间复杂度O(n).

vector<int> v = {5, 1, 7, 3, 10, 18, 9};//初始化V
//输出最大的元素, *表示解引用,即通过地址(迭代器)得到值
nth_element(v.begin(), v.begin() + 3, v.end());
//这里v[3]的位置将会位于排序后的位置,其他的任意
for(auto &i : v) cout << i << " ";

输出

3 1 5 7 9 18 10

取小数(保留两位小数)

cout << fixed << setprecision << 1.0*sum / m << '\n';

其他库函数

memset()

==memset()==是一个用于设置内存块值的函数。它的原型定义在****头文件中,函数的声明如下:

void* memset(void* ptr, int value, size_t num);

memset()函数接受三个参数:

1.ptr: 指向要设置值的内存块的指针。

  1. value: 要设置的值,通常是一个整数(8位二进制数)
  2. num: 要设置的字节数(Byte = 8bit)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[5];
    memset(a, 0, sizeof(a));//memset(a, 0, sizeo a);
    //或者写成 0*3f
    for(int i = 0; i < 5; ++i) cout << a[i] << '\n';
    //memset(a, 1, sizeof a);
    //for(int i = 0; i < 5; ++i) 
    //cout << bitset<32>(a[i]) << '\n';
    reutnr 0;
}

swap()

swap(T &a, T &b) 函数接受两个参数

swap()函数可以用于交换任意类型的变量

int a = 10;
int b = 20;
std::swap(a, b);

reverse()

reverse() 是一个用于反转容器中元素顺序的函数。

它的原型定义在****头文件中,函数声明如下:

template<class BidirIt>
void reverse(BidirTt first, BidirIt last);

reverse()函数将[first, last) 范围内的元素顺序进行反转。

也就是说,它会将[first, last)范围内的元素按相反的顺序重新排列。

reverse()函数可用于反转各种类型的容器,包括数组、向量、链表等。

以下是一个示例,展示如何使用reverse()函数反转一个整型向量的元素顺序:

#include<iostream>
#include<vector>
#includea<algorithm>
using namespace std;
int main(){
    vector<int> vec = {1, 2, 3, 4, 5};
    reverse(vec.begin(), vec.end());
    for(int num : vec){
  cout << num << " ";
    }
    cout << endl;
    return 0;
}

unique()

unique(first, last) 函数接受两个参数:

unique()函数将[first, last)范围内的相邻重复元素去除,并返回一个指向去重后范围的尾后迭代器。

去重后的范围中只保留了第一个出现的元素,后续重复的元素都被移除。

unique()函数可用于去除各种类型的容器中的相邻重复元素,包括数组、向量、链表等。

以下是一个示例,展示如何使用unique()函数去除一个整型向量中的相邻重复元素

//必须先排序,然后去重
int main(){
  vertor<int> vec = {1, 1, 2, 2, 3, 3, 4, 4, 5};
    auot it = unique(vec.begin(), vec.end());
    vec.erase(it, vec.endl);
    for(int num : vec){
    cout << num << " ";
    }
    cout << endl;
    return 0;
}


相关文章
|
22天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
20 2
|
28天前
|
C++
C++ 数学函数、头文件及布尔类型详解
C++ 支持数学操作,如`max`和`min`函数找最大值和最小值,以及`&lt;cmath&gt;`库中的`sqrt`、`round`等数学函数。`bool`类型用于布尔逻辑,取值`true`(1)或`false`(0)。布尔表达式结合比较运算符常用于条件判断,例如在`if`语句中检查年龄是否达到投票年龄。在代码示例中,`isCodingFun`和`isFishTasty`变量分别输出1和0。
121 1
|
28天前
|
算法 C++ 容器
C++中模板函数以及类模板的示例(template)
C++中模板函数以及类模板的示例(template)
|
1天前
|
编译器 C++
【C++进阶】引用 & 函数提高
【C++进阶】引用 & 函数提高
|
6天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
6天前
|
C++
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
|
6天前
|
存储 C++
C++从入门到精通:2.1.1函数和类
C++从入门到精通:2.1.1函数和类
|
15天前
|
算法 搜索推荐 C++
浅谈sort函数底层(一道c++面试的天坑题)
浅谈sort函数底层(一道c++面试的天坑题)
|
17天前
|
编译器 C++
C++ 解引用与函数基础:内存地址、调用方法及声明
C++ 中的解引用允许通过指针访问变量值。使用 `*` 运算符可解引用指针并修改原始变量。注意确保指针有效且不为空,以防止程序崩溃。函数是封装代码的单元,用于执行特定任务。理解函数的声明、定义、参数和返回值是关键。函数重载允许同一名称但不同参数列表的函数存在。关注公众号 `Let us Coding` 获取更多内容。
135 1
|
18天前
|
编译器 C语言 C++
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
20 0