1.产生随机数
要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;
要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a;
要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;
using namespace std;
#include <stdlib.h>
#include <time.h>
int main()
{
int i;
srand((unsigned)time(NULL)); //初始化随机数种子
for (i=0; i<10; i++) //产生10个随机数
{
cout<<rand()%(10-1)+1<<"/t";
}
cout<<endl;
return 0;
}
2.set 与 vector 转换
#include<set>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> vec;
vec = { 1, 2, 3, 4, 8, 9, 3, 2, 1, 0, 4, 8 };
set<int> st(vec.begin(), vec.end());
vec.assign(st.begin(), st.end());
vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
cout << *it<<endl;
return 0;
}
3.upper_bound和lower_bound
https://blog.youkuaiyun.com/qq1337715208/article/details/81072709
https://blog.youkuaiyun.com/u011008379/article/details/50725670
前提:一个非降序列!!!!!! 原理:在一个左闭右开的有序区间里进行二分查找
upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针(迭代器);
lower_bound则是返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针。
然后减去首迭代器 a,就是两个迭代器的距离了(也就是数组中下标)
但是如果这个序列里找不到大于 m 的值,所以迭代器走到尽头也没有找到,于是返回了一个尾迭代器(在这里是数组越界后的那一个元素的指针),它在数组中下标为 size 。
#include<bits/stdc++.h>
using namespace std;
int main() {
int a[] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
cout << (lower_bound(a, a + 12, 4) - a) << endl; //输出 9
cout << (upper_bound(a, a + 12, 4) - a) << endl; //输出 12
cout << (lower_bound(a, a + 12, 1) - a) << endl; //输出 0
cout << (upper_bound(a, a + 12, 1) - a) << endl; //输出 3
cout << (lower_bound(a, a + 12, 3) - a) << endl; //输出 6
cout << (upper_bound(a, a + 12, 3) - a) << endl; //输出 9
cout << (lower_bound(a, a + 12, 5) - a) << endl; //输出 12
cout << (upper_bound(a, a + 12, 5) - a) << endl; //输出 12
cout << (lower_bound(a, a + 12, 0) - a) << endl; //输出 0
cout << (upper_bound(a, a + 12, 0) - a) << endl; //输出 0
return 0;
}
4.C++ int与string的转化
https://blog.youkuaiyun.com/lxj434368832/article/details/78874108
https://blog.youkuaiyun.com/u010510020/article/details/73799996
5.大小写字母和数字的ascii码值是多少
a-z:97-122
A-Z:65-90
0-9:48-57
6.C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
https://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html