板凳——————————————————c++(77)

    技术2024-07-24  16

    #include #include #include #include using namespace std::filesystem; int SequentialSearch (int *a, const int n, const int x){ int i ; for( i = 0; i < n; i++){ if(a[i] == x) return i; } if (i == n) return -1; }

    struct Foo { int mInt; std::string mStr; bool mBool; };

    bool operator<(const Foo& f1, const Foo& f2) { return std::tie(f1.mInt, f1.mStr, f1.mBool) < std::tie(f2.mInt, f2.mStr, f2.mBool); }

    class Foo2 { public: Foo2(std::string str, int i) : mStr(str), mInt(i) {}

    private: std::string mStr; int mInt; };

    int add(int a, int b) { return a + b; }

    void processPath1(const path& p) { if (!exists§) { return; }

    auto begin = recursive_directory_iterator(p); auto end = recursive_directory_iterator(); for (auto iter = begin; iter != end; ++iter) { const std::string spacer(iter.depth() * 2, ' '); auto& entry = *iter; if (is_regular_file(entry)) { std::cout << spacer << "File: " << entry; std::cout << " (" << file_size(entry) << " bytes)" << std::endl; } else if (is_directory(entry)) { std::cout << spacer << "Dir: " << entry << std::endl; } }

    }

    void processPath2(const path& p, size_t level = 0) { if (!exists§) { return; }

    const std::string spacer(level * 2, ' '); if (is_regular_file(p)) { std::cout << spacer << "File: " << p; std::cout << " (" << file_size(p) << " bytes)" << std::endl; } else if (is_directory(p)) { std::cout << spacer << "Dir: " << p << std::endl; for (auto& entry : directory_iterator(p)) { processPath2(entry, level + 1); } }

    }

    int main(){ int m [] {2, 4, 6, 8, 0, 1, 3, 5, 7, 9}; int result; int num = 7; result = SequentialSearch(m, 10, num); if(result == -1) std::cout << “No found!” << std::endl; else std::cout << “in m[” << result << "] found “<< num << " !” << std::endl;

    std::tuple<int, std::string, bool> t1(16, "Test", true); //tuple t1(16, "Test"s, true); // C++17 template argument deduction for constructors auto[i, str, b] = t1; std::cout << "Decomposed: i = " << i << ", str = \"" << str << "\", b = " << b << std::endl; { int i = 0; std::string str; bool b = false; std::cout << "Before: i = " << i << ", str = \"" << str << "\", b = " << b << std::endl; std::tie(i, str, b) = t1; std::cout << "After: i = " << i << ", str = \"" << str << "\", b = " << b << std::endl; } { int i = 0; bool b = false; std::cout << "Before: i = " << i << ", b = " << b << std::endl; std::tie(i, std::ignore, b) = t1; std::cout << "After: i = " << i << ", b = " << b << std::endl; } std::tuple<double, std::string> t2(3.14, "string 2"); auto t3 = std::tuple_cat(t1, t2); std::cout << std::get<0>(t3) << std::endl; std::cout << std::get<1>(t3) << std::endl; std::cout << std::get<2>(t3) << std::endl; std::cout << std::get<3>(t3) << std::endl; std::cout << std::get<4>(t3) << std::endl; std::tuple<int, std::string> t5(123, "def"); std::tuple<int, std::string> t4(123, "abc"); if (t5 < t4) { std::cout << "t5 < t4" << std::endl; } else { std::cout << "t5 >= t4" << std::endl; } Foo f1{ 42, "Hello", 0 }; Foo f2{ 32, "World", 0 }; std::cout << (f1 < f2) << std::endl; std::cout << (f2 < f1) << std::endl; auto myTuple = std::make_tuple("Hello world.", 42); auto foo = std::make_from_tuple<Foo2>(myTuple); std::cout << std::apply(add, std::make_tuple(39, 3)) << std::endl; // 42

    //professional c++ 4th edition p 483 std::filesystem::__cxx11::path p1(LR"(D:\Foo\Bar)"); std::filesystem::__cxx11::path p2(L"D:/Foo/Bar"); std::cout << p1 << std::endl; //“D:\Foo\Bar” std::cout << p2 << std::endl; //“D:/Foo/Bar”

    std::filesystem::__cxx11::path p3(L"D:\\Foo"); p3.append("Bar"); p3 /= "Bar"; std::cout << p3 << std::endl; //"D:\\Foo/Bar/Bar" std::filesystem::__cxx11::path p4(L"D:\\Foo"); p4.concat("Bar"); p4 += "Bar"; std::cout << p4 << std::endl; //"D:\\FooBarBar" std::filesystem::__cxx11::path p5(LR"(C:\Foo\Bar)"); for (const auto& component : p5) { std::cout << component << std::endl; //"C:\\Foo\\Bar" } std::filesystem::__cxx11::path myPath(L"c:/windows/win.ini"); directory_entry dirEntry(myPath); if (dirEntry.exists() && dirEntry.is_regular_file()) { std::cout << "File size: " << dirEntry.file_size() << std::endl; }

    // space_info s2 = space(“c:\”); // std::cout << "Capacity: " << s2.capacity << std::endl; // std::cout << "Free: " << s2.free << std::endl;

    std::filesystem::__cxx11::path p6(LR"(D:\Foo\Bar)"); processPath1(p6); std::filesystem::__cxx11::path p7(LR"(D:\Foo\Bar)"); processPath2(p7); return 0;

    } /* wannian07@wannian07-PC:~$ g++ -std=c++17 -o c13 c13.cpp -pthread c13.cpp: 在函数‘int SequentialSearch(int*, int, int)’中: c13.cpp:11:3: 警告:在有返回值的函数中,控制流程到达函数尾 [-Wreturn-type] 11 | } | ^ wannian07@wannian07-PC:~$ ./c13 in m[8] found 7 !

    Decomposed: i = 16, str = “Test”, b = 1

    Before: i = 0, str = “”, b = 0 After: i = 16, str = “Test”, b = 1 Before: i = 0, b = 0 After: i = 16, b = 1

    16 Test 1 3.14 string 2

    t5 >= t4

    0 1

    42

    “D:\Foo\Bar” “D:/Foo/Bar”

    “D:\Foo/Bar/Bar”

    “D:\FooBarBar”

    */

    Processed: 0.016, SQL: 9