Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't have a <code>vector</code> of strings -- you have a <code>vector</code> of pointer-to-char. NULL is a perfectly valid pointer-to-char which happens to not point to anything, so it is stored in the vector.</p> <p>Note that the pointers you are actually storing are pointers to char literals. The strings are not copied. </p> <p>It doesn't make a lot of sense to mix the C++ style <code>vector</code> with the C-style char pointers. Its not illegal to do so, but mixing paradigms like this often results in confused &amp; busted code.</p> <p>Instead of using a <code>vector&lt;char*&gt;</code> or a <code>vector&lt;char&gt;</code>, why not use a <code>vector&lt;string&gt;</code> ?</p> <h2>EDIT</h2> <p>Based on your edit, it seems like what your'e trying to do is flatten several strings in to a single <code>vector&lt;char&gt;</code>, with a NULL-terminator between each of the flattened strings.</p> <p>Here's a simple way to accomplish this:</p> <pre><code>#include &lt;algorithm&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;iterator&gt; using namespace std; int main() { // create a vector of strings... typedef vector&lt;string&gt; Strings; Strings c_strings; c_strings.push_back("Stack Over Flow"); c_strings.push_back(""); c_strings.push_back("Answer"); /* Flatten the strings in to a vector of char, with a NULL terminator between each string So the vector will end up looking like this: S t a c k _ O v e r _ F l o w \0 \0 A n s w e r \0 ***********************************************************/ vector&lt;char&gt; chars; for( Strings::const_iterator s = c_strings.begin(); s != c_strings.end(); ++s ) { // append this string to the vector&lt;char&gt; copy( s-&gt;begin(), s-&gt;end(), back_inserter(chars) ); // append a null-terminator chars.push_back('\0'); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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