- 列出目錄下所有檔案並用 grep 找出目標檔案
- 使用 for loop 爬過每個檔案名稱並使用 sed 得到另外一個名稱
程式碼:
1 2 3 4 5 6 7 8 | files=$(ls | grep '@2x.png$') for f in $files do echo $f g=$(echo $f | sed 's/@2x//g') echo $g done |
以上是核心概念,接下來再小修改程式滿足特定需求就可以了
1 2 3 4 5 6 7 8 | files=$(ls | grep '@2x.png$') for f in $files do echo $f g=$(echo $f | sed 's/@2x//g') echo $g done |
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 | #include <chrono> #include <ctime> #include <string> #include <iostream> std::string asString(const std::chrono::system_clock::time_point& tp) { constexpr bool blUseLocalTime = true; std::time_t t = std::chrono::system_clock::to_time_t(tp); std::string ts = blUseLocalTime ? std::ctime(&t) : std::asctime(gmtime(&t)); ts.resize(ts.size() - 1); return ts; } std::chrono::system_clock::time_point makeTimePoint (int year, int mon, int day, int hour, int min, int sec = 0) { struct std::tm t; t.tm_sec = sec; // second of minute (0 .. 59 and 60 for leap seconds) t.tm_min = min; // minute of hour (0 .. 59) t.tm_hour = hour; // hour of day (0 .. 23) t.tm_mday = day; // day of month (1 .. 31) t.tm_mon = mon - 1; // month of year (0 .. 11) t.tm_year = year - 1900; // year since 1900 t.tm_isdst = -1; // determine whether daylight saving time auto tt = std::mktime(&t); if (tt == -1) { throw "no valid system time"; } return std::chrono::system_clock::from_time_t(tt); } int main() { auto tp1 = makeTimePoint(2010, 01, 01, 00, 00); std::cout << asString(tp1) << std::endl; auto tp2 = makeTimePoint(2011, 05, 23, 13, 44); std::cout << asString(tp2) << std::endl; } |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <chrono> #include <iostream> #include <iomanip> #include <thread> #include <ctime> #include <string> #include <vector> #include <numeric> template <typename T> void printClockData() { using PeriodType = typename T::period; std::cout << " - precision: "; std::cout << static_cast<double>(PeriodType::num) / PeriodType::den << " seconds" << std::endl; std::cout << " - is_steady: " << std::boolalpha << T::is_steady << std::endl; } std::string asString(const std::chrono::system_clock::time_point& tp) { constexpr bool blUseLocalTime = false; std::time_t t = std::chrono::system_clock::to_time_t(tp); std::string ts = blUseLocalTime ? std::ctime(&t) : std::asctime(gmtime(&t)); ts.resize(ts.size() - 1); return ts; } volatile int sink; int main() { /* Clock */ std::cout << "system_clock: " << std::endl; printClockData<std::chrono::system_clock>(); std::cout << "\nsteady_clock: " << std::endl; printClockData<std::chrono::steady_clock>(); std::cout << "\nhigh_resolution_clock: " << std::endl; printClockData<std::chrono::high_resolution_clock>(); /* system_clock */ auto system_start = std::chrono::system_clock::now(); // do some heavy work here, more than five seconds using namespace std::chrono_literals; // std::this_thread::sleep_for(6s); if (std::chrono::system_clock::now() > system_start + std::chrono::seconds(5)) { // If someone adjust the clock, condition may produce "false" std::cout << "Checked!" << std::endl; } auto diffSec = std::chrono::system_clock::now() - system_start; auto sec = std::chrono::duration_cast<std::chrono::milliseconds>(diffSec); std::cout << "This program runs: " << sec.count() << " milliseconds" << std::endl; /* steady_clock */ for (auto size = 1ull; size < 1000000000ull; size *= 100) { auto start = std::chrono::steady_clock::now(); std::vector<int> Vec(size, 123); sink = std::accumulate(Vec.begin(), Vec.end(), 0u); // make sure it's a side effect auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> diffSec = end - start; std::cout << "Time to fill and iterate a vector of " << size << " ints : " << diffSec.count() << " s\n"; } /* Time Point */ std::chrono::system_clock::time_point tp; auto sysClockNow = std::chrono::system_clock::now(); auto sysClockMin = std::chrono::system_clock::time_point::min(); auto sysClockMax = std::chrono::system_clock::time_point::max(); std::cout << "epoch: " << asString(tp) << std::endl; std::cout << "current time: " << asString(sysClockNow) << std::endl; std::cout << "minimum time: " << asString(sysClockMin) << std::endl; std::cout << "maximum time: " << asString(sysClockMax) << std::endl; /* Timepoint and Duration Arithmetic */ using TypeDays = std::chrono::duration<int, std::ratio<3600 * 24>>; std::chrono::time_point<std::chrono::system_clock> timePoint; std::cout << "epoch: " << asString(tp) << std::endl; tp += TypeDays(1) + std::chrono::hours(23) + std::chrono::minutes(55); auto diff = tp - std::chrono::system_clock::time_point(); auto minutesDiff = std::chrono::duration_cast<std::chrono::minutes>(diff); TypeDays daysDiff = std::chrono::duration_cast<TypeDays>(diff); std::cout << "later: " << asString(tp) << std::endl; std::cout << "diff: " << minutesDiff.count() << " minute(s)" << std::endl; std::cout << "diff: " << daysDiff.count() << " day(s)" << std::endl; tp -= std::chrono::hours(24 * 365); std::cout << "-1 year: " << asString(tp) << std::endl; tp -= std::chrono::duration<int, std::ratio<3600 * 24 * 365>>(50); std::cout << "-50 years: " << asString(tp) << std::endl; tp -= std::chrono::duration<int, std::ratio<3600 * 24 * 365>>(505000); std::cout << "-505000 years: " << asString(tp) << std::endl;// underflow } |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <chrono> #include <ratio> #include <iostream> #include <iomanip> template <typename T> class TD; template <typename _Value, typename _Fraction> std::ostream& operator << (std::ostream& os, const std::chrono::duration<_Value, _Fraction>& dur) { os << "[" << dur.count() << " of " << _Fraction::num << "/" << _Fraction::den << " sec]"; return os; } int main() { /* Basic Representation */ std::chrono::duration<int> twentySec(20); // value: 20, fraction: 1 sec (default) std::chrono::duration<double, std::ratio<60>> halfMin(0.5); // value: 0.5, fraction: 60 sec std::chrono::duration<long, std::milli> oneMs(1); // value: 1, fraction: 1 milli sec /* Use Helper Class */ std::chrono::seconds twentySec2(20); std::chrono::hours oneDay(24); std::chrono::microseconds oneMs2(1); //std::chrono::minutes halfMin2(0.5);// compile error, the duration type is int /* Arithmetic */ std::chrono::seconds d1(42); std::chrono::milliseconds d2(10); auto diff = d1 - d2; std::cout << diff.count() << std::endl; // 41990 (of 1/1000 sec) std::chrono::duration<int, std::ratio<1, 3>> d3(1); std::chrono::duration<int, std::ratio<1, 5>> d4(1); auto add = d3 + d4; std::cout << add.count() << std::endl; // 8 (of 1/15 sec) std::cout << std::boolalpha << (d3 < d4) << std::endl;// false /* Arithmetic Return Type */ //TD<decltype(diff)> x;// compiler deduce: TD<std::chrono::duration<__int64,std::milli>> //TD<decltype(add)> x;// compiler deduce: TD<std::chrono::duration<int,std::ratio<0x01,0x0f>>> //TD<decltype(twentySec2 - oneDay)> x;// compiler deduce: TD<std::chrono::duration<__int64,std::ratio<0x01,0x01>>> //TD<decltype(twentySec2 - oneMs2)> x;// compiler deduce: TD<std::chrono::duration<__int64,std::micro>> /* Arithmetic and Conversion */ std::chrono::milliseconds ms(0); // undefined value without (0) ms += twentySec2 + oneDay; --ms; ms *= 2; std::cout << ms.count() << " ms" << std::endl;//172839998 ms std::chrono::nanoseconds ns(ms); std::cout << ns.count() << " ns" << std::endl;// 172839998000000 ns /* Static Operations */ auto zeroMS = std::chrono::milliseconds::zero();// duration with zero value auto maxMS = std::chrono::milliseconds::max();// duration with max value auto minMS = std::chrono::milliseconds::min();// duration with min value using TypeMsValue = std::chrono::milliseconds::rep; using TypeMsFraction = std::chrono::milliseconds::period; std::cout << twentySec << std::endl; // use overloaded operator << std::cout << d3 << std::endl; // use overloaded operator << /* Explicit Conversion */ // example 1: convert to different fraction std::chrono::seconds sec(55); //std::chrono::minutes m1 = sec; // compile-time error std::chrono::minutes m1 = std::chrono::duration_cast<std::chrono::minutes>(sec); std::cout << m1 << std::endl; // value 0 due to information lost // example 2: conver to different value type, double to long long halfMin;// type: std::chrono::duration<double, std::ratio<60>> //std::chrono::seconds s1 = halfMin; // compile-time error std::chrono::seconds s1 = std::chrono::duration_cast<std::chrono::seconds>(halfMin); std::cout << s1 << std::endl; // [30 of 1/1 sec] // example 3: segment into different units ms = std::chrono::milliseconds(7255042); std::chrono::hours hh = std::chrono::duration_cast<std::chrono::hours>(ms); std::chrono::minutes mm = std::chrono::duration_cast<std::chrono::minutes>(ms % std::chrono::hours(1)); std::chrono::seconds ss = std::chrono::duration_cast<std::chrono::seconds>(ms % std::chrono::minutes(1)); std::chrono::milliseconds msec = std::chrono::duration_cast<std::chrono::milliseconds>(ms % std::chrono::seconds(1)); std::cout << "raw: " << hh << "::" << mm << "::" << ss << "::" << msec << std::endl; std::cout << " " << std::setfill('0') << std::setw(2) << hh.count() << "::" << std::setw(2) << mm.count() << "::" << std::setw(2) << ss.count() << "::" << std::setw(2) << msec.count() << std::endl; } |
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
| #include <iostream>
#include <ratio>
#include <cstdint>
int main()
{
/* Representation */
using FiveThirds = std::ratio < 5, 3 > ;
std::cout << FiveThirds::num << "/" << FiveThirds::den << std::endl; // 5/3
using AlsoFiveThirds = std::ratio < 25, 15 >;
std::cout << AlsoFiveThirds::num << "/" << AlsoFiveThirds::den << std::endl; // 5/3
std::ratio<42, 42> one;
std::cout << one.num << "/" << one.den << std::endl; // 1/1
using Zero = std::ratio<0>; // denominator default 1
Zero zero;
std::cout << zero.num << "/" << zero.den << std::endl; // 0/1
using Neg = std::ratio < 7, -3 >;
std::cout << Neg::num << "/" << Neg::den << std::endl; // -7/3
/* Arithmetics: meta functions */
using Addtype = std::ratio_add<FiveThirds, AlsoFiveThirds>::type;
std::cout << Addtype::num << "/" << Addtype::den << std::endl; // 10/3
std::cout << std::boolalpha
<< std::ratio_equal<FiveThirds, AlsoFiveThirds>::value << std::endl; // true
//std::ratio_multiply < std::ratio<1, INTMAX_MAX>, std::ratio<1, 2> >; // compile error
//std::ratio_divide<FiveThirds, Zero>; // compile error
//std::ratio_divide<FiveThirds, Zero>::type; // compile error
/* Predefined units */
using NanoType = std::ratio<1, 1000000000LL>::type;
std::cout << std::boolalpha << std::ratio_less<std::nano, NanoType>::value << std::endl;// false
std::cout << std::boolalpha << std::ratio_less<NanoType, std::nano>::value << std::endl;// false
std::cout << std::boolalpha << std::ratio_equal<std::nano, NanoType>::value << std::endl;// true
}
|
#include <cstdint>
namespace std
{
template <intmax_t N, intmax_t D = 1>
struct ratio {
static_assert(D != 0, "denominator cannot be zero");
static_assert(N >= -INTMAX_MAX && D >= -INTMAX_MAX, "out of range");
static constexpr intmax_t num = ...;
static constexpr intmax_t den = ...;
using type = ratio < num, dem > ;
};
}
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 | #include <type_traits> #include <iostream> template <typename T> struct add_const_ref { typedef const T& type; }; template <typename T> struct add_const_ref<T&> { typedef T& type; }; template <typename T> struct add_const_ref2 : std::conditional<std::is_reference<T>::value, T, const T&> { }; int main() { std::cout << std::is_same<add_const_ref<int&>::type, int&>::value << std::endl; std::cout << std::is_same<add_const_ref<int>::type, const int&>::value << std::endl; std::cout << std::is_same<add_const_ref2<int&>::type, int&>::value << std::endl; std::cout << std::is_same<add_const_ref2<int>::type, const int&>::value << std::endl; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> /* Primary template */ template <typename T> struct IsPointer { static const bool value = false; }; /* Partial Specialization */ template <typename T> struct IsPointer<T*> { static const bool value = true; }; int main() { std::cout << "IsPointer<int *>::value = " << IsPointer<int *>::value << std::endl; std::cout << "IsPointer<int>::value = " << IsPointer<int>::value << std::endl; } |
#include <random>
#include <iostream>
#include <vector>
#include <sstream>
//#define BETTER_SOLUTION
//#define OK_SOLUTION
class Resource {
public:
Resource() : pResource(new int(count++)) {}
~Resource() {}
static Resource create() {
Resource *rsc = new Resource();
}
void destroy() {
std::random_device rd;
std::default_random_engine dre(rd());
std::uniform_real_distribution<> urd(0, 1);
if (urd(rd) >= 0.95) {
std::ostringstream oStream;
oStream << "Exception from dtor. Resource " << *pResource << " leaked.";
throw std::runtime_error(oStream.str().c_str());
}
std::cout << "Release Resource " << *pResource << std::endl;
delete pResource;
}
int getResource() const { return *pResource; }
private:
int *pResource;
static int count;
};
int Resource::count = 0;
class ResourceHolder {
public:
ResourceHolder() {
#ifdef BETTER_SOLUTION
released = false;
#endif
}
~ResourceHolder() {
#if defined(BETTER_SOLUTION)
if (!released) {
try {
// Release resource if the client didn't
pResource.destroy();
} catch (std::exception &e) {
// If closing fails, log that and terminate or swallow
std::cout << "Holder cought a failure: "<< e.what() << std::endl;
// Optional.
// std::abort();
}
}
#elif defined(OK_SOLUTION)
try {
pResource.destroy();
} catch (std::exception &e) {
// Make a log that something failed.
std::cout << "Holder cought a failure: "<< e.what() << std::endl;
// Optional.
// std::abort();
}
#else
pResource.destroy();
#endif
}
#ifdef BETTER_SOLUTION
void release() {// For client code
pResource.destroy();
released = true;
}
#endif
int resource() const { return pResource.getResource(); }
friend std::ostream& operator <<(std::ostream &, const ResourceHolder&);
private:
Resource pResource;
#ifdef BETTER_SOLUTION
bool released;
#endif
};
std::ostream& operator <<(std::ostream &os, const ResourceHolder& w)
{
os << "Do something on Resource " << w.resource();
return os;
}
void createResourceAndTestLeave()
{
std::vector<ResourceHolder> v(50);
for (const auto& w : v) {
std::cout << w << std::endl;
}
#ifdef BETTER_SOLUTION
for (auto& w : v) {
try {
w.release();
} catch (std::exception &e) {
std::cout << "Client caught exception: " << e.what() << std::endl;
}
}
#endif
}
int main()
{
createResourceAndTestLeave();
}
std::vector<ResourceHolder> v(50);
ResourceHolder::~ResourceHolder() {
try {
pResource.destroy();
}
catch (std::exception &e) {
// Make a log that something failed.
std::cout << "Holder cought a failure: " << e.what() << std::endl;
// Optional.
// std::abort();
}
}
ResourceHolder::~ResourceHolder() {
if (!released) {
try {
// Release resource if the client didn't
pResource.destroy();
}
catch (std::exception &e) {
// If closing fails, log that and terminate or swallow
std::cout << "Holder cought a failure: " << e.what() << std::endl;
// Optional.
// std::abort();
}
}
}
void ResourceHolder::release() {// For client code
pResource.destroy();
released = true;
}