C++常用算法总结

PS:后续添加 没有顺序了 搜到什么看什么吧

非char型数组的复制

1
2
3
4
5
6
7
8
9
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[] = {1,2,3};
int b[3];
memcpy(b,a,2*sizeof(int));
cout << b[0] << b[1] << b[2] << endl;
}

查找子串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1 = "123";
string s2 = "23";
string s3 = "73";
cout << s1.find(s2) << endl;;
unsigned int c = s1.find(s3);
cout << c << endl;

// 如果没找到 发挥特别的指针 npos
// freopen("in.txt","r",stdin);
}

数据初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<bits/stdc++.h>
using namespace std;
#define MAXN 220
void display(int num)
{
cout << num << " ";
}
int main()
{
int a[2][5],b[10];
//fill只能初始化一维数组
fill(b,b+10,1);
//fill(a,a+100,1);
memset(a,0,sizeof(a));
//for_each(a,a+MAXN,display);

//数据输出
cout << "b:" << ":";
for_each(b,b+10,display);
cout << endl;

// cout << a << ":";
// for_each(a,a+10,display);
// cout << endl;
}

最大公约数

1
2
3
4
5
6
7
8
9
//公倍数 两数相乘  除以公约数
#include<bits/stdc++.h>
using namespace std;
int a[40] = {1,2};
int main()
{
cout << __gcd(12,6);
//cout << __INT32_MAX__ << endl;
}

1.accumulate求和

1
2
3
4
5
6
7
8
9
10
11
12
#include<numeric>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(3);
v.push_back(5);
v.push_back(9);
cout << accumulate(v.begin(),v.end(),0); //0是累加的初值
}

2. atof 字符串转换为double

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
字符串 类型换成double类型
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int main()
{
char a[] = "123" ;
int b = atof(a);
cout << b;
}

3. 容器为空 begin和end相等

1
2
3
4
5
6
7
8
9
10
11
12
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> v;
if(v.begin()==v.end())
cout << "ok1" << endl;
v.push_back(4);
if(v.begin()==v.end())
cout << "ok2" << endl;
}

4. bitset

可实现十进制和二进制的互换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<bitset>
using namespace std;
int main()
{
bitset<32> a(8);
cout << a[1] << endl;
int b;
b = a.to_ullong();
cout <<a << " " << b <<endl;
}
​```c
### 5.copy
​```c
/*
copy(begin,end,begin)
将第一个数组的begin至end 赋值到第二个容器begin开始
*/
#include<iostream>
#include<algorithm>
using namespace std;
template <class T>
void print(T x)
{
cout << x << endl;
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9} ;
int b[] = {9,8,7,6,5,4,3,2,1} ; //
copy(a,a+5,b); //复制前五个
for_each(b,b+9,print<int>);
}

6. count

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
count(begin,end, int) 大于int型的数
count_if(begin,end,fun) 自定义函数
*/

#include<iostream>
#include<algorithm>
using namespace std;
template <class T>
void print(T x)
{
cout << x << endl;
}

int main()
{
int a[10] = {1,2,3,4,5,5,7,8,9,10};
cout << count(a,a+10,10);
//for_each(a,a+9,print<int>) ;
}

7. erase()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
void fun(string &str)
{
str.pop_back();
}
int main()
{
string s= "123456";
cout << s << endl;
fun(s);
cout << s << endl;
//删除某一段的数值
s.erase(s.end()-2,s.end()-1);
cout << s << endl;
}

8. find_if()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
find_if(begin,end,greatThree)
找到大于三的所以所有数,return 首地址 就是一个数组
*/

#include<iostream>
#include<algorithm>
using namespace std;
bool greatThree(int x)
{
if(x > 3)
return true;
else
return false;
}
template <class T>
void print(T x)
{
cout << x << endl;
}

int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *p = find_if(a,a+10,greatThree) ;
for(int i = 0;i < 7;i++)
cout << *p++ <<endl;

//for_each(a,a+9,print<int>) ;
}

9 find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
find返回找到元素的地址
*/

#include<iostream>
#include<algorithm>
using namespace std;
template <class T>
void print(T x)
{
cout << x << endl;
}

int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
cout << find(a,a+10,6);
}

