I wonder what the correct syntax is to delete All contents like below:
2D vector
string vector
double vector

These 3 look like this:

std::vector<std::vector<string> > Word2(100, std::vector<string>(100));
std::vector<string> Word1(100);
std::vector<double> Number1(100);

Is it correct to do this:

delete[] Word2;
delete[] Word1;
delete[] Number1;

Thank you

None of the above. (You never new ed anything.)

What is ment by that. "new" ed. Assume that these 3 types of vector has all elements full of values/strings.
I have never deleted vectors before.

Is it correct to do like this. I am not sure if clear() really delete All contens in the vector/2D vector.

Word2.clear();
Word1.clear();
Number1.clear();

None of the above. (You never new ed anything.)

What is ment by that. "new" ed. Assume that these 3 types of vector has all elements full of values/strings.

Never delete anything that you didn't new .

I have never deleted vectors before.

And why would you? The destructor handles such things on its internals.

Is it correct to do like this. I am not sure if clear() really delete All contens in the vector/2D vector.

Word2.clear();
Word1.clear();
Number1.clear();

Ah, clearing contents. I think that looks right.

Side note: be careful about using various terms that have well-defined language meanings, it can sometimes cause confusion.

Thanks Dave Sinkula, I think that made it a bit clearer for me.

Never delete anything that you didn't new .

And why would you? The destructor handles such things on its internals.

Ah, clearing contents. I think that looks right.

Side note: be careful about using various terms that have well-defined language meanings, it can sometimes cause confusion.

I found out that clear() do clear all indexes and this was not what I was after. For example for this 2D-vector I wonder how to "Reset" all contents/indexes.
Thanks..

std::vector<std::vector<string> > Words(100, std::vector<string>(100));

Generally when I have encountered these "clear the string" queries, I feel obliged to ask why. Why do you "need" to clear strings?

Yes it is good that you are asking actually. I am not quite sure. I have heard of that if you fill a std::vector<string> words(1000); or a ex:
std::vector<double> numbers(1000); with numbers.

All 1001 elements consista of a string. If you fill it up from the beginning again 0-1000, these new string will "overwrite" the old ones so it is not necessary to "emty" all elements between fill and fill with new values.

Am I thinking right here. So it would be not nessecary to emty the vector then ?

Thank you..

Generally when I have encountered these "clear the string" queries, I feel obliged to ask why. Why do you "need" to clear strings?

Right -- if you're overwriting anyway, you don't need to overwrite with 'empty' merely to follow that by overwriting with 'something'.

By the way, a vector of 1000 elements has 1000 elements. You're doing funky math!:icon_razz:
(They're numbered 0-999).

heh, yes you are right. I was thinking wrong there, 1000 elements ofcourse :D

Thank you Dave

Right -- if you're overwriting anyway, you don't need to overwrite with 'empty' merely to follow that by overwriting with 'something'.

By the way, a vector of 1000 elements has 1000 elements. You're doing funky math!:icon_razz:
(They're numbered 0-999).

std::vector<std::vector<string> > Word2(100, std::vector<string>(100));
std::vector<string> Word1(100);
std::vector<double> Number1(100);

Is it correct to do this:

delete[] Word2;
delete[] Word1;
delete[] Number1;

Thank you

When you are using the STL vector and other collection class you cannot assume how the underlying implementation is done using which data structures. Yes they could be doing a new[] or they could even be using a linked list or whatever as long as the vector implementation is able to fulfill the performance criteria and interface contract set by the C++ standard.

Therefore to your question, there is no need to do a delete [] at all for those vector variables. But if you code like below you need to do some action.

vector<double> *Number1 = new vector<double>(10) ;
Number1->push_back(1.0);
...
delete Number1; //free memory

OR

vector<double *> *Number2 = new vector<double>(10);
Number2->push_back(new double(1.0));
...
vector<double *>::iterator itr = Number2->begin();
while (itr != Number2->end()) {
  delete(*itr);
  ++itr;
} //iterate through the vector Number2 to free them one by one
...
delete Number2; //free memory
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.