Hello. Before I go on, have a look at the FAQ for advice on posting
questions about code that doesn't work.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
The important point there is to post the code, not a description of
the code. You are having a problem in C++. You can describe that
problem precisely in C++, but only approximately in English. I am
having to guess a bit, which means I might be wrong.
For an example of what minimal means, see the code example below or
the one I posted in response to your question about returning
references to local variables. Enough to illustrates the point but
absolutely no more.
One final point. ALWAYS copy and paste DIRECTLY from your code editor
into your message. If you type code in to your message by hand, you
risk introducing other errors which distract from your actual problem.
Have a look at the rest of the FAQ too while you're there. Lots of
useful stuff.
I tried to clear a vector "v" using "v.clear()". If "v" contains
those objects that are non-built-in (e.g. string), then "clear()" can
indeed remove all contents. However, if "v" contains built-in types
(e.g. int), then "clear()" doesn't remove anything at all. Why does
"clear()" have this behaviour?
What makes you think nothing is removed?
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main()
{
vector<int> vi(2);
vector<string> vs(2);
cout << vi.size() << " " << vi.size() << "\n";
vi.clear();
vs.clear();
cout << vi.size() << " " << vi.size() << "\n";
}
If the above program prints out anything other than
2 2
0 0
then your compiler's broken.
Now, (and here I am speculating), maybe your development environment
allows you to look at the contents of the memory that, before you
called clear, was owned by the vector. Note that clear does not have
to *change* the contents of the memory, it just removes the elements
from the vector. As far as the vector is concerned, it is now empty.
What values happen to still exist in memory it no longer owns is of no
concern.
Also, when I copy one vector "v1" from another vector "v2", with "v1"
longer than "v2" (e.g. "v1" has 2 elements and "v2" has one element),
then depending on the type of their contents, I found out:
1. if the type is built-in then, then v1[0] has the value of v2[0],
but v1[1] still has the old value of v1[1].
2. if the type is not built-in, then v1[0] has the value of v2[0], but
v1[1] has some garbage value.
This time you'll have to show some code. There are lots of ways of
copying elements from one to another - assigment, calling insert,
std::copy, manually copying element by element. Those are just some
examples off the top of my head. Without knowing which technique you
are using it is impossible to know why you are seeing confusing
results. However, I'll have a guess again. Are you trying something
like this?
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// Create two different sized vectors
// and give them values.
vector<int> v1(1);
vector<int> v2(2);
v1[0] = 42;
v2[0] = 100;
v2[1] = 200;
// Manually copy elements from v2 (size 2)
// to v1 (size 1).
v1[0] = v2[0]; // OK, now v1[0] == 100.
v1[1] = v2[1]; // NOOOOOO!! BAD!!
}
The last line v1[1] = v2[1]; can not work. v1 has size 1 so there is
no element with an index of 1 (which would be the second element) to
assign to. If you try and do something like that, the behaviour is
undefined. That means anything can happen. It can crash. It can appear
to work. It can behave differently for different contained types. It
can work in debug but not in release. It can stop working when you
change to a different compiler, or just change compiler settings. It
can work on Monday but not on Tuesday. Or anything else you can think
of.
This makes me think vector copying and clearing (clear.()) have
different effects to vectors, depending on the type. what is going on
exactly?
Rest assured that the effect of any operation on a vector is
independent of the type of object contained (as long as it is a type
that meets the requirements - copy constructible and assignible - for
being stored in a vector).
If I haven't answered your question yet, post some code, following the
guidelines in the FAQ, that illustrates the problem.
Gavin Deane