10.for_each()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream> 
#include<algorithm>
using namespace std;
template <class T>
void print(T x)
{
cout << x << endl;
}

int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
for_each(a,a+10,print<int>);
}

11.itoa

int类型换成char类型 还可以设置转换进制
十进制可以转换成任意进制

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
int 类型换成string类型
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int a = 64;
char b[3];
itoa(a,b,8); //8表示转换后的进制
cout << b[0] << b[1] << b[2];
}

12. max_element 和 min_element 求最大最小元素

1
2
3
4
5
6
7
8
9
10
11
12
/*
*max_element(begin,end)
求最大成员
*/
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {1,2,5,6,3,4,8,9,7} ;
cout << *max_element(a,a+9);
}

13. memset 初始化

  • 可以如下替代

    1
    2
    3
    4
    5
    6
    7
    8
    #include<iostream>
    using namespace std;
    int main()
    {
    int a[10],b[10] = {1,2,3};
    for(int i = 0;i < 10;i++)
    cout << a[i] << " " << b[i] << endl;
    }
  • memset

1
2
3
4
5
6
7
8
9
10
11
12
/*
初始化数组
*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int a[10] ;
memset(a,0,10); //十个全部放0

}

14.replace_if() 满足条件的替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
replace_if(begin,end,fun,int)将满足fun条件的换成int
replace(begin,end,3,5) 将所以的3替换成5
*/

#include<iostream>
#include<algorithm>
using namespace std;
bool greatTree(int x)
{
if(x > 3)
return true;
else
return false;
}
template <class T>
void print(T x)
{
cout << x << endl;
}

int main()
{
int a[] = {1,2,3,4,5,6,7,8,9} ;
replace_if(a,a+9,greatTree,9);//大于三的数替换成9
for_each(a,a+9,print<int>) ;
}

15.strstr() char*类型找到子串

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{

char* str = "I am a Android developer!";
char* subStr = "Android";
char* result = strstr(str, subStr); //找到子串后面的字符串
printf("result = %s\n", result);//结果:result = Android developer!
return 0;
}

16. substr() 截取字符

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int main()
{
string str = "123456789";

//__LONG_LONG_MAX__ a = 18374001125;
cout << str.substr(2,3); //截取第i个字符后面三个
}

17.计时 getTIckCount()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
long n = 1000000000;
long t1 = GetTickCount();
//...娴嬭瘯浠g爜
while(n--);
long t2 = GetTickCount();
cout << "time:"<<(int(t2)-int(t1)) << endl;
return 0;
}

18.toupper() 和 tolower 字符的变换

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int main()
{
string a = "oIu";
a[0] = toupper(a[0]);
a[0] = tolower(a[1]);
cout << a;
}

19. 去重复unique() 必须已经排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<algorithm>
using namespace std;
void print(int a)
{
cout <<a << " ";
}
int main()
{
int a[6] ={1,1,2,2,3,3};
for_each(a,a+6,print);
cout << endl;
int pos = unique(a,a+6)-a;
for_each(a,a+6,print);
cout << endl;
}

20. 判断isalpha()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
判断字符或数字
*/

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
char a = 'r' ,b = '1';
if(isalpha(a))
cout << "ok" ;
if(isdigit(b))
cout << "ko";

}

21. 取第k位数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h> 
int len(int x){
if(x<10) return 1;
return len(x/10)+1;
}

// 取x的第k位数字
int f(int x, int k){
if(len(x)-k==0) return x%10;
return f(x/10,3); //填空
}

int main()
{
int x = 23574;
printf("%d\n", f(x,3));
return 0;
}

交并集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<sstream>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void print(int n)
{
cout << n << " ";
}
int main()
{
vector<int> v1 = {1,2,3},v2={2,3,4},v3;
//差集
// set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
// 交集
// set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
//并集
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(v3, v3.begin()));
v1 = v3;
for_each(v1.begin(),v1.end(),print);
}

vector 通过值删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int n)
{
cout << n << " ";
}
int main()
{
vector<int> v={1,2,3,4,5};
v.erase(remove(v.begin(),v.end(),3));
for_each(v.begin(),v.end(),print);
}
Donate comment here