#include <iostream>#include <vector>#include <algorithm>template <typename T>int copy_max(T& to, T& from, unsigned char delim, size_t limit){    typename T::iterator pos = std::find(from.begin(), from.end(), delim);    typename T::iterator prev_pos = pos;    if(pos == from.end())    {        return -1;    }    size_t index = std::distance(from.begin(), pos);    while(pos != from.end()     && index <= limit)    {        prev_pos = pos;        pos = std::find(pos + 1, from.end(), delim);        index = std::distance(from.begin(), pos);    }    index = std::distance(from.begin(), prev_pos);    if(index == 0)    {        return -1;    }    to.insert(to.begin(), from.begin(), prev_pos);    from.erase(from.begin(), prev_pos);    return index;}void output(const std::vector<unsigned char>& va, const std::vector<unsigned char>& vb ){    std::string str(va.begin(), va.end());    std::cout << To:  << str << std::endl;    str.assign(vb.begin(), vb.end());    std::cout << From:  << str << std::endl;  }static int case_num = 1;#define expect(a, b) \std::cout << [ << case_num ++ << ] ; \if(a == b) \std::cout << #a <<  ==  << #b <<  TRUE  << std::endl;\else {\std::cout << FAIL  << #a <<  !=  << #b <<  FALSE  << std::endl;\throw new int;\}int main() {    std::string b = Hi\x24The\x24HowAreYou\x24?;    std::vector<unsigned char> va;    std::vector<unsigned char> vb(b.begin(), b.end());    int index = copy_max(va, vb, '\x24', 5);    expect(index, 2);    output(va, vb);    va.clear();    index = copy_max(va, vb, '\x24', 5);    expect(index, 4);    va.clear();    index = copy_max(va, vb, '\x24', 5);    expect(index, -1);    return 0;}Don't mind the main() function and testing macros, this is just something I threw together in a matter of minutes.
Function to copy characters from one buffer to another given the maximum size
c++