update
This commit is contained in:
53
learn_class/modern_C++_30/container2/array.cpp
Normal file
53
learn_class/modern_C++_30/container2/array.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <map> // std::map
|
||||
#include "../container1/output_container.h"
|
||||
|
||||
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;
|
||||
}
|
46
learn_class/modern_C++_30/container2/hash.cpp
Normal file
46
learn_class/modern_C++_30/container2/hash.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
|
||||
#include <algorithm> // std::sort
|
||||
#include <functional> // std::less/greater/hash
|
||||
#include <iostream> // std::cout/endl
|
||||
#include <string> // std::string
|
||||
#include <vector> // std::vector
|
||||
#include "../container1/output_container.h"
|
||||
|
||||
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;
|
||||
}
|
30
learn_class/modern_C++_30/container2/priority_queue.cpp
Normal file
30
learn_class/modern_C++_30/container2/priority_queue.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
|
||||
#include <functional> // std::greater
|
||||
#include <iostream> // std::cout/endl
|
||||
#include <memory> // std::pair
|
||||
#include <queue> // std::priority_queue
|
||||
#include <vector> // std::vector
|
||||
#include "../container1/output_container.h"
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
73
learn_class/modern_C++_30/container2/relacontainer.cpp
Normal file
73
learn_class/modern_C++_30/container2/relacontainer.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include "../container1/output_container.h"
|
||||
#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;
|
||||
|
||||
}
|
41
learn_class/modern_C++_30/container2/unorder.cpp
Normal file
41
learn_class/modern_C++_30/container2/unorder.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by light on 19-12-16.
|
||||
//
|
||||
|
||||
|
||||
#include <complex> // std::complex
|
||||
#include <iostream> // std::cout/endl
|
||||
#include <unordered_map> // std::unordered_map
|
||||
#include <unordered_set> // std::unordered_set
|
||||
#include "../container1/output_container.h"
|
||||
|
||||
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