fork download
  1. #include <algorithm>
  2. #include <functional>
  3. #include <cstddef>
  4. #include <cstring>
  5.  
  6. #define NS aslib
  7. // #define NS std
  8. // ↑ユーザー定義型用の完全特殊化以外をstd名前空間に記述するのは本当は×
  9.  
  10. #define RUN
  11.  
  12. namespace {
  13. template<typename Itr>
  14. Itr find_detail_for_char2darray(Itr begin, Itr end, const char *target) {
  15. return std::find_if(begin, end,
  16. std::not1(std::bind2nd(std::ptr_fun(std::strcmp), target))
  17. );
  18. }
  19. }
  20.  
  21. namespace NS {
  22. template<std::size_t N>
  23. const char (*find(const char (*begin)[N], const char (*end)[N], const char *target))[N] {
  24. return ::find_detail_for_char2darray(begin, end, target);
  25. }
  26.  
  27. #if NS==std
  28. // std下に置く場合は、以下の特殊化の定義も必要となる
  29.  
  30. template<std::size_t N>
  31. char (*find(char (*begin)[N], char (*end)[N], const char *target))[N] {
  32. return find_detail_for_char2darray(begin, end, target);
  33. }
  34.  
  35. template<std::size_t N>
  36. const char (*find(const char (*begin)[N], const char (*end)[N], char *target))[N] {
  37. return find_detail_for_char2darray(begin, end, target);
  38. }
  39.  
  40. template<std::size_t N>
  41. char (*find(char (*begin)[N], char (*end)[N], char *target))[N] {
  42. return find_detail_for_char2darray(begin, end, target);
  43. }
  44. #endif
  45. }
  46.  
  47. #ifdef RUN
  48. #include <cassert>
  49. int main()
  50. {
  51. const char month[][4] = {
  52. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  53. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  54. };
  55. const std::size_t monthNum = sizeof(month) / sizeof(month[0]);
  56.  
  57. char wday[][4] = {
  58. "Sun", "Mon", "Tue", "Web", "Thu", "Fri", "Sat"
  59. };
  60. const std::size_t wdayNum = sizeof(wday) / sizeof(wday[0]);
  61.  
  62. char target[4];
  63.  
  64. assert(NS::find(month, month + monthNum, "Jan") == &month[0]);
  65. assert(NS::find(month, month + monthNum, "XXX") == month + monthNum);
  66.  
  67. assert(NS::find(wday, wday + wdayNum, "Sun") == &wday[0]);
  68.  
  69. std::strcpy(target, "Feb");
  70. assert(NS::find(month, month + monthNum, target) == &month[1]);
  71.  
  72. std::strcpy(target, "Mon");
  73. assert(NS::find(wday, wday + wdayNum, target) == &wday[1]);
  74. }
  75. #endif
  76.  
Success #stdin #stdout 0s 2720KB
stdin
Standard input is empty
stdout
Standard output is empty