c++常用函数

C++常用函数汇总

STL容器和迭代器相关的函数

头文件

#include<algorithm>

实验数据

vector<int> v = {1,1,2,3,4,5,6,7,8,9};
string str = "hello world";

max_element 返回容器中最大元素的迭代器

auto i = max_element(v.begin(), v.end());
cout << *i << endl;//输出迭代器指向的值

reverse 原地翻转容器中的元素

reverse(v.begin(), v.end());
for (auto i : v)
{
 cout << i << endl;// 这里 i 直接是容器元素的拷贝,不需要解引用
}
reverse(str.begin(), str.end());
cout << "str " << str << endl;

accumulate 序列求和

需要头文件

#include <numeric>
int result = accumulate(v.begin(), v.end(), 0);//0处代表累加的初值

count 统计元素个数

int key = 1;
int num = count(v.begin(), v.end(), key)

find 返回找到元素的迭代器,找不到返回end()

int key = 1;
if (find(v.begin(), v.end(), key) != v.end())//判断容器里是否有该元素

copy 拷贝

vector<int> v2;
v2.resize(v.size());
copy(v.begin(), v.end(), v2.begin());

advance 迭代器进退

int key =3;
auto i = find(v.begin(), v.end(), key);
advance(i, 2);//正数前进,负数后退
cout << *i << endl;

next_permutation 数组全排列

注意,使用next_permutation函数的数组必须是升序排列,否则全排列失败
可以用sort()先排序

vector<int> v0 = {1,2,3,4};
vector<vector<int> > v1;
while (next_permutation(v0.begin(), v0.end()))
{
 v1.push_back(v0);
}
for (auto i : v1)
{
 for (int j : i)
 {
	 cout << j << " ";
 }
 cout << endl;
}
输出结果
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

random_shuffle 对容器元素进行随机排序

random_shuffle(v0.begin(),v0.end());

tolower() 大写字母转小写,toupper() 小写转大写

除了大写字母其余不做修改

string s = "AB cDEFg%!";
for (int i = 0;i < s.length();i++)
{
 s[i] = tolower(s[i]);
}
cout << s << endl;
string s = "AB cDEFg%!";
for (int i = 0; i < s.length(); i++)
{
 s[i] = toupper(s[i]);//小写转大写
}
cout << s << endl;

isalpha() 判断是否是字母

string s = "123AB cDEFg%!";
for (int i = 0; i < s.length(); i++)
{
 if (isalpha(s[i]))//如果是字母转换成大写
 {
	 s[i] = toupper(s[i]);
 }
}
cout << s << endl;

isdigit() 判断是否十进制数

isalnum() 判断是否字母或数字

islower() 判断是否小写字母

isupper() 判断是否大写字母

sort() 排序函数

//默认升序
sort(A.begin(),A.end());
//自定义降序
sort(A.begin(),A.end(),[](int a,int b){return a > b;});

istringstream 将数据类型转换为输入数据流,读取数据

1.需要头文件

#include <sstream>

2.读取int数据,遇到空格可以跳过继续读,遇到其他字符如%停止

string s1 = "123  456%7";
istringstream iss(s1);
vector<int>numbers;
int num;
while (iss >> num)
{
 numbers.push_back(num);
 cout << num << endl;
}
//输出
123
456

3.按顺序读取不同类型数据

string data = "123 John 456 Doe";
istringstream iss1(data);
int id1, id2;
string name1, name2;

// 从字符串流中按照预设的格式提取数据
iss1 >> id1 >> name1 >> id2 >> name2;
cout << "ID1: " << id1 << ", Name1: " << name1 << endl;
cout << "ID2: " << id2 << ", Name2: " << name2 << endl;

4.读取数据

string input = "aaa,bb,ccc";
stringstream ss(input);
string str;
vector<string> strs;
//getline, 将数据流中的字符以","为间隔传入str
while (getline(ss, str, ',')) {
 strs.push_back(str);
}
for (string i : strs)
{
 cout << i << endl;
}
//输出
aaa
bb
ccc

replace 将字符串中某些字符替换成其他字符

string s = "a,b,c";
replace(s.begin(), s.end(), ',', ' ');//两个位置字符都不能为空

v.erase和 map.erase 删除迭代器指定位置元素

vector<int> v = {1,2,3,4,5,6,7,8,9};
for (auto i = v.begin();i != v.end();)
{
	if (*i % 2 != 0)
	{
		i = v.erase(i);
	}
	else i++;
}
//输出
2
4
6
8
map<int,string> c = 
{
	{1, "one" }, {2, "two" }, {3, "three"},
	{4, "four"}, {5, "five"}, {6, "six"  }
};
// erase all odd numbers from c
for (auto it = c.begin(); it != c.end(); ) {
	if (it->first % 2 != 0)
		it = c.erase(it);
	else
		++it;
}
for (auto& p : c) {
	cout << p.second << ' ';
}

upper_bound 找到大于某值得第一个值,返回地址

lower_bound 找到大于等于某值得第一个值,返回地址

vector<int> v = { 1,2,3,4,5,6,7,8,9 };
auto i = upper_bound(v.begin(), v.end(),3);
cout << *i << endl;//输出4
auto j = lower_bound(v.begin(), v.end(), 3);
cout << *j << endl;//输出3

substr 截取子串,还有另一种方法

string s = "hello world";
string ss = s.substr(0, 5);
cout << ss << endl;

int len = s.length();
string sss(s, 5, len - 5);
cout << sss << endl;
//输出
hello
 world

正序和倒序遍历

for (auto i = v.begin(); i != v.end(); i++)
{
 cout << *i << endl;
}
for (auto i = v.rbegin(); i != v.rend(); i++)
{
 cout << *i << endl;
}

类型转换

string转int

string s1 = "123";
string s2 = "0.0002";
int n1 = stoi(s1);
int n2 = stoi(s2);//结果是0,强制类型转换

string转long long int

int不超过10个数字,long long int不超过19个数字

string s3 = "1111111111111111111";
long long n3 = stoll(s3);

整型转string

int a = 123456;
string s = to_string(a);

其他函数

swap 交换

swap(nums[i], nums[ptr]);

max 比较两者最大值

max_num = max(a,b);

round 四舍五入成整数

int a = round(11.5); //四舍五入,得到a=12

ceil 向上取整

int a = ceil(11.3); //a = 12
int b = ceil(-11.6); //b = -11
auto c = double(11)/2; //c = 5.5
auto d = ceil(double(11)/2); // d = 6

floor 向下取整

int a = floor(11.6); //a = 11
int b = floor(-11.6); //b = -12
auto c = double(11)/2; //c = 5.5
auto d = floor(double(11)/2); // d = 5

pow n次方

pow(a,2);
pow(a, 5);

abs 绝对值

num = abs(num);//abs是针对于int类型的

INT_MAX/MIN 最大最小整型

C++中常量INT_MAX和INT_MIN分别表示最大、最小整数。

  • 21
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值