Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not a C++ expert, but it seems that your problem stems from copying the <code>Type</code> instances, instead of a reference/pointer to the <code>Type</code> instances.</p> <pre><code>std::map&lt;Type&gt; myMap; // &lt;-- this is wrong, since std::map requires two template parameters, not one </code></pre> <p>If you add elements to the map and they're not pointers, then I believe the copy constructor is invoked and that will certainly cause delays with a large data structure. Use the pointer instead:</p> <pre><code>std::map&lt;KeyType, ObjectType*&gt; myMap; </code></pre> <p>Furthermore, your example is a little confusing since you "insert" a value of type <code>int</code> in the map when you're expecting a value of type <code>Type</code>. I think you should assign the reference to the item, not the index.</p> <pre><code>myMap[theType.key] = &amp;myVector[i]; </code></pre> <h2>Update:</h2> <p>The more I look at your example, the more confused I get. If you're using the std::map, then it should take two template types:</p> <pre><code>map&lt;T1,T2&gt; aMap; </code></pre> <p>So what are you REALLY mapping? <code>map&lt;Type, int&gt;</code> or something else?</p> <p>It seems that you're using the <code>Type.key</code> member field as a key to the map (it's a valid idea), but unless key is of the same type as <code>Type</code>, then you can't use it as the key to the map. So is <code>key</code> an instance of <code>Type</code>??</p> <p>Furthermore, you're mapping the current vector index to the key in the map, which indicates that you're just want the index to the vector so you can later access that index location fast. Is that what you want to do?</p> <h2>Update 2.0:</h2> <p>After reading your answer it seems that you're using <code>std::map&lt;long,int&gt;</code> and in that case there is no copying of the structure involved. Furthermore, you don't need to make a local reference to the object in the vector. If you just need to access the key, then access it by calling <code>myVector[i].key</code>.</p>
    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.
 

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