User:Mikero

From Cppwiki

Jump to: navigation, search
CHANGE This user has CHANGE calc privileges
calc
#C++ This user is a EFNet #C++ channel operator
Operator

My name is Michael Goldshteyn. I have been on the #c++ channel, under the nandle mikero, since the early nineties and have been an op for well over a decade. I have been programming, daily, since I was about 13 years old, back in 1983. My first programming language was BASIC, from which I slowly transitioned to C in 1985. Around 1989, I discovered C++ and have been actively programming in it, since. My favorite scripting language is Perl, which I have been using since the late nineties. The choice of Perl was primarily due to the ease with which one can solve a scripting problem by utilizing a module or modules from CPAN.

I am currently employed in the financial sector, doing Windows C++ development. My interests include programming, photography, and watching as much TV as family life allows me. I am married and have two sons, Joshua (5 y.o.) and Benjamin (2 y.o.).

Here is my obligatory "Hello, world" code:

Code
#include <algorithm>
#include <iostream>
#include <string>

int main()
{
  std::string s("\ndlrow ,olleH");
  
  std::copy(s.rbegin(),s.rend(),std::ostream_iterator<char>(std::cout));
}
Output
Hello, world



Contents

Potential C++ Standard contributions

Here are some algorithms I was hoping would make their way into the next revision of the C++ Standard (See section 4.3 of: 14 crazy ideas for the standard library in C++0x), along with implementations written by me:

is_sorted

Code
template <class ForwardIterator>
bool is_sorted(ForwardIterator first, ForwardIterator last)
{
  if (first!=last)
    for (ForwardIterator it(first++);first!=last;++it /* or it=first */,++first)
      if (*first<*it)
        return false;

  return true;
}

template <class ForwardIterator, class Predicate>
bool is_sorted(ForwardIterator first, ForwardIterator last,
               Predicate pred)
{
  if (first!=last)
    for (ForwardIterator it(first++);first!=last;++it /* or it=first */,++first)
      if (!pred(*it,*first))
        return false;

  return true;
}

nonunique_copy

Code
template<typename InputIterator, typename OutputIterator>
OutputIterator nonunique_copy(InputIterator first1, InputIterator last1, OutputIterator first2)
{
  if (first1!=last1) // Test for empty range
    for (InputIterator prev(first1++);first1!=last1;++prev,++first1)
      if (*first1==*prev) // Test if current pair is not unique
        {
        *first2++=*first1; // Store in output sequence
        for (;;) // Find next set of distinct elements 
          {
          prev=first1++;
          if (first1==last1) // End of input range hit, done
            return first2;
          if (!(*first1==*prev)) // Next non-unique pair found, skip and look for next dupe
            break;
          }
        }

  return first2;
}

template<typename InputIterator, typename OutputIterator, typename BinaryPredicate>
OutputIterator nonunique_copy(InputIterator first1, InputIterator last1, OutputIterator first2, BinaryPredicate pred)
{
  if (first1!=last1) // Test for empty range
    for (InputIterator prev(first1++);first1!=last1;++prev,++first1)
      if (pred(*first1,*prev)) // Test if current pair is not unique
        {
        *first2++=*first1; // Store in output sequence
        for (;;) // Find next set of distinct elements 
          {
          prev=first1++;
          if (first1==last1) // End of input range hit, done
            return first2;
          if (!pred(*first1,*prev)) // Next non-unique pair found, skip and look for next dupe
            break;
          }
        }

  return first2;
}
Sample code highlighting usage of nonunique_copy
Code
#include <algorithm>
#include <iostream>
#include <vector>
/* #include header containing nonunique_copy */

int main()
{	
  using namespace std; // For brevity in this example

  int nums[]={1,2,2,3,4,4,4,4,4,5};
  vector<int> nonUniqueNums; // set<int> could have been used instead, since the resulting elements are unique
  
  nonunique_copy(nums,nums+sizeof(nums)/sizeof(int),back_inserter(nonUniqueNums));
  
  copy(nonUniqueNums.begin(),nonUniqueNums.end(),ostream_iterator<int>(cout,","));
  cout << '\n';  
}
Output
2,4,


Other useful class templates

select1st and select2nd - some useful class templates for pair element isolation

Code
#include <functional>

template <typename pair_type>
struct select1st: public std::unary_function<const pair_type &, typename const pair_type::first_type &>
{
  const typename pair_type::first_type &operator()(const pair_type &v) const
  {
    return v.first;
  }
};

template <typename pair_type>
struct select2nd: public std::unary_function<const pair_type &, typename const pair_type::second_type &>
{
  const typename pair_type::second_type &operator()(const pair_type &v) const
  {
    return v.second;
  }
};
Sample code highlighting usage of select1st and select2nd
Code
#include <algorithm>
#include <boost/assign/list_of.hpp>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
// #include header containing select1st and select2nd

int main()
{
  using namespace std; // For brevity in this example

  // Create a map of ordinal values to their names, with three associations
  typedef map<int,string> IntStringMap;
  IntStringMap ordinalNamesMap(boost::assign::map_list_of
    (1,string("1st"))
    (2,string("2nd"))
    (3,string("3rd")));

  // Display all keys in order
  transform(
    ordinalNamesMap.begin(),
    ordinalNamesMap.end(),
    ostream_iterator<IntStringMap::key_type>(cout,","),
    select1st<IntStringMap::value_type>());
  cout << '\n';

  // Display all values, in key order
  transform(
    ordinalNamesMap.begin(),
    ordinalNamesMap.end(),
    ostream_iterator<IntStringMap::mapped_type>(cout,","),
    select2nd<IntStringMap::value_type>());
  cout << '\n';
}
Output
1,2,3,
1st,2nd,3rd,
Sample code converted to use boost::bind instead of select1st and select2nd
Code
#include <algorithm>
#include <boost/assign/list_of.hpp>
#include <boost/bind.hpp>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <string>

int main()
{
  using namespace std; // For brevity in this example

  // Create a map of ordinal values to their names, with three associations
  typedef map<int,string> IntStringMap;
  IntStringMap ordinalNamesMap(boost::assign::map_list_of
    (1,string("1st"))
    (2,string("2nd"))
    (3,string("3rd")));

  // Display all keys in order
  transform(
    ordinalNamesMap.begin(),
    ordinalNamesMap.end(),
    ostream_iterator<IntStringMap::key_type>(cout,","),
    boost::bind(&IntStringMap::value_type::first,_1));
  cout << '\n';

  // Display all values, in key order
  transform(
    ordinalNamesMap.begin(),
    ordinalNamesMap.end(),
    ostream_iterator<IntStringMap::mapped_type>(cout,","),
    boost::bind(&IntStringMap::value_type::second,_1));
  cout << '\n';
}
Output
1,2,3,
1st,2nd,3rd,