Reputation: 54193
Here is what I'm doing.
I have a class which I instance and it has a std::vector.
when I first instance the class this std::vector is empty.
The way I use it is I exponentially add to it and clear. Ex:
Add a number, clear the vector:
Add 2 numbers, clear the vector:
Add 3 numbers, clear the vector,
Add 4 numbers, clear the vector. ......
Is a std::vector the bst way to do what I'm doing? I tried to do reserve(100,000) in the constructor but this did not help.
Is there maybe a better container for my usage?
Thanks
Upvotes: 1
Views: 1548
Reputation:
Something like:
struct A {
std::vector <int> v;
A() : v(100000) {}
};
is probably what you want. When you say:
A a;
This creates an instance the struct A (works for classes too) containing a vector of the required size.
Upvotes: 2
Reputation: 182083
Your algorithm appears to be quadratic. If you really need 100,000 elements, you're adding an element 1 + 2 + 3 + ... + 100,000 times. That's about 5,000,000,000 operations. That many operations, no matter how trivial they are, will take a while on a standard pc, regardless of whether you're using std::vector
or hand-crafted assembly language.
Upvotes: 11