STL 标准模板库 数据结构与算法分离,不是面向对象的。
容器 container包含两类序列和关联
序列
list 双向链表 O(n) 任何位置上都快 链表是内存地址不连续 存的快
vector 动态数组 头尾快 中部慢 数组是连续的 取的快
deques
关联
set map 索引取 取的快
本文讲vector 的使用。随机数是用来测试的,顺便也给了两种方式(randon,default_random_engine)。
1.Vector的基本操作
2.Vector二维数组
3.随机数
1.Vector的基本操作
1.创建 vector<int>obj;
2.添加与赋值 obj.push_back(i)
3.删除 obj.pop_back()
4.个数 obj.size()
5.清除 obj.clear();
6.排序 sort(obj.begin(),obj.end());//从小到大
reverse(obj.begin(),obj.end());//从大到小
7.访问 数组 obj[i]
迭代器 vector<int>::iterator it;
for(it=obj.begin();it!=obj.end();it++)
{ cout<<*it<<" "; }
8.索引访问 .at() try catch 边界检查
void STLvectorOne(void)
{
std::cout << "STLvector(void)";
vector<int>obj; //1.创建
for (int i = 0; i < 10; i++)
{
obj.push_back(i);//2.添加
cout << obj[i] << ",";
}
vector<string> strv(10, "00");// 创建10个string "00"
for (int i = 0; i < 10; i++)
{
cout << strv[i] << ",";
}
cout << "\n";
for (int i = 0; i < 5; i++)
{
obj.pop_back();//3.删除
}
int m_size = obj.size();//4.个数
for (int i = 0; i < 10; i++)//push_back(elem)在数组最后添加数据
{
obj.push_back(i);
cout << obj[i] << ",";
}
obj.clear();//5.清除所有数据
for (int i = 0; i < obj.size(); i++)
{
cout << obj[i] << endl;
}
//6.排序 添加10个任意值
default_random_engine e;
uniform_int_distribution<unsigned> u(0, 9);//0-9之间的整数
for (size_t i = 0; i < 10; ++i)
{
int m_randomint = u(e);
obj.push_back(m_randomint);
cout << obj[i] << endl;
}
sort(obj.begin(), obj.end());//从小到大
cout << "从小到大:" << endl;
for (int i = 0; i < obj.size(); i++)
{
cout << obj[i] << ",";
}
cout << "\n" << endl;
cout << "从大到小:" << endl;
reverse(obj.begin(), obj.end());//从大到小
for (int i = 0; i < obj.size(); i++)
{
cout << obj[i] << ",";
}
//7.迭代器访问
vector<int>::iterator it;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素
for (it = obj.begin(); it != obj.end(); it++)
{
cout << *it << " ";
}
//8.索引访问
string str_value = strv.at(0);//at 边界检查的
cout << str_value << " ";
}
2.Vector二维数组
vector<vector<int> > array(m_column);
array[i].resize(m_row);
void STLvectorMutil(void)
{
int m_column = 2; //有几个数组 列 column
int m_row = 3; //每个数组的个数 行 row
vector<vector<int> > array(m_column); //
for (int i = 0; i < m_column; i++)//动态二维数组为5行6列,
{
array[i].resize(m_row); //初识化二维数组 值全为0
}
//1.数组赋值
array[0] = {1,2,3};
array[1] = {4,5,6};
/*
输出如下
1 2 3
4 5 6
*/
//2.数组赋值
for (int i = 0; i < m_column; i++)
{
for (int j = 0; j < m_row; j++)
{
array[i][j] = i;
}
}
/*
输出如下
0 0 0
1 1 1
*/
//3.数组赋值
for (int i = 0; i < m_column; i++)
{
for (int j = 0; j < m_row; j++)
{
array[i][j] = j + i * (m_column+1)+1;
}
}
/*
1 2 3
4 5 6
*/
for (int i = 0; i < m_column; i++)
{
for (int j = 0; j < m_row; j++)
{
cout << array[i][j] << "";
}
cout << "\n";
}
3.随机数
3.1rand()
srand()时间种子
rand()%n 表示 从0到n-1的数
[0,n) 就是
[a,b)的随机整数,使用(rand() % (b-a))+ a;
[a,b]的随机整数,使用(rand() % (b-a+1))+ a;
(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;
a + rand() % n;其中的a是起始值,n是整数的范围。
(a,b) :a + (int)b * rand() / (RAND_MAX + 1)。
(0~1) 浮点数,可以使用rand() / double(RAND_MAX)。
#include <cstdlib>
#include <ctime>
#define random_int(a,b) (rand()%(b-a)+a)
#define random_X(x) rand()%(x)
#define random_float(a,b) a + b* rand() /double (RAND_MAX + 1)
void myrandom()
{
srand((int)time(0)); // 没有这行,两次运行结果会是一样的。
for (int i = 0; i < 10; i++)
{
cout << random_int(10,100) << " ,"; //产生10到100之间的整数, 不包括10,100
cout << random_X(100) << " ,"; //产生0到100之间的整数,可以有0
cout << random_float(10,20) << " "; //10-20之间的浮点数
}
}
3.2 default_random_engine()
using std::uniform_int_distribution;//范围 分布
using std::default_random_engine;
using std::normal_distribution;
void STL_random(void)
{
//1.随机数 1-2147483646
default_random_engine e;
for (size_t i = 0; i < 10; ++i) //生成十个随机数
cout << e() << endl;
cout << "Min random:" << e.min() << endl; //输出该随机数引擎序列的范围
cout << "Max random:" << e.max() << endl;
//2.种子
//default_random_engine e; //或者直接在这里改变种子 e(10)
e.seed(10); //设置新的种子
for (size_t i = 0; i < 10; ++i)
cout << e() << endl;
cout << "Min random:" << e.min() << endl;
cout << "Max random:" << e.max() << endl;
//3.0-9随机数 没有大小顺序
// default_random_engine e;
uniform_int_distribution<unsigned> m_uint(0, 9);
for (size_t i = 0; i < 10; ++i)
cout << m_uint(e) << " ";
cout << endl;
//4.生成0.0-1.0的double
uniform_real_distribution<double> m_udouble(0, 1); //生成范围为0.0-1.0的随机数序列
for (size_t i = 0; i < 5; ++i)
cout << m_udouble(e) << " ";
cout << endl;
//5.正态分布,大部分生成的随机数落在0-8之间
// default_random_engine e;
vector<unsigned> vals(9);
normal_distribution<> n(4, 1.5); //
for (size_t i = 0; i != 200; ++i)
{
unsigned v = lround(n(e)); //舍入到最近整数
if (v < vals.size())
++vals[v]; //统计0-8数字出现的次数
}
for (decltype(vals.size()) i = 0; i != vals.size(); ++i)
cout << i << ": " << string(vals[i], '#') << endl;
}