各种奇怪的程序备份

flaribbit · 更新于 2022-04-13 · 0 次阅读

军备竞赛之 c++ 实现 enumerate

和锤哥的一次军备竞赛,实现方法比较暴力,但居然还挺好用

cpp
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename T>
class enumerate {
   private:
    T& obj;
    using iter_t = decltype(obj.begin());
    using item_t = decltype(*obj.begin());

   public:
    enumerate(T& obj) : obj(obj) {}
    class iterator {
       public:
        size_t i;
        iter_t iter;
        iterator(iter_t iter) : i(0), iter(iter) {}
        iterator& operator++() {
            i++;
            iter++;
            return *this;
        }
        pair<size_t, item_t> operator*() {
            return {i, *iter};
        }
        bool operator!=(const iterator& other) {
            return iter != other.iter;
        }
    };
    iterator begin() {
        return iterator(obj.begin());
    }
    iterator end() {
        return iterator(obj.end());
    }
};

int main() {
    vector<int> nums = {1, 1, 4, 5, 1, 4};
    for (auto&& [i, num] : enumerate(nums)) {
        cout << i << " " << num << endl;
    }
    vector<string> words = {"hello", "world"};
    for (auto&& [i, word] : enumerate(words)) {
        cout << i << " " << word << endl;
    }
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

c++ 自带的编码转换

放在这里备份一下 当模板了

cpp
#include <string>
#include <codecvt>  // for std::codecvt_utf8
#include <locale>   // for std::wstring_convert
#include <cstdio>
#ifdef _WIN32
#include <windows.h>
#endif

int main() {
#ifdef _WIN32
    SetConsoleOutputCP(65001);
#endif
    std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> u32conv;
    std::string u8string = "你好世界";
    std::u32string unicode_string = u32conv.from_bytes(u8string);
    std::string u8string2 = u32conv.to_bytes(unicode_string);
    for (auto c : unicode_string) printf("%x ", c);
    printf("%s\n", u8string2.c_str());
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

缺啥补啥

待补充

cpp
#include <iostream>
#include <vector>

#include "co/unitest.h"
#include "co/os.h"

template <typename S, typename T>
S& operator<<(S& output, std::vector<T>& vec) {
    output << "[";
    bool first = true;
    for (T& x : vec) {
        if (!first) output << ", ";
        output << x;
        first = false;
    }
    return output << "]";
}

DEF_test(fun) {
    DEF_case(compare vector) {
        std::vector<int> v1{6, 5, 4};
        std::vector<int> v2{6, 5};
        EXPECT_EQ(v1, v2);
    }
}

int main(int argc, char* argv[]) {
    flag::init(argc, argv);
    unitest::run_all_tests();
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31