Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I might look into <code>std::stringstream</code> from <code>&lt;sstream&gt;</code>. C-style <code>strtok</code> has a number of usability problems, and C-style strings are just troublesome. </p> <p>Here's an ultra-trivial example of it tokenizing a sentence into words:</p> <pre><code>#include &lt;sstream&gt; #include &lt;iostream&gt; #include &lt;string&gt; int main(void) { std::stringstream sentence("This is a sentence with a bunch of words"); while (sentence) { std::string word; sentence &gt;&gt; word; std::cout &lt;&lt; "Got token: " &lt;&lt; word &lt;&lt; std::endl; } } janks@phoenix:/tmp$ g++ tokenize.cc &amp;&amp; ./a.out Got token: This Got token: is Got token: a Got token: sentence Got token: with Got token: a Got token: bunch Got token: of Got token: words Got token: </code></pre> <p>The <code>std::stringstream</code> class is "bi-directional", in that it supports input and output. You'd probably want to do just one or the other, so you'd use <code>std::istringstream</code> or <code>std::ostringstream</code>. </p> <p>The beauty of them is that they are also <code>std::istream</code> and <code>std::ostream</code>s respectively, so you can use them as you'd use <code>std::cin</code> or <code>std::cout</code>, which are hopefully familiar to you. </p> <p>Some might argue these classes are expensive to use; <code>std::strstream</code> from <code>&lt;strstream&gt;</code> is mostly the same thing, but built on top of cheaper C-style 0-terminated strings. It might be faster for you. But anyway, I wouldn't worry about performance right away. Get something working, and then benchmark it. Chances are you can get enough speed by simply writing well-written C++ that minimizes unnecessary object creation and destruction. If it's still not fast enough, then you can look elsewhere. These classes are probably fast enough, though. Your CPU can waste thousands of cycles in the amount of time it takes to read a block of data from a hard disk or network. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload