In temptations against chastity, the spiritual masters advise us, not so much to contend with the bad thought, as to turn the mind to some spiritual, or, at least, indifferent object. It is useful to combat other bad thoughts face to face, but not thoughts of impurity.
Sermons of St. Alphonsus Liguori: For All the Sundays of the Year (ed. TAN Books, 2012) - ISBN: 9781505103038