prepare for c++17

This commit is contained in:
Changkun Ou
2018-03-26 09:08:36 +02:00
parent 8a3eb8f271
commit 71025d8bc6
39 changed files with 28 additions and 11 deletions

35
code/4/4.1.cpp Normal file
View File

@@ -0,0 +1,35 @@
//
// 4.1.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// std::array
#include <iostream>
#include <array>
void foo(int *p, int len) {
for (int i = 0; i != len; ++i) {
std::cout << p[i] << std::endl;
}
}
int main() {
std::array<int, 4> arr= {1,4,3,2};
//int len = 4;
//std::array<int, len> arr = {1,2,3,4}; // 非法, 数组大小参数必须是常量表达式
// C 风格接口传参
// foo(arr, arr.size()); // 非法, 无法隐式转换
foo(&arr[0], arr.size());
foo(arr.data(), arr.size());
// 更多接口使用
std::sort(arr.begin(), arr.end());
for(auto &i : arr)
std::cout << i << std::endl;
return 0;
}

36
code/4/4.2.cpp Normal file
View File

@@ -0,0 +1,36 @@
//
// 4.2.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// 无序容器
#include <iostream>
#include <string>
#include <unordered_map>
#include <map>
int main() {
// 两组结构按同样的顺序初始化
std::unordered_map<int, std::string> u = {
{1, "1"},
{3, "3"},
{2, "2"}
};
std::map<int, std::string> v = {
{1, "1"},
{3, "3"},
{2, "2"}
};
// 分别对两组结构进行遍历
std::cout << "std::unordered_map" << std::endl;
for( const auto & n : u)
std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
std::cout << std::endl;
std::cout << "std::map" << std::endl;
for( const auto & n : v)
std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
}

77
code/4/4.3.cpp Normal file
View File

@@ -0,0 +1,77 @@
//
// 4.3.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// std::tuple 及其操作
#include <tuple>
#include <iostream>
#include <boost/variant.hpp>
auto get_student(int id)
{
if (id == 0)
return std::make_tuple(3.8, 'A', "张三");
if (id == 1)
return std::make_tuple(2.9, 'C', "李四");
if (id == 2)
return std::make_tuple(1.7, 'D', "王五");
// 返回类型被推断为 std::tuple<double, char, std::string>
return std::make_tuple(0.0, 'D', "null");
}
template <size_t n, typename... T>
boost::variant<T...> _tuple_index(size_t i, const std::tuple<T...>& tpl) {
if (i == n)
return std::get<n>(tpl);
else if (n == sizeof...(T) - 1)
throw std::out_of_range("越界.");
else
return _tuple_index<(n < sizeof...(T)-1 ? n+1 : 0)>(i, tpl);
}
template <typename... T>
boost::variant<T...> tuple_index(size_t i, const std::tuple<T...>& tpl) {
return _tuple_index<0>(i, tpl);
}
template <typename T>
auto tuple_len(T &tpl) {
return std::tuple_size<T>::value;
}
int main()
{
auto student = get_student(0);
std::cout << "ID: 0, "
<< "GPA: " << std::get<0>(student) << ", "
<< "成绩: " << std::get<1>(student) << ", "
<< "姓名: " << std::get<2>(student) << '\n';
double gpa;
char grade;
std::string name;
// 元组进行拆包
std::tie(gpa, grade, name) = get_student(1);
std::cout << "ID: 1, "
<< "GPA: " << gpa << ", "
<< "成绩: " << grade << ", "
<< "姓名: " << name << '\n';
std::tuple<std::string, double, double, int> t("123", 4.5, 6.7, 8);
std::cout << std::get<std::string>(t) << std::endl;
// std::cout << std::get<double>(t) << std::endl; // 非法, 引发编译期错误
std::cout << std::get<3>(t) << std::endl;
// 拼接元组
auto new_tuple = std::tuple_cat(get_student(1), std::move(t));
// 迭代
for(int i = 0; i != tuple_len(new_tuple); ++i)
// 运行期索引
std::cout << tuple_index(i, new_tuple) << std::endl;
}