Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
std::search is an STL
algorithm for searching a sequence of elements
in containers.
It is similar to std::find, except that std::find searches for a single element.
Example
Assume you work with a std::vector of integers
in which two sequences are forbidden: zero-zero and one-zero. With std::search
these sequences can be used to find if our exemplary std::vector is valid.
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
//From http://www.richelbilderbeek.nl/CppSeperateString.htm
const std::vector<std::string> SeperateString (
const std::string& input,
const std::string& seperator)
{
std::vector<std::string> v;
typedef std::string::const_iterator Iterator;
const Iterator end = input.end();
Iterator i1 = input.begin();
{
//Copy until first comma
Iterator i2 = std::search(i1,end,seperator.begin(), seperator.end());
assert(i1!=i2);
std::string s;
std::copy(i1,i2, std::back_inserter(s));
v.push_back(s);
i1 = i2;
}
while (i1!=end)
{
//Copy from comma to (comma || end)
Iterator i2 = std::search(i1 + 1,end,seperator.begin(), seperator.end());
assert(i1!=i2);
assert(i2 == end || std::equal(seperator.begin(),seperator.end(),i2));
std::string s;
std::copy(i1+1,i2, std::back_inserter(s));
v.push_back(s);
i1 = i2;
}
return v;
}
|
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
