ACCU2007
|home |print view |recent changes |changed April 11, 2007 |
exact
|You are 23.22.76.170 <- set your identity!
go to start recent
changes

Hubert told about the BigFour on the equation of productivity

He also set out some programming challenges:

  1. write a simple expression interpreter in C++ and observe what you are doing with respect to productivity
  2. solve a list of exercises using STL without loops


The last exercise included a challenge to print all possible anagrams of a word you type in. I implemented a solution using an iterator:

struct anaIter:public std::iterator<std::forward_iterator_tag,std::string>{
    std::string word;
    std::string anagram;
    anaIter(std::string w=std::string()):word(w){}

    std::string operator*(){
        if(anagram.size()!=word.size())anagram=word; // special first access
        return anagram;
    }
    anaIter& operator++() { 
        std::next_permutation(anagram.begin(),anagram.end()); return *this;
    }
    bool operator==(anaIter const &other)const{ 
	if (other.word.size())return anagram == other.anagram;
	else return anagram == word;
    }
    bool operator!=(anaIter const &other)const{ return !(*this == other);}
};

int main(){
	std::string word;
	std::cin >> word;
	std::copy(anaIter(word), anaIter(),
	          std::ostream_iterator<std::string>(std::cout,", "));
}


Last edited April 11, 2007 go to start recent
changes