support bazel complie this project and format code.
This commit is contained in:
48
learn_class/modern_cpp_30/container2/BUILD
Normal file
48
learn_class/modern_cpp_30/container2/BUILD
Normal file
@@ -0,0 +1,48 @@
|
||||
# please run `bazel run //learn_class/modern_cpp_30/container2:relacontainer`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/container2:priority_queue`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/container2:hash`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/container2:array`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/container2:unorder`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "relacontainer",
|
||||
srcs = ["relacontainer.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
deps = [
|
||||
"//learn_class/modern_cpp_30/container1:output_container",
|
||||
],
|
||||
)
|
||||
cc_binary(
|
||||
name = "priority_queue",
|
||||
srcs = ["priority_queue.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
deps = [
|
||||
"//learn_class/modern_cpp_30/container1:output_container",
|
||||
],
|
||||
)
|
||||
cc_binary(
|
||||
name = "hash",
|
||||
srcs = ["hash.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
deps = [
|
||||
"//learn_class/modern_cpp_30/container1:output_container",
|
||||
],
|
||||
)
|
||||
cc_binary(
|
||||
name = "array",
|
||||
srcs = ["array.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
deps = [
|
||||
"//learn_class/modern_cpp_30/container1:output_container",
|
||||
],
|
||||
)
|
||||
cc_binary(
|
||||
name = "unorder",
|
||||
srcs = ["unorder.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
deps = [
|
||||
"//learn_class/modern_cpp_30/container1:output_container",
|
||||
],
|
||||
)
|
42
learn_class/modern_cpp_30/container2/array.cpp
Normal file
42
learn_class/modern_cpp_30/container2/array.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
#include "../container1/output_container.h"
|
||||
#include <iostream>
|
||||
#include <map> // std::map
|
||||
|
||||
using namespace std;
|
||||
#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
|
||||
|
||||
void test(int a[8]) { cout << ARRAY_LEN(a) << endl; }
|
||||
|
||||
void test1(int arr[]) {
|
||||
// 不能编译
|
||||
// std::cout << std::size(arr)
|
||||
// << std::endl;
|
||||
}
|
||||
|
||||
typedef char mykey_t[8];
|
||||
typedef std::array<char, 8> mykey_t1;
|
||||
|
||||
int main() {
|
||||
int a[8];
|
||||
test(a);
|
||||
|
||||
// C++17 直接提供了一个 size 方法,可以用于提供数组长度,
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
std::cout << "The array length is " << std::size(arr) << std::endl;
|
||||
// 并且在数组退化成指针的情况下会直接失败
|
||||
test1(arr);
|
||||
|
||||
std::map<mykey_t, int> mp;
|
||||
mykey_t mykey{"hello"};
|
||||
// mp[mykey] = 5;
|
||||
// 轰,大段的编译错误
|
||||
|
||||
std::map<mykey_t1, int> mp1;
|
||||
mykey_t1 mykey1{"hello"};
|
||||
mp1[mykey1] = 5; // ok
|
||||
cout << mp1 << endl;
|
||||
}
|
37
learn_class/modern_cpp_30/container2/hash.cpp
Normal file
37
learn_class/modern_cpp_30/container2/hash.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
#include "../container1/output_container.h"
|
||||
#include <algorithm> // std::sort
|
||||
#include <functional> // std::less/greater/hash
|
||||
#include <iostream> // std::cout/endl
|
||||
#include <string> // std::string
|
||||
#include <vector> // std::vector
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
// 初始数组
|
||||
vector<int> v{13, 6, 4, 11, 29};
|
||||
cout << v << endl;
|
||||
|
||||
// 从小到大排序
|
||||
sort(v.begin(), v.end());
|
||||
cout << v << endl;
|
||||
|
||||
// 从大到小排序
|
||||
sort(v.begin(), v.end(), greater<int>());
|
||||
cout << v << endl;
|
||||
|
||||
cout << hex;
|
||||
|
||||
auto hp = hash<int *>();
|
||||
cout << "hash(nullptr) = " << hp(nullptr) << endl;
|
||||
cout << "hash(v.data()) = " << hp(v.data()) << endl;
|
||||
cout << "v.data() = " << static_cast<void *>(v.data()) << endl;
|
||||
|
||||
auto hs = hash<string>();
|
||||
cout << "hash(\"hello\") = " << hs(string("hello")) << endl;
|
||||
cout << "hash(\"hellp\") = " << hs(string("hellp")) << endl;
|
||||
}
|
26
learn_class/modern_cpp_30/container2/priority_queue.cpp
Normal file
26
learn_class/modern_cpp_30/container2/priority_queue.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
#include "../container1/output_container.h"
|
||||
#include <functional> // std::greater
|
||||
#include <iostream> // std::cout/endl
|
||||
#include <memory> // std::pair
|
||||
#include <queue> // std::priority_queue
|
||||
#include <vector> // std::vector
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
priority_queue<pair<int, int>, vector<pair<int, int>>,
|
||||
greater<pair<int, int>>>
|
||||
q;
|
||||
q.push({1, 1});
|
||||
q.push({2, 2});
|
||||
q.push({0, 3});
|
||||
q.push({9, 4});
|
||||
while (!q.empty()) {
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
}
|
||||
}
|
57
learn_class/modern_cpp_30/container2/relacontainer.cpp
Normal file
57
learn_class/modern_cpp_30/container2/relacontainer.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
#include "../container1/output_container.h"
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
set<int> s{1, 1, 1, 2, 3, 4};
|
||||
cout << s << endl;
|
||||
|
||||
multiset<int, greater<int>> ms{1, 1, 1, 2, 3, 4};
|
||||
cout << ms << endl;
|
||||
|
||||
map<string, int> mp{{"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}};
|
||||
|
||||
cout << mp << endl;
|
||||
|
||||
mp.insert({"four", 4});
|
||||
cout << mp << endl;
|
||||
|
||||
cout << (mp.find("four") == mp.end()) << endl;
|
||||
|
||||
cout << (mp.find("five") == mp.end()) << endl;
|
||||
|
||||
mp["five"] = 5;
|
||||
|
||||
cout << mp << endl;
|
||||
|
||||
multimap<string, int> mmp{{"one", 1}, {"two", 2}, {"three", 3}, {"four", 4}};
|
||||
|
||||
cout << mmp << endl;
|
||||
|
||||
mmp.insert({"four", -4});
|
||||
|
||||
cout << mmp << endl;
|
||||
|
||||
cout << (mp.find("four")->second) << endl;
|
||||
cout << (mp.lower_bound("four")->second) << endl;
|
||||
|
||||
cout << (mp.upper_bound("four")->second) << endl;
|
||||
cout << ((--mp.upper_bound("four"))->second) << endl;
|
||||
|
||||
multimap<string, int>::iterator lower, upper;
|
||||
std::tie(lower, upper) = mmp.equal_range("four");
|
||||
cout << (lower != upper) << endl; // 检测区间非空
|
||||
|
||||
cout << lower->second << endl;
|
||||
cout << (--upper)->second << endl;
|
||||
}
|
31
learn_class/modern_cpp_30/container2/unorder.cpp
Normal file
31
learn_class/modern_cpp_30/container2/unorder.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
#include "../container1/output_container.h"
|
||||
#include <complex> // std::complex
|
||||
#include <iostream> // std::cout/endl
|
||||
#include <unordered_map> // std::unordered_map
|
||||
#include <unordered_set> // std::unordered_set
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace std {
|
||||
|
||||
template <typename T> struct hash<complex<T>> {
|
||||
size_t operator()(const complex<T> &v) const noexcept {
|
||||
hash<T> h;
|
||||
return h(v.real()) + h(v.imag());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
int main() {
|
||||
unordered_set<int> s{1, 1, 2, 3, 5, 8, 13, 21};
|
||||
cout << s << endl;
|
||||
|
||||
unordered_map<complex<double>, double> umc{{{1.0, 1.0}, 1.4142},
|
||||
{{3.0, 4.0}, 5.0}};
|
||||
cout << umc << endl;
|
||||
}
|
Reference in New Issue
Block a user