Function to copy characters from one buffer to another given the maximum size #include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;


template &lt;typename T&gt;
int copy_max(T&amp; to, T&amp; 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() 
    &amp;&amp; index &lt;= 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&lt;unsigned char&gt;&amp; va, const std::vector&lt;unsigned char&gt;&amp; vb )
{
    std::string str(va.begin(), va.end());
    std::cout &lt;&lt; "To: " &lt;&lt; str &lt;&lt; std::endl;
    str.assign(vb.begin(), vb.end());
    std::cout &lt;&lt; "From: " &lt;&lt; str &lt;&lt; std::endl;  
}
static int case_num = 1;
#define expect(a, b) \
std::cout &lt;&lt; "[" &lt;&lt; case_num ++ &lt;&lt; "] "; \
if(a == b) \
std::cout &lt;&lt; #a &lt;&lt; " == " &lt;&lt; #b &lt;&lt; " TRUE " &lt;&lt; std::endl;\
else {\
std::cout &lt;&lt; "FAIL " &lt;&lt; #a &lt;&lt; " != " &lt;&lt; #b &lt;&lt; " FALSE " &lt;&lt; std::endl;\
throw new int;\
}

int main() {
    std::string b = "Hi\x24The\x24HowAreYou\x24?";
    std::vector&lt;unsigned char&gt; va;
    std::vector&lt;unsigned char&gt; 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.
The function copy_max() is mis-named, No one maintaining this code would know from the name of the function that the from string is being shortened by the from.erase(from.begin(), prev_pos). Copy does not imply destruction of the source, move might be more appropriate.

You may also want to consider the Single Responsibility Principle and break copy_max() into multiple functions. I see at least 2 functions there. This loop is complex enough to be it's own function and can be called from
copy_max()

    while(pos != from.end() &amp;&amp; index &lt;= limit)
    {
        prev_pos = pos;
        pos = std::find(pos + 1, from.end(), delim);
        index = std::distance(from.begin(), pos);
    }


Doing so makes the logic of copy_max() clearer.

I'm fairly sure that you can calculate index only once. I'm not sure why it's being calculated in 3 different places in copy_max().

The title is miss leading as well, since this will copy more than just characters.