PartD 将会使用visual studio 2017 作为开发环境进行测试
tuple元组类型
这个类型非常像swift的元组类型,以下几个例子就可以理解tuple的作用了。
int main()
{
//初始化一个tuple
tuple<int, double, long, string> tupleA(12, 20.5, 100L, "TONY");
//通过make_tuple创建一个tuple
auto tupleB = make_tuple("TONYA", 'a', 88);
//使用get模板类获得tupleA_1 第0个元素,并使用auto推测其类型
auto tupleA_1 = get<0>(tupleA);
cout << tupleA_1 << endl;
//定义tupleB 模板类实体的类型别名
typedef decltype(tupleB) trans;
//通过tuple_size模板类获得tupleB对应的模板实例的元素数量
auto target_size = tuple_size<trans>::value;
cout << target_size << endl;
//通过tuple_element模板类获得tupleB模板实例的第1个元素类型
tuple_element<1, trans>::type tupleB_1 = get<1>(tupleB);
cout << tupleB_1 << endl;
}
其中tuple的关系运算符==、!=、>、< 均和容器的运算规则相同
使用tuple的场景通常为返回多个不同的元素:
typedef tuple<double, int, string, char> new_tmp_type;
new_tmp_type new_function();
int main()
{
auto result = new_function();
cout << get<0>(result) << endl;
cout << get<1>(result) << endl;
cout << get<2>(result) << endl;
cout << get<3>(result) << endl;
}
new_tmp_type new_function() {
return { 10.5,25,string("TONY"),'a'};
}
bitset由于使用场景不多,就不作描述了,主要提供很多用于位运算的方便操作,详细可以参考C++primer(第5版)640页
随机数:
在新标准的C++中,已经不使用基于C的rand作为随机数生成的唯一方法,我们可以使用随机数引擎和分布,其中随机数引擎是主要生成随机数的类,而随机数分布是控制随机生成的根据随机数引擎返回指定类型的、在给定范围内的、服从特定概率分布的随机数。
以下是一个简单的随机数生成方案:
int main() {
shared_ptr<vector<unsigned>> vector_p = getRandomVector();
for each (auto item in *vector_p)
{
cout << item << endl;
}
string hold;
cin >> hold;
}
shared_ptr<vector<unsigned>> getRandomVector() {
default_random_engine e;
shared_ptr<vector<unsigned>> vector_p(new vector<unsigned>());
for (int i = 0; i < 10; i++) {
vector_p->push_back(e());
}
return vector_p;
}
我们只使用了default_random_engine随机数引擎,生成了范围10个e.min() 到 e.max()的随机数。
我们使用随机数分布限定返回随机数0-9的范围:
shared_ptr<vector<unsigned>> getRandomVector() {
default_random_engine e;
uniform_int_distribution<unsigned> u(0, 9);
shared_ptr<vector<unsigned>> vector_p(new vector<unsigned>());
for (int i = 0; i < 10; i++) {
vector_p->push_back(u(e));
}
return vector_p;
}
但是我们发现我们每次生成的随机数都是一致的,如果我们要连续生成随机数我们需要将:
default_random_engine e;
uniform_int_distribution<unsigned> u(0, 9);
我们需要将上述两个变量定义为static,否则如果我们连续使用该方法生成的随机数都是一样的,相关代码如下:
int main() {
shared_ptr<vector<unsigned>> vector_pA = getRandomVector();
shared_ptr<vector<unsigned>> vector_pB = getRandomVector();
cout << (*vector_pA == *vector_pB) << endl; //返回true
string hold;
cin >> hold;
}
shared_ptr<vector<unsigned>> getRandomVector() {
default_random_engine e;
shared_ptr<vector<unsigned>> vector_p(new vector<unsigned>());
for (int i = 0; i < 10; i++) {
vector_p->push_back(e());
}
return vector_p;
}
如果我们没有将随机发生器(随机数引擎和分布类型的集合)定义为static则每次都是一样的数据内容,我们需要将随机发生器定义为static则程序会记录其生成状态,此时连续调用getRandomVector就会产生两个不一样的随机vector,相关代码如下:
int main() {
shared_ptr<vector<unsigned>> vector_pA = getRandomVector();
shared_ptr<vector<unsigned>> vector_pB = getRandomVector();
cout << (*vector_pA == *vector_pB) << endl; //返回false
string hold;
cin >> hold;
}
shared_ptr<vector<unsigned>> getRandomVector() {
static default_random_engine e;
static uniform_int_distribution<unsigned> u(0, 9);
shared_ptr<vector<unsigned>> vector_p(new vector<unsigned>());
for (int i = 0; i < 10; i++) {
vector_p->push_back(u(e));
}
return vector_p;
}
我们可以使用uniform_read_distribution分布随机类生成浮点随机数:
int main() {
shared_ptr<vector<double>> vector_pA = getRandomVector();
for_each((*vector_pA).begin(), (*vector_pA).end(), [](const double &item) {
cout << item << endl;
});
string hold;
cin >> hold;
}
shared_ptr<vector<double>> getRandomVector() {
static default_random_engine e;
static uniform_real_distribution<double> u(0, 1);
shared_ptr<vector<double>> vector_p(new vector<double>());
for (int i = 0; i < 10; i++) {
vector_p->push_back(u(e));
}
return vector_p;
}
由于我们每次生成随机都是一样的,唯一影响随时的是随机种子,我们可以在随机数引擎构造函数中设定seed(种子),也可以通过seed()函数进行设置:
int main() {
shared_ptr<vector<unsigned>> vector_pA = getRandomVector(10);
shared_ptr<vector<unsigned>> vector_pB = getRandomVector(10);
cout << (*vector_pA == *vector_pB) << endl; //种子相同随机数相同(返回true)
shared_ptr<vector<unsigned>> vector_pC = getRandomVector(11);
cout << (*vector_pA == *vector_pC) << endl; //种子不同随机数不同(返回false)
string hold;
cin >> hold;
}
shared_ptr<vector<unsigned>> getRandomVector(long seed) {
default_random_engine e(seed);
uniform_int_distribution<unsigned> u(0, 1);
shared_ptr<vector<unsigned>> vector_p(new vector<unsigned>());
for (int i = 0; i < 10; i++) {
vector_p->push_back(u(e));
}
return vector_p;
}
同样可以使用e.seed(seed)方式进行设置随机种子。
生成随机BOOL
int main() {
default_random_engine e;
bernoulli_distribution u;
if (u(e)) {
cout << "true" << endl;
}
else {
cout <<