vector stl destruct

I

irinalipov

typedef
vector<string> pipeline;
typedef
vector<pipeline> subPipelineStrings;
subPipelineStrings* m_subPipelineStrings
m_subPipelineStrings =
new subPipelineStrings(3);
string *s1 = new string("s1");
string *s2 = new string("s2");
string *s3 = new string("s3");


for (int i = 0; i < m_subPipelineStrings ->size();++i)
{
pipeline &p = m_subPipelineStrings->at(i);
p.push_back(*s1) ;
p.push_back(*s2) ;
p.push_back(*s3) ;
}


The question : How do i destruct m_subPipelineStrings ?
 
R

Ron AF Greve

hi,

You have to do that yourself just before the vector is destroyed (or earlier
of course) (probably in the destructor of your class).

That is the price you have to pay for not using something like.

std::vector<std::string>



Regards, Ron AF Greve

http://informationsuperhighway.eu
 
R

Ron AF Greve

Hi sorry,


I thought you were storing pointers (my mistake).

in this case you can immediately destroy them since the vector makes a copy
of them.

However apparently there is no need to dynamically allocate them first just
push the strings right away.

p.push_back( "string" ) ;

Regards, Ron AF Greve

http://informationsuperhighway.eu
 
S

SG

typedef vector<string> pipeline;
typedef vector<pipeline> subPipelineStrings;
subPipelineStrings* m_subPipelineStrings =
new subPipelineStrings(3);
string *s1 = new string("s1");
string *s2 = new string("s2");
string *s3 = new string("s3");

for (int i = 0; i < m_subPipelineStrings ->size();++i)
{
    pipeline &p = m_subPipelineStrings->at(i);
    p.push_back(*s1) ;
    p.push_back(*s2) ;
    p.push_back(*s3) ;
}

The question : How do i destruct m_subPipelineStrings ?

Simple:
delete m_subPipelineStrings;

But you also should delete the objects *s1, *s2, and *s3 at some
point:
delete s1;
delete s2;
delete s3;

You should ask yourself why you allocated the string objects *s1, *s2,
*s3, and the vector *m_subPipelineStrings on the heap.

Also, make sure you understand what vector<string>::push_back really
does. A vector of T manages its own /copies/ of T. push_back will copy-
construct a new element from the given one. So, you could have just
written the following:

typedef vector<string> pipeline_t;
vector<pipeline_t> pipelines (3);
if (!pipelines.empty()) {
pipeline_t & p = pipelines[0];
p.push_back("s1");
p.push_back("s2");
p.push_back("s3");
}
for (int i=1, s=pipelines.size(); i<s; ++i) {
pipelines = pipelines[0]; // copy-assigns vector
}

Cheers!
SG
 
I

irinalipov

typedef vector<string> pipeline;
typedef vector<pipeline> subPipelineStrings;
subPipelineStrings* m_subPipelineStrings =
    new subPipelineStrings(3);
string *s1 = new string("s1");
string *s2 = new string("s2");
string *s3 = new string("s3");
for (int i = 0; i < m_subPipelineStrings ->size();++i)
{
    pipeline &p = m_subPipelineStrings->at(i);
    p.push_back(*s1) ;
    p.push_back(*s2) ;
    p.push_back(*s3) ;
}
The question : How do i destruct m_subPipelineStrings ?

Simple:
  delete m_subPipelineStrings;

But you also should delete the objects *s1, *s2, and *s3 at some
point:
  delete s1;
  delete s2;
  delete s3;

You should ask yourself why you allocated the string objects *s1, *s2,
*s3, and the vector *m_subPipelineStrings on the heap.

Also, make sure you understand what vector<string>::push_back really
does. A vector of T manages its own /copies/ of T. push_back will copy-
construct a new element from the given one. So, you could have just
written the following:

  typedef vector<string> pipeline_t;
  vector<pipeline_t> pipelines (3);
  if (!pipelines.empty()) {
    pipeline_t & p = pipelines[0];
    p.push_back("s1");
    p.push_back("s2");
    p.push_back("s3");
  }
  for (int i=1, s=pipelines.size(); i<s; ++i) {
    pipelines = pipelines[0]; // copy-assigns vector
  }

Cheers!
SG- Hide quoted text -

- Show quoted text -


10x
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top