How Can I Avoid Repetitive Programming

Started by
34 comments, last by GameDev.net 18 years, 1 month ago
this may be a bit dangerous but I think this is what you suggested?
#include <map>#include <string>typedef std::multimap<std::string, int> mymap;typedef mymap::const_reverse_iterator myiter;int main(int argc, const char** argv){	mymap multimap_order;myiter const_it_replacement_begin = multimap_order.rbegin();myiter const_it_replacement_end = multimap_order.rend();myiter const_it = multimap_order.rbegin();for ( ; ; ){	if ( const_it != const_it_replacement_end ){	}	else{		break;	}	++const_it;}	return 0;}


and instead of const_it != const_it_replacement_end
I tried
const_it < const_it_replacement_end
but got the same error.

Is the above code the best I can optimize this situation?
Advertisement
full driver test

#include <map>#include <string>#include <iostream>using namespace std;int nocase_cmp( const string &, const string&);struct case_insensitive_test{  bool operator()(const string & s1, const string& s2) const  {    return nocase_cmp(s1, s2) < 0;  }};typedef std::multimap<std::string, int, case_insensitive_test> mymap;typedef mymap::const_reverse_iterator myiter;int nocase_cmp(const string & s1, const string& s2) {  string::const_iterator it1=s1.begin();  string::const_iterator it2=s2.begin();  //stop when either string's end has been reached  while ( (it1!=s1.end()) && (it2!=s2.end()) )   {     if(::toupper(*it1) != ::toupper(*it2)) //letters differ?     // return -1 to indicate smaller than, 1 otherwise      return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;     //proceed to the next character in each string    ++it1;    ++it2;  }  size_t size1=s1.size(), size2=s2.size();// cache lengths   //return -1,0 or 1 according to strings' lengths    if (size1==size2)       return 0;    return (size1<size2) ? -1 : 1;}int main(int argc, const char** argv){mymap multimap_order;multimap_order.insert(pair<string, int>("ABC", 324));multimap_order.insert(pair<string, int>("abc", 122));multimap_order.insert(pair<string, int>("ABC", 324));multimap_order.insert(pair<string, int>("gds", 523));myiter const_it_replacement_begin = multimap_order.rbegin();myiter const_it_replacement_end = multimap_order.rend();myiter const_it = multimap_order.rbegin();for ( ; ; ){	if ( const_it != const_it_replacement_end ){		cout << (*const_it).first;	}	else{		break;	}	++const_it;}	return 0;}


I feel this can definately be better!
but i'm a noob and don't know how to make it better, please any point outs or recommendations?
Couldn't he just define 2 different functions and use a functor ?

IF(YES)
function = fct_A;
ELSE
function = fct_B;

call function;

( I'm really not sure of my solution since I've only read about functors yesterday ;) )
Original post by Bregma
Quote:Original post by Tradone
Okay, the problem is that the compiler is picking up the non-const rend() member function of the map. It seems it's a bug in the version of GCC you're using: your original code compiles without a problem with GCC 4.1.
I'm sure that I've actually had that problem with rbegin for const_reverse_iterators in VS2002, before.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
Quote:Original post by Bregma
Okay, the problem is that the compiler is picking up the non-const rend() member function of the map. It seems it's a bug in the version of GCC you're using: your original code compiles without a problem with GCC 4.1.
I'm sure that I've actually had that problem with rbegin for const_reverse_iterators in VS2002, before.


On further investigation, this is DR #280 (a defect in the C++ standard itself). The standards committee have adopted an accepted solution, and it looks like GCC 4.1 and later versions of MSVC have adopted the fix.

It seems the cause of the problem was that comparison operators for certain iterators were specified as member functions, so there is no implicit conversion between const_reverse_iterators and reverse_iterators. If Tradone had put his rend() on the left of the comparison operator, it would have worked. The defect resolution is to make the comparison operators non-member functions.

Let that be a lesson: operators should not be member functions.

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Bregma
If Tradone had put his rend() on the left of the comparison operator, it would have worked.
Nope, that doesn't work in VC2003 at least. It resolves rend() to non-const version and complains again about missing operator.

This topic is closed to new replies.

Advertisement