目录
二分查找
upper_bound, lower_bound
#include<algorithm>
using namespace std;
#include<algorithm>
using namespace std;
int main () {
int a[10];
pos1 = lower_bound(a, a + 10, s) - a;
//lower_bound(查找的起始位置,查找的终止为止,需要查找的数 )
//返回第一个大于等于s的地址
//地址!所以要数组下标要减数组的头“a”
pos2 = upper_bound(a, a + 10, s) - a;
//基本与lower_bound相似,唯一不同,它找的的第一个大于s的地址
//如果要找第一个小于(等于)s的地址,加个cmp
//for example
pos3 = lower_bound(a, a + 10, s, greater<int>()) - a;
//upper_bound同理
//这两种函数如果差找不到目标,就返回查找的最后一个元素的地址
return 0;
}
binary_search
#include<algorithm>
using namespace std;
#include<stdio.h>
#include<algorithm>
using namespace std;
int main () {
int a[10];
bool jdg = binary_search(a, a + 10, s);
//binary_search试图在a中寻找s。
//如果a内有s,它会返回true,否则返回false
//它不返回查找位置
return 0;
}
Stack
.top()
.size()
.push(a)
.pop()
.empty()
#include<stack.h>
using namespace std;
*每个函数的“()”别忘啦!
#include<stdio.h>
#include<stack>
#include<stdbool.h>
using namespace std;
int main(){
stack<int> s1;
//<>里是类型
//Init a empty stack s1
bool isempty = s1.empty();
//If empty return true; else return false
s1.pop();
//delete(move away) the top element, without a return
int a = 10;
s1.push(a);
s1.push(10);
//add the element a or 10 in the stack's top
stack<int>::size_type size = s1.size();
//return the number of elements in the stack
//attention:
//"stack<int> size_type" can't be replaced by "int"
printf("%d", size);
//output can use "%d"
//OMG!
//using "int" is okay!
int top_element = s1.top();
//return the element of the stack's top
//without delete it
return 0;
}
Init题外话
#include<stdio.h>
#include<stack>
using namespace std;
int main(){
stack<int> s1 = {1, 2, 3}; //wrong!!!!
deque<int> dq{1, 2, 3};
stack<int> s2(dq); //copy dq
//true!
return 0;
}
Queue
.empty()
.size()
.front()
.back()
.push(a)
.pop()
#include<queue>
using namespace std;
#include<stdio.h>
#include<stdbool.h>
#include<queue>
using namespace std;
int main(){
queue<int> q1;
bool isempty = q1.empty();
int size = q1.size();
int frontelement = q1.front();
int backelement = q1.back();
//both .back() and .front() are not deleting element
q1.push(10);
q1.pop();
//delete the front of queue
return 0;
}
max/min
几个量取max/min
#include <iostream>
#include <cmath>
int main () {
int a, b, c, ...;
max({a, b, c, ....});
//取a,b,c,...变量中的最大值
return 0;
}
数组取max/min
#include <iostream>
#include <algorithm>
int main () {
int a[5] = { 1, 2, 5, 4, 0};
cout << *max_element(a, a + 3) << endl;
//取下标 0 ~ 2 的 max
cout << *min_element(a, a + 3) << endl;
//取下标 0 ~ 2 的 min
//这个函数返回的是地址指针,需在最前面加上 *
return 0;
}
String
#include <string.h>
to_string()
#include <iostream>
#include <string.h>
using namespace std;
int main () {
//to_string()函数 数字转为字符串
int s1 = 123456789;
string s2 = to_string(s1);
//char s2[15] = to_string(s1) wrong!!!
string pi = "pi is " + to_string(3.1415926);
cout << pi << endl;
/*
output:
pi is 3.141593
*/
return 0;
}
substr()
#include <iostream>
#include <string.h>
using namespace std;
int main () {
//substr()函数 复制子字符串,从指定位置开始并具有指定的长度, substr(start, length)
//当 pos+len-1 超过字符串的范围时, 自动调整 len 的长度从而复制到字符串 s 的最后一个字符
//当 pos ,也就是初始位置超过字符串的范围时,substr函数会抛出一个out_of_range 的异常
string str1 = "123456";
string str2 = str1.substr(2); //只有一个数字5表示从下标为2开始一直到结尾
string str3 = str1.substr(2, 3);
cout << str2 << endl;
cout << str3 << endl;
/*
output:
3456
345
*/
return 0;
}
stoi()
#include <iostream>
#include <string.h>
using namespace std;
int main () {
//stoi()函数 字符串转为int型,stoi(字符串, 起始位置, n进制)
//string to int
string str = "1234";
int num = stoi(str);
cout << num << endl;
/*
output:
1234
*/
//同理stol(), stof(), stod()
return 0;
}
strcpy() / strncpy()
#include <iostream>
#include <string.h>
using namespace std;
int main () {
//strcpy(s1, s2) 字符串复制的函数
//作用是将字符串2复制到字符数组1中去
//strcpy(s1, s2, n)字符串选择复制的函数
//n:表示将字符串2中的n个单个字符复制到字符数组1中
//最少为0个,最多不能超过字符串2的长度
return 0;
}
strcmp() / strncmp()
#include <iostream>
#include <string.h>
using namespace std;
int main () {
//strcmp(s1, s2) 字符串比较的函数
//s1 = s2 return 0
//s1 < s2 return <0
//s1 > s2 return >0
//strncmp(s1, s2, n) 字符串选择比较的函数
//n:选择字符串的前n个字符进行比较
return 0;
}