C++ Split string into a vector While profiling parts of my code, I noticed that my Split( ... ) function is rather slow, (about 50% of the time).
I was wondering if there was a more efficient way of Splitting a string into a vector.    

My subject string can be anything between 2 words to well over 1000 words, hence the reason why I would like to speed it up.

Here is an example program with the function.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;
#include &lt;vector&gt;

void Split(const std::string&amp; subject, std::vector&lt;std::string&gt;&amp; container)
{
  container.clear();
  size_t len = subject.length() + 1;
  char* s = new char[ len ];
  memset(s, 0, len*sizeof(char));
  memcpy(s, subject.c_str(), (len - 1)*sizeof(char));
  for (char *p = strtok(s, " "); p != NULL; p = strtok(NULL, " "))
  {
    container.push_back( p );
  }
  delete[] s;
}

int main()
{
  std::vector&lt;std::string&gt; container;
  Split( "Hello World", container );

  for( std::vector&lt;std::string&gt;::const_iterator it = container.begin(); it != container.end();++it)
  {
    std::cout &lt;&lt; *it &lt;&lt; "!\n";
  }
}


Any suggestions on how I could speed it up.    

Of course any other general comments are more than welcome.

Edit profiling is very simple.

...
clock_t t = clock (); 
std::vector&lt;std::string&gt; container;
Split( "actus non facit reum nisi mens sit rea", container );
t = clock() - t;
std::cout &lt;&lt; (((float)t)/CLOCKS_PER_SEC) &lt;&lt; "ms\n";
...
I haven't attempted to measure the timing of this, in part because I don't know how you're planning to use it, but here are some ideas for improving your code.

Consider returning something useful from functions

Your Split function doesn't return anything, which isn't an error, but it's an odd design.  I'd expect it to be declared like this instead:

std::vector&lt;std::string&gt; Split(const std::string&amp; subject)


Understand sizeof

The sizeof operator is defined as always returning 1 for sizeof(char), so having that expression as part of your code probably isn't useful.

Use "range for" to simplify your code

The code to print the vector could be made much simpler and shorter by using a "range for" that was introduced in C++11:

for(const auto &amp;item : container) {
    std::cout &lt;&lt; item &lt;&lt; "!\n";
}


Use standard library algorithms

I'd be inclined to write this using C++ rather than C functions.  One way to do this is to use std::copy and std::stringstream:

std::vector&lt;std::string&gt; Split(const std::string&amp; subject)
{
    std::istringstream ss{subject};
    using StrIt = std::istream_iterator&lt;std::string&gt;;
    std::vector&lt;std::string&gt; container{StrIt{ss}, StrIt{}};
    return container;
}


I've also been playing with std::sregex_token_iterator lately.  Here's how that might look:

std::vector&lt;std::string&gt; Split(const std::string&amp; subject) {
    static const std::regex re{"\\s+"};
    std::vector&lt;std::string&gt; container{
        std::sregex_token_iterator(subject.begin(), subject.end(), re, -1), 
        std::sregex_token_iterator()
    };
    return container;
}