#include
#include
#include
#include
//accept an array, print it's elems
template
void Print (const T (&array)[M]) { for (const auto &elem : array) cout << elem << "\n"; } //accept a container, find a value template
T Find (T beg_it, T end_it, U value) { bool flag = false; for (; beg_it != end_it; ++beg_it) if (*beg_it == value) { flag = true; break; } return flag ? beg_it : end_it; } //check if we found the value template
void Check (T container, U value) { auto result = Find (container.begin (), container.end (), value); cout << ((result == container.end ()) ? "Cannot find it!" : "We find it!") << endl; } int main () { std::ios::sync_with_stdio (false); vector
ivec{ 1, 34, 32, 31 }; list
slst{"fuck", "you"}; Check(ivec, 34); Check(slst, "she"); string sArray[] = { "fuck", "you", "very much" }; int iArray[] = {12, 32, 343, 22}; Print (iArray); Print (sArray); return 0; }
简单模版的使用
最新推荐文章于 2022-07-03 10:51:50 发布