I would like to try to answer this question as it relates to your request on function you said
" I ask because I want to cope with it better, and the best way to cope with something is to know what it is really there for." which means you are looking to change ...
you are asking about change and coping IMO
Coping: Coping with thoughts about other women is shown in behavioural research to be very common, most married monogamous men and women report having these thoughts, just because science in terms of evolutionary biology, says it exists and serves a purpose. If you feel like mating everything with in a 5 yard radius you may have a high labido this can be due to imbalances in a diet, meds, mood. Seek medical advice. If you have mild to lust here are some ideas about not succumbing to it...being a man or a women lust wreaks havoc. I should know, I am a someone who is guilty of it, and live with that regret. So I am speaking from personal experience, and can tell you that I have not only repented of this sin but taken very SERIOUS steps to not let it happen again. Being 44 years of age I have lived long enough to speak to this. Lol.
Disclaimer: These ideas are from my training as a Acceptance and Commitment (ACT) Therapist. So this type of therapy is used in treatment of unwanted behaviours.
So think about it this way,
You value your wife, and you value beautiful women. Your actions towards your wife is based on your values i.e she is beautiful and so you are attracted to her. You also value being sexually pure in thoughts and actions.
Values based actions are not only in line with aligning with Hashem's desires for your life but values based action is also scientifically proved to help you not AVOID things... (see ACT Therapy and any research by Dr Steven Hayes)
If you are having lust or thoughts that are not according to your values, to cope with that you can do the following according to the "ACT model" of living a values based life
Scientifically proven approach to recover from habitual unwanted behavior
Focus on what is important and make small steps towards committed action. ie. when you think of other women, notice that-and refocus your mind towards moving towards what you value and whats important, ie, living a life with habits and actions pleasing and honouring to Hashem
If you are using images of other women as an avoidance mechanism- ie. feeling lonley or stressed or not being fulfilled notice perhaps how you feel when you start to think lust full thoughts..are your tired? are you lonely? When notice that you can choose to t focus on committed action towards whats important-can take action-REST or call someone etc and notice how this improves your moves a way from whats most important to you.
In my opinion its about being VERY CLEAR on values clarification, and noticing when you are not living according to your values and then making committed action to correct the thought, action or behaviour.
THis is based on values based research from contextualscience.org. or Steven Hayes or Dr Russ Harris work mainly if you want to read more about it.
If you want to see how ACT can help with lust also this link can help which is Russ's ideas on not getting "hooked" by these thoughts
https://www.actmindfully.com.au/upimages/Choice_Point_2.0_A_Brief_Overview_-_Russ_Harris_April_2017.pdf
I would like to end my saying I am glad you asked this question, as it is something we all struggle with, and it takes courage to ask this!
Warm Regards,
Ray Frank