PDA

View Full Version : <vector>



mickey
7th January 2006, 00:31
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

jacek
7th January 2006, 01:02
std::vector can't have empty spaces. Even when you resize it, it will be populated with objects.

wysota
7th January 2006, 01:22
You must use a vector or list of pointers if you want to avoid creating empty objects.

shad
7th January 2006, 13:00
Isn't the 'vector::reserve()' what you are looking for?


struct Obj { };
std::vector<Obj> objs;
objs.reserve(10);

wysota
8th January 2006, 18:00
"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.

Thomas
8th January 2006, 19:38
Easy. Just do a resize. For example

std::vector< double > foo(100, 1.234567);
creates a vector of 100 doubles.

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:

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

wysota
8th January 2006, 21:18
I just got funny results with experimenting with resize()


#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:

$ ./a.out
Constructed object no 1
Constructed object no 2
Constructed object no 3
Constructed object no 4

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?

shad
8th January 2006, 21:43
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 10



#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;
}

yop
8th January 2006, 21:46
http://www.cppreference.com/cppvector/resize.html and http://www.cppreference.com/containers.html


void resize( size_type num, const TYPE& val = TYPE() );
When describing the functions associated with these various containers, this website defines the word TYPE to be the object type that the container holds.
See what happens when you don't give a second argument?

MarkoSan
9th January 2006, 13:42
If you ignore second argument, it receives the value that function TYPE() returns.

yop
9th January 2006, 14:28
Though my question was retorical ;), you re right

MarkoSan
9th January 2006, 14:31
LOL, why do you post retorical questions? :D

yop
9th January 2006, 14:45
I'm Greek, we invented them :D