Does insertion of elements in a vector damages a pointer to the vector?

后端 未结 6 598
心在旅途
心在旅途 2021-01-13 10:19

In a program to simulate logic gates I switched from using arrays

node N[1000];

to vectors

vector N;
<         


        
6条回答
  •  天涯浪人
    2021-01-13 10:46

    Returning a pointer to an internal STL member is probably not the best idea. When you give an object to an STL container you are basically giving up control of it. You are telling the STL it can move it around as it sees fit to maintain the promises that the container gives you. Returning the index where the node is located is a better idea like Steven Sudit mentioned.

    Once you get the index you could create a function that returns a copy of the contents of the node you are interested in. This way you also maintain data encapsulation with the STL container, not allowing anyone else to modify its contents.

提交回复
热议问题