Changeset 3466


Ignore:
Timestamp:
Feb 10, 2016, 4:39:38 AM (6 years ago)
Author:
Peter
Message:

closes #842. Document how to combine compose_f_gx_hy, PairFirst?, and std::less to accomplish a functor that compares pairs ignore .second. Also include such an example in the test.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility.cc

    r3417 r3466  
    7878void test_compose_functors(test::Suite& suite);
    7979void test_dereferencer(test::Suite& suite);
     80void test_ticket842(test::Suite& suite);
    8081
    8182int main(int argc, char* argv[])
     
    230231  test_rename(suite);
    231232  test_replace(suite);
     233  test_ticket842(suite);
    232234
    233235  return suite.return_value();
     
    757759    suite.err() << "error: " << s << "\n";
    758760}
     761
     762
     763void test_ticket842(test::Suite& suite)
     764{
     765  suite.out() << "test_ticket842\n";
     766  std::vector<std::pair<std::string, int> > data;
     767  data.push_back(std::make_pair("Orange", 2));
     768  data.push_back(std::make_pair("Orange", 1));
     769  data.push_back(std::make_pair("Apple", 10));
     770
     771  typedef utility::PairFirst<const std::pair<std::string, int> > PF;
     772  typedef std::less<std::string> strLess;
     773  utility::compose_f_gx_hy<strLess, PF, PF> compare;
     774  // stable sort should shuffle 'Apple' first, and then keep the order
     775  // of the equaivalent 'Orange' elements, i.e., <Orange, 2> should stay
     776  // before <Orange, 1>.
     777  std::stable_sort(data.begin(), data.end(), compare);
     778  if (!suite.add(data[0].first == "Apple"))
     779    suite.err() << "incorrect data[0].first: " << data[0].first << "\n";
     780  if (!suite.add(data[1].first == "Orange"))
     781    suite.err() << "incorrect data[1].first: " << data[1].first << "\n";
     782  if (!suite.add(data[1].second == 2))
     783    suite.err() << "incorrect data[1].second: " << data[1].second << "\n";
     784}
  • trunk/yat/utility/stl_utility.h

    r3417 r3466  
    160160
    161161     \see compose_f_gxy, compose_f_gx, and compose_f_gx_hx
     162
     163     Here is an example using the class to construct a functor that
     164     compares std::pairs ignoring \c second.
     165
     166     \code
     167     vector<pair<string, foo> > data;
     168     data.push_back(make_pair("Orange", foo(2));
     169     data.push_back(make_pair("Orange", foo(1));
     170     data.push_back(make_pair("Apple", foo(10));
     171     typedef PairFirst<const pair<string, foo> > PF;
     172     compose_f_gx_hy<less<string>, PF, PF> compare;
     173     sort(data.begin(), data.end(), compare);
     174     \endcode
     175
    162176   */
    163177  template<class F, class G, class H>
Note: See TracChangeset for help on using the changeset viewer.