Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not really a good idea, but I'll show you how.</p> <p>You can get a raw pointer to the integer this way:</p> <pre><code>int * myPointer2 = (int*)&amp;(s[0]); </code></pre> <p>but this is really bad practice because you can't guarantee that the span structure doesn't have any <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" rel="nofollow noreferrer">padding</a>, so while it might work fine for me and you today we can't say much for other systems.</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; struct Span{ int from; int to; }; int main() { std::vector&lt;Span&gt; s; Span a = { 1, 2}; Span b = {2, 9}; Span c = {10, 14}; s.push_back(a); s.push_back(b); s.push_back(c); int * myPointer = (int*)&amp;(s[0]); for(int k = 0; k &lt; 6; k++) { std::cout &lt;&lt; myPointer[k] &lt;&lt; std::endl; } return 0; } </code></pre> <p>As I said, that hard reinterpret cast will often work but is very dangerous and lacks the cross-platform guarantees you normally expect from C/C++.</p> <p>The next worse thing is this, that will actually do what you asked but you should never do. This is the sort of code you could get fired for:</p> <pre><code>// Baaaad mojo here: turn a vector&lt;span&gt; into a vector&lt;int&gt;: std::vector&lt;int&gt; * pis = (std::vector&lt;int&gt;*)&amp;s; for ( std::vector&lt;int&gt;::iterator It = pis-&gt;begin(); It != pis-&gt;end(); It++ ) std::cout &lt;&lt; *It &lt;&lt; std::endl; </code></pre> <p>Notice how I'm using a pointer to vector and pointing to the address of the vector object s. My hope is that the internals of both vectors are the same and I can use them just like that. For me, this works and while the standard templates may luckily require this to be the case, it is not generally so for templated classes (see such things as padding and template specialization).</p> <p>Consider instead copying out an array (see ref 2 below) or just using s<a href="http://en.wikipedia.org/wiki/Data_structure_alignment" rel="nofollow noreferrer">1</a>.from and s[2].to.</p> <p>Related Reading:</p> <ol> <li><a href="https://stackoverflow.com/questions/849168/are-stdvector-elements-guaranteed-to-be-contiguous">Are std::vector elements guaranteed to be contiguous?</a></li> <li><a href="https://stackoverflow.com/questions/2923272/how-to-convert-vector-to-array-c">How to convert vector to array in C++</a></li> </ol>
    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