vector<string> MLE ~50MB

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
nev4
New poster
Posts: 15
Joined: Sun Apr 30, 2006 10:19 am
Location: Lithuania

vector<string> MLE ~50MB

Post by nev4 »

In all problems where i try to use this i get Memory Limit Exceeded. I clear vector for every next case, but no effect. I'm used to get 49632 kilobytes (as tried deque instead were ~33MB) in every problem when i'm using this combo, even if problem description guarantees only up 6MB (100 strings * 60 chars or 40 strings * 40 chars).

Can i use size_t instead of string::size_type for string::npos determing?

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Re: vector<string> MLE ~50MB

Post by Krzysztof Duleba »

nev4 wrote:In all problems where i try to use this i get Memory Limit Exceeded. I clear vector for every next case, but no effect. I'm used to get 49632 kilobytes (as tried deque instead were ~33MB) in every problem when i'm using this combo, even if problem description guarantees only up 6MB (100 strings * 60 chars or 40 strings * 40 chars).
Clear doesn't really work on vectors - it sets size to 0, but capacity remains unchanged. You have to swap a vector with an empty one:

Code: Select all

template<typename T>
inline void really_clear_vector(vector<T> &v) {
  vector<T> tmp;
  swap(v, tmp);
}
The important thing is that tmp is local in the function scope and will be destroyed when no longer needed.

Note that assigning an empty vector to v has the same effect as clear.
nev4 wrote:Can i use size_t instead of string::size_type for string::npos determing?
You shouldn't, but it will work in most cases (int will work too, at least on 32-bit architectures).
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

Post Reply

Return to “C++”