
Given: typedef std::deque<char> DequeString; std::string s("Hello sweet world!"); std::string word("sweet"); DequeString dequeString; // Copy "Hello sweet world!" into the deque string std::copy(s.begin(),s.end(),std::back_inserter(dequeString)); // Now, I want to find where "sweet" appears in the dequeString. An iterator to its location would be fine. // This doesn't quite do it, since it looks for _any_ occurrence of a character from word in dequeString std::find_first_of(dequeString.begin(),dequeString.end(),word.begin(),word.end()); // But, I want to find where _the whole_ word occurs in dequeString, similar to how std::string::find() does it: std::string::size_type wordPos=s.find(word); Is there something in boost or the standard C++ lib that can be used? Thanks, Michael Goldshteyn

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Michael Goldshteyn wrote: | Given: | | typedef std::deque<char> DequeString; | std::string s("Hello sweet world!"); | std::string word("sweet"); | DequeString dequeString; | | // Copy "Hello sweet world!" into the deque string | std::copy(s.begin(),s.end(),std::back_inserter(dequeString)); | | // Now, I want to find where "sweet" appears in the dequeString. An iterator | to its location would be fine. | | // This doesn't quite do it, since it looks for _any_ occurrence of a | character from word in dequeString | std::find_first_of(dequeString.begin(),dequeString.end(),word.begin(),word.end()); | | // But, I want to find where _the whole_ word occurs in dequeString, similar | to how std::string::find() does it: | std::string::size_type wordPos=s.find(word); | | Is there something in boost or the standard C++ lib that can be used? | std::search sounds like what you are looking for :) Note that most implementations don't specialise search for characters or anything, an in general it's impossible to get any better than O((n-m)m) where n is the length of the string you are looking in, and m is the length of the string you are looking for... a more specialised algorithm that just looked for strings in strings could do better than that.. Chris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (MingW32) iD8DBQFCQuAa3FpDzErifpIRArdiAJsFJyS1N5H3l0SBjkBDleTEvnlypACgmX70 xBLOexamYHtjUAG9BMhk6is= =mTYV -----END PGP SIGNATURE-----

On Thu, Mar 24, 2005 at 09:27:51AM -0600, Michael Goldshteyn wrote:
Given:
typedef std::deque<char> DequeString; std::string s("Hello sweet world!"); std::string word("sweet"); DequeString dequeString;
// Copy "Hello sweet world!" into the deque string std::copy(s.begin(),s.end(),std::back_inserter(dequeString));
// Now, I want to find where "sweet" appears in the dequeString. An iterator to its location would be fine.
// This doesn't quite do it, since it looks for _any_ occurrence of a character from word in dequeString std::find_first_of(dequeString.begin(),dequeString.end(),word.begin(),word.end());
// But, I want to find where _the whole_ word occurs in dequeString, similar to how std::string::find() does it: std::string::size_type wordPos=s.find(word);
Is there something in boost or the standard C++ lib that can be used?
std::search() jon

On Thu, Mar 24, 2005 at 09:27:51AM -0600, Michael Goldshteyn wrote:
Given:
typedef std::deque<char> DequeString; std::string s("Hello sweet world!"); std::string word("sweet"); DequeString dequeString;
// Copy "Hello sweet world!" into the deque string std::copy(s.begin(),s.end(),std::back_inserter(dequeString));
// Now, I want to find where "sweet" appears in the dequeString. An iterator to its location would be fine.
// This doesn't quite do it, since it looks for _any_ occurrence of a character from word in dequeString std::find_first_of(dequeString.begin(),dequeString.end(),word.begin(),word.end());
// But, I want to find where _the whole_ word occurs in dequeString, similar to how std::string::find() does it: std::string::size_type wordPos=s.find(word);
Is there something in boost or the standard C++ lib that can be used?
Thanks,
Michael Goldshteyn
Hi Michael, You should have a look into The String Algorithm Library. Algorithm find_first() does exactly what you want. It works with any sequential std container. For documentation see: http://www.meta-comm.com/engineering/resources/cs-win32_metacomm/doc/html/st... Specificaly the find_first algorith: http://www.meta-comm.com/engineering/resources/cs-win32_metacomm/doc/html/fi... Regards, Pavol

Thanks for all the responses guys, with regards to std::search. Especially, thanks to Pavol for pointing me to the find_first algorithm that is part of the boost algorithms. It looks very interesting! :) Michael Goldshteyn
participants (4)
-
Chris Jefferson
-
Jonathan Wakely
-
Michael Goldshteyn
-
Pavol Droba