book: translation of ch04

Update #12
This commit is contained in:
Changkun Ou
2019-07-19 19:17:16 +02:00
parent bf6b09253b
commit 7776a59e97
9 changed files with 412 additions and 69 deletions

View File

@@ -1,36 +0,0 @@
//
// 4.1.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// 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;
}

View File

@@ -0,0 +1,68 @@
//
// 4.1.linear.container.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <iostream>
#include <array>
#include <vector>
void foo(int *p, int len) {
for (int i = 0; i != len; ++i) {
std::cout << p[i] << std::endl;
}
}
int main() {
std::vector<int> v;
std::cout << "size:" << v.size() << std::endl; // output 0
std::cout << "capacity:" << v.capacity() << std::endl; // output 0
// As you can see, the storage of std::vector is automatically managed and
// automatically expanded as needed.
// But if there is not enough space, you need to redistribute more memory,
// and reallocating memory is usually a performance-intensive operation.
v.push_back(1);
v.push_back(2);
v.push_back(3);
std::cout << "size:" << v.size() << std::endl; // output 3
std::cout << "capacity:" << v.capacity() << std::endl; // output 4
// The auto-expansion logic here is very similar to Golang's slice.
v.push_back(4);
v.push_back(5);
std::cout << "size:" << v.size() << std::endl; // output 5
std::cout << "capacity:" << v.capacity() << std::endl; // output 8
// As can be seen below, although the container empties the element,
// the memory of the emptied element is not returned.
v.clear();
std::cout << "size:" << v.size() << std::endl; // output 0
std::cout << "capacity:" << v.capacity() << std::endl; // output 8
// Additional memory can be returned to the system via the shrink_to_fit() call
v.shrink_to_fit();
std::cout << "size:" << v.size() << std::endl; // output 0
std::cout << "capacity:" << v.capacity() << std::endl; // output 0
std::array<int, 4> arr= {1,4,3,2};
//int len = 4;
//std::array<int, len> arr = {1,2,3,4}; // illegal, size of array must be constexpr
// C style parameter passing
// foo(arr, arr.size()); // illegal, cannot convert implicitly
foo(&arr[0], arr.size());
foo(arr.data(), arr.size());
// more usage
std::sort(arr.begin(), arr.end());
for(auto &i : arr)
std::cout << i << std::endl;
return 0;
}

View File

@@ -1,11 +1,11 @@
//
// 4.2.cpp
// 4.2.unordered.map.cpp
// chapter 04 containers
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// 无序容器
#include <iostream>
#include <string>
@@ -13,7 +13,7 @@
#include <map>
int main() {
// 两组结构按同样的顺序初始化
// initialized in same order
std::unordered_map<int, std::string> u = {
{1, "1"},
{3, "3"},
@@ -25,7 +25,7 @@ int main() {
{2, "2"}
};
// 分别对两组结构进行遍历
// iterates in the same way
std::cout << "std::unordered_map" << std::endl;
for( const auto & n : u)
std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";

View File

@@ -1,11 +1,11 @@
//
// 4.3.cpp
// 4.3.tuples.cpp
// chapter 04 containers
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// std::tuple 及其操作
#include <tuple>
@@ -15,19 +15,19 @@
auto get_student(int id)
{
if (id == 0)
return std::make_tuple(3.8, 'A', "张三");
return std::make_tuple(3.8, 'A', "John");
if (id == 1)
return std::make_tuple(2.9, 'C', "李四");
return std::make_tuple(2.9, 'C', "Jack");
if (id == 2)
return std::make_tuple(1.7, 'D', "王五");
// 返回类型被推断为 std::tuple<double, char, std::string>
return std::make_tuple(1.7, 'D', "Ive");
// return type is std::tuple<double, char, std::string>
return std::make_tuple(0.0, 'D', "null");
}
template <size_t n, typename... T>
constexpr std::variant<T...> _tuple_index(const std::tuple<T...>& tpl, size_t i) {
if constexpr (n >= sizeof...(T))
throw std::out_of_range("越界.");
throw std::out_of_range("out of range.");
if (i == n)
return std::variant<T...>{ std::in_place_index<n>, std::get<n>(tpl) };
return _tuple_index<(n < sizeof...(T)-1 ? n+1 : 0)>(tpl, i);
@@ -52,32 +52,32 @@ 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';
<< "GPA: " << std::get<0>(student) << ", "
<< "Grade: " << std::get<1>(student) << ", "
<< "Name: " << std::get<2>(student) << '\n';
double gpa;
char grade;
std::string name;
// 元组进行拆包
// tuple unpack
std::tie(gpa, grade, name) = get_student(1);
std::cout << "ID: 1, "
<< "GPA: " << gpa << ", "
<< "成绩: " << grade << ", "
<< "姓名: " << name << '\n';
<< "GPA: " << gpa << ", "
<< "Grade: " << grade << ", "
<< "Name: " << 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<double>(t) << std::endl; // illegal, runtime error
std::cout << std::get<3>(t) << std::endl;
// 拼接元组
// concat
auto new_tuple = std::tuple_cat(get_student(1), std::move(t));
// 迭代
// iteration
for(int i = 0; i != tuple_len(new_tuple); ++i) {
std::cout << tuple_index(new_tuple, i) << std::endl; // 运行期索引
std::cout << tuple_index(new_tuple, i) << std::endl; // runtime indexing
}
}

14
code/4/Makefile Normal file
View File

@@ -0,0 +1,14 @@
#
# modern cpp tutorial
#
# created by changkun at changkun.de
# https://github.com/changkun/modern-cpp-tutorial
#
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))
%.out: %.cpp Makefile
clang++ $< -o $@ -std=c++2a -pedantic
clean:
rm *.out