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
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
Regards
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?
Qt Code:
struct Obj { }; std::vector<Obj> objs; objs.reserve(10);To copy to clipboard, switch view to plain text mode
"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.
Last edited by wysota; 8th January 2006 at 17:27.
Easy. Just do a resize. For example
creates a vector of 100 doubles.Qt Code:
std::vector< double > foo(100, 1.234567);To copy to clipboard, switch view to plain text mode
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:
Qt 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.56789To copy to clipboard, switch view to plain text mode
I just got funny results with experimenting with resize()
Qt 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; }To copy to clipboard, switch view to plain text mode
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?$ ./a.out
Constructed object no 1
Constructed object no 2
Constructed object no 3
Constructed object no 4
Last edited by wysota; 8th January 2006 at 20:21.
seems you forget about copy ctors.
You are right, the behaviour of vector is implementation defined. That's the gcc 3.4.2 output:
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 10Qt 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; }To copy to clipboard, switch view to plain text mode
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?When describing the functions associated with these various containers, this website defines the word TYPE to be the object type that the container holds.Qt Code:
void resize( size_type num, const TYPE& val = TYPE() );To copy to clipboard, switch view to plain text mode
Last edited by yop; 8th January 2006 at 20:48.
If you ignore second argument, it receives the value that function TYPE() returns.
Qt 5.3 Opensource & Creator 3.1.2
Though my question was retorical , you re right
LOL, why do you post retorical questions?
Qt 5.3 Opensource & Creator 3.1.2
Bookmarks