Hi,
I'm looking for resize a vector without destroy it. (eg. I want push_back 10 elements in my vector without use push_back(object), because I don't want create an instance of object)
Is it possible?
Thanks
Printable View
Hi,
I'm looking for resize a vector without destroy it. (eg. I want push_back 10 elements in my vector without use push_back(object), because I don't want create an instance of object)
Is it possible?
Thanks
std::vector can't have empty spaces. Even when you resize it, it will be populated with objects.
You must use a vector or list of pointers if you want to avoid creating empty objects.
Isn't the 'vector::reserve()' what you are looking for?
Code:
struct Obj { }; std::vector<Obj> objs; objs.reserve(10);
"reserve" creates empty objects. Vectora can't have placeholders to objects without actual objects.
Looks like, although it should, reserve actually does not create objects. However it may be implementation dependent.
Easy. Just do a resize. For example
creates a vector of 100 doubles.Code:
std::vector< double > foo(100, 1.234567);
Now resize the vector to 200 elements but do not destroy or modify the current elements. You can choose to initialise the new elements or to leave them alone:
Code:
foo.resize(200); // Resizes to 200 elements. foo.resize(300, 4.56789); // Resizes from 200 to 300 elements and initialises only the new elements to 4.56789
I just got funny results with experimenting with resize()
Code:
#include <vector> #include <iostream> class Test{ public: Test(){ static int _no = 1; std::cout << "Constructed object no " << _no++ << std::endl; } }; int main(){ std::vector<Test> x; x.resize(10); x.resize(15); x.resize(20); x[ 15 ] = Test(); return 0; }
Result:
I understand constructing of the last test object (because of calling the constructor directly), but what about the first three? It looks like each resize() call creates one object of contained type. Can anyone explain it?Quote:
$ ./a.out
Constructed object no 1
Constructed object no 2
Constructed object no 3
Constructed object no 4
seems you forget about copy ctors.
You are right, the behaviour of vector is implementation defined. That's the gcc 3.4.2 output:
Quote:
ctor 0
copy ctor 1
copy ctor 2
copy ctor 3
dtor 4
ctor 5
copy opt 6
dtor 7
dtor 8
dtor 9
dtor 10
Code:
#include <vector> #include <iostream> class Test { static int _no; public: Test() { std::cout << "ctor " << _no++ << std::endl; } Test(const Test&) { std::cout << "copy ctor " << _no++ << std::endl; } ~Test() { std::cout << "dtor " << _no++ << std::endl; } Test& operator= (const Test&) { std::cout << "copy opt " << _no++ << std::endl; } }; int Test::_no = 0; int main() { std::vector<Test> x; x.resize(3); x[ 15 ] = Test(); return 0; }
http://www.cppreference.com/cppvector/resize.html and http://www.cppreference.com/containers.html
See what happens when you don't give a second argument?Quote:
When describing the functions associated with these various containers, this website defines the word TYPE to be the object type that the container holds.Code:
void resize( size_type num, const TYPE& val = TYPE() );
If you ignore second argument, it receives the value that function TYPE() returns.
Though my question was retorical ;), you re right
LOL, why do you post retorical questions? :D
I'm Greek, we invented them :D