Quick question.
I've got a list and I simply added just one element to the list
.
But when I print out myList.size()
, it returns 18
? Even though there is one element?
To add my element I use: myList.push_bac开发者_如何学Pythonk(element)
;
§23.3.6 says:
The member function
size()
returns the number of elements in the container.
This means that
1) you have a broken C++ environment, or
2) you have, somehow, added more elements to your list.
Unfortunately, it's hard to tell which is the case, as you haven't posted enough information. What would be nice is a small, complete, examples that demonstrates what you are trying to do, a description of what happens and what you expected to happen.
Another possibility that i can think of is that the list has been created with a finite size using the following constructor :
explicit list ( size_type n, const T& value= T(), const Allocator& = Allocator() );
If I am not wrong you want to do something like this...I performed a quick search and found the following..
int main ()
{
list<int> myints;
cout << "0. size: " << (int) myints.size() << endl;
for (int i=0; i<10; i++) myints.push_back(i);
cout << "1. size: " << (int) myints.size() << endl;
myints.insert (myints.begin(),10,100);
cout << "2. size: " << (int) myints.size() << endl;
myints.pop_back();
cout << "3. size: " << (int) myints.size() << endl;
return 0;
}
At the start do MyList.clear()
then push the elements and finally check the size.
精彩评论