mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
revision #1: 更新第二章中已维护的代码
This commit is contained in:
8
exercises/2/fold.expresion.cpp
Normal file
8
exercises/2/fold.expresion.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <iostream>
|
||||
template<typename ... T>
|
||||
auto average(T ... t) {
|
||||
return (t + ... ) / sizeof...(t);
|
||||
}
|
||||
int main() {
|
||||
std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
|
||||
}
|
||||
22
exercises/2/structured.binding.cpp
Normal file
22
exercises/2/structured.binding.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
template <typename Key, typename Value, typename F>
|
||||
void update(std::map<Key, Value>& m, F foo) {
|
||||
for (auto&& [key, value] : m ) value = foo(key);
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::map<std::string, long long int> m {
|
||||
{"a", 1},
|
||||
{"b", 2},
|
||||
{"c", 3}
|
||||
};
|
||||
update(m, [](std::string key) -> long long int {
|
||||
return std::hash<std::string>{}(key);
|
||||
});
|
||||
for (auto&& [key, value] : m)
|
||||
std::cout << key << ":" << value << std::endl;
|
||||
}
|
||||
0
exercises/2/variadic.template.parameter.pack.cpp
Normal file
0
exercises/2/variadic.template.parameter.pack.cpp
Normal file
Reference in New Issue
Block a user