Click here to Skip to main content
12,768,125 members (44,684 online)
Click here to Skip to main content


26 bookmarked
Posted 19 Apr 2007

find_first_of: A performance pitfall among the STL algorithms

, 18 Jun 2007 CPOL
This article is discussing the performance problems found in the most notable find_first_of implementations and suggests useful improvements and workarounds.
 *  Copyright (c) 2007
 *  Jim Xochellis
 *  Permission to use, copy, modify, distribute and sell this software for any purpose
 *  is hereby granted without fee, provided that the above copyright notice appears in
 *  all copies and that both that copyright notice and this permission notice appear in
 *  supporting documentation. Jim Xochellis makes no representations about the suitability
 *  of this software for any purpose. It is provided "as is" without express or implied
 *  warranty.


#include <memory.h>

namespace dpx

template<typename _InputIterator, typename _ForwardIterator>
_InputIterator find_first_of(_InputIterator __first1, _InputIterator __last1,
					_ForwardIterator __first2, _ForwardIterator __last2)
	unsigned char __MapArray[256];
	memset(__MapArray, 0, 256);

	for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
		__MapArray[(unsigned char) *__iter] = 1;

	for ( ; __first1 != __last1; ++__first1)
		if (__MapArray[(unsigned char) *__first1] != 0)
			return __first1;

	return __last1;

} //namespace dpx

#endif //__DPX_DPX_FIND_FIRST_OF_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jim Xochellis
Software Developer
Greece Greece
I live in Greece with my wife and our two daughters. I am a professional software developer since 1999, using mostly C/C++ in my work.

My main expertise are: C/C++, STL, software optimization, generic programming and debugging. I am also very experienced in client–server programming, communications, concurrent programming, software security and cryptography. Finally, in my early professional years, I have worked a lot on cross-platform programming (Mac+Win).

I am familiar with the MFC, wxWidgets and Cplat GUI frameworks and the Python, Java, Pascal, Fortran, Prolog and Rexx programming languages.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 18 Jun 2007
Article Copyright 2007 by Jim Xochellis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid