By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

STL

P: n/a
Hi,

What does the following code mean? It's a two-dimensional vector?

#include <vector>;

vector<vector<int vv;

Thanks,
Michael

Aug 7 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a

"Michael" <mi*******@gmail.comskrev i meddelandet
news:11*********************@b28g2000cwb.googlegro ups.com...
Hi,

What does the following code mean? It's a two-dimensional vector?
Sort of. It is a vector of vectors of integers.

The inner vectors can be of different sizes, so it is not immediately
a matrix.
Bo Persson
>
#include <vector>;

vector<vector<int vv;

Thanks,
Michael

Aug 7 '06 #2

P: n/a
Michael wrote:
What does the following code mean? It's a two-dimensional vector?

#include <vector>;
Delete the semicolon. Add: using namespace std;
>
vector<vector<int vv;
Yes, a two-dimensional vector -- i.e., a vector of vectors.

Cheers! --M

Aug 7 '06 #3

P: n/a
mlimber wrote:
>vector<vector<int vv;

Yes, a two-dimensional vector -- i.e., a vector of vectors.
AKA a "ragged array"; halfway between a "sparse matrix" and a matrix.

BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 7 '06 #4

P: n/a

Phlip wrote:
mlimber wrote:
vector<vector<int vv;
Yes, a two-dimensional vector -- i.e., a vector of vectors.

AKA a "ragged array"; halfway between a "sparse matrix" and a matrix.

BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Could you provide such an initialized vector of vector for me to
understand?

Michael

Aug 7 '06 #5

P: n/a

"Michael" <mi*******@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
>
Phlip wrote:
>mlimber wrote:
>vector<vector<int vv;

Yes, a two-dimensional vector -- i.e., a vector of vectors.

AKA a "ragged array"; halfway between a "sparse matrix" and a matrix.

BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!

Could you provide such an initialized vector of vector for me to
understand?
Don't you have a sample yourself already? Where did you get that line of
code, if not from an example?

-Howard

Aug 7 '06 #6

P: n/a

Howard wrote:
>
Don't you have a sample yourself already? Where did you get that line of
code, if not from an example?
I got from a post, which does not have an initilization. So, I do not
have a sample here.

Thanks,
Michael

Aug 7 '06 #7

P: n/a

"Michael" <mi*******@gmail.comwrote in message
news:11**********************@n13g2000cwa.googlegr oups.com...
>
Howard wrote:
>>
Don't you have a sample yourself already? Where did you get that line of
code, if not from an example?

I got from a post, which does not have an initilization. So, I do not
have a sample here.
A google search "c++ vector of vectors" should give you plenty of examples.
Aug 7 '06 #8

P: n/a
Phlip wrote:
mlimber wrote:
>>vector<vector<int vv;
Yes, a two-dimensional vector -- i.e., a vector of vectors.

AKA a "ragged array"; halfway between a "sparse matrix" and a matrix.

BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?
I guess it depends-- and when doesn't it? :)

The conservative move would be to reserve the space for the outer vector
at the outset so that only the 1D inner vectors need to be reallocated
dynamically. I wonder though if a clever implementation might be able
to move a vector without copying its dynamic storage space?
Aug 7 '06 #9

P: n/a
Phlip wrote:
mlimber wrote:
>>vector<vector<int vv;
Yes, a two-dimensional vector -- i.e., a vector of vectors.

AKA a "ragged array"; halfway between a "sparse matrix" and a matrix.

BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?
Isn't that what "reserve" is for...?
--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.

In science it often happens that scientists say, 'You know
that's a really good argument; my position is mistaken,'
and then they actually change their minds and you never
hear that old view from them again. They really do it.
It doesn't happen as often as it should, because scientists
are human and change is sometimes painful. But it happens
every day. I cannot recall the last time something like
that happened in politics or religion.

- Carl Sagan, 1987 CSICOP keynote address

Aug 7 '06 #10

P: n/a
fungus wrote:
>>>vector<vector<int vv;
>BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?
Isn't that what "reserve" is for...?
Does that default-construct each element?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 8 '06 #11

P: n/a

Phlip wrote:
fungus wrote:
>>vector<vector<int vv;
BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?
Isn't that what "reserve" is for...?

Does that default-construct each element?
No! It only reserves the vector for a number of elements, but do not
create any objects in it. Se the member funtions size() and capacity().
-Thomas

Aug 8 '06 #12

P: n/a
Thomas wrote:
>Does [reserve()] default-construct each element?
No! It only reserves the vector for a number of elements, but do not
create any objects in it. Se the member funtions size() and capacity().
So are these behaviors undefined?

vector<intv;
v.reserve(1);
v[0]; // <-- ?
int q;
q; // <-- ?

I am fairly certain q causes undefined behavior, because an integer might
contain garbage with an illegal bit pattern. Treating it as an rvalue, even
without using it, is undefined.

If so, is v[0] undefined?

If the vector instead contained objects with important default constructors,
v[0] would be undefined too, right?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 8 '06 #13

P: n/a

Michael wrote:
Phlip wrote:
mlimber wrote:
>vector<vector<int vv;
>
Yes, a two-dimensional vector -- i.e., a vector of vectors.
AKA a "ragged array"; halfway between a "sparse matrix" and a matrix.

BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!

Could you provide such an initialized vector of vector for me to
understand?

Try this:

void print_vector(const std::vector<int>& v)
{
for(unsigned int i = 0; i < v.size() ; ++i)
{
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
int main(int argc, char** argv)
{
std::vector<std::vector<int v(10);
for(unsigned int i = 0; i < v.size(); ++i)
{
v[i] = std::vector<int>(10);
}
for(unsigned int i = 0; i < v.size(); ++i)
{
for(unsigned int j = 0; j < v[i].size(); ++j)
{
v[i][j] = (i*10) + j;
}
}
for(unsigned int i = 0; i < v.size(); ++i)
{
print_vector(v[i]);
}
return 0;
}

-Thomas

Aug 8 '06 #14

P: n/a

Phlip wrote:
Thomas wrote:
Does [reserve()] default-construct each element?
No! It only reserves the vector for a number of elements, but do not
create any objects in it. Se the member funtions size() and capacity().

So are these behaviors undefined?

vector<intv;
v.reserve(1);
v[0]; // <-- ?
int q;
q; // <-- ?
Yes, I think so too. Can't imagine that the vector should hava
different behavior with objects that POD's.
I am fairly certain q causes undefined behavior, because an integer might
contain garbage with an illegal bit pattern. Treating it as an rvalue, even
without using it, is undefined.

If so, is v[0] undefined?

If the vector instead contained objects with important default constructors,
v[0] would be undefined too, right?
Yes! That's true!
Try this:

#include <iostream>
#include <vector>
class foo_t
{
public:
foo_t(void)
{
std::cout << "foo_t(void)" << std::endl;
}
foo_t(const foo_t&)
{
std::cout << "foo_r(const foo_t)" << std::endl;
}
~foo_t(void)
{
std::cout << "~foo_t(void)" << std::endl;
}
};

int main(int /*argc*/, char** /*argv*/)
{

std::vector<foo_tv;
std::cout << "Before reserve" << std::endl;
v.reserve(10);
std::cout << "After reserve" << std::endl;
std::cout << "Size: " << v.size() << std::endl;
std::cout << "Capacity: " << v.capacity() << std::endl <<
std::endl;;

std::cout << "Starting std::vector<foo_tv2(10)" << std::endl;
std::vector<foo_tv2(10);
std::cout << "Ending std::vector<foo_tv2(10)" << std::endl;
std::cout << "Size(v2): " << v2.size() << std::endl;
std::cout << "Size(v2): " << v2.capacity() << std::endl <<
std::endl;

std::cout << "Ending main" << std::endl;

return 0;

}

Here you will se that after the reserve, the vector do not contain any
object. So of course you can not access any objects that don't exist.

std:vector<has a constructor that takes an int, an this creates
default constructed objects. It actually creates a, default constructed
object and copies this a number of times as you will se i you run my
example. std::vector<>, also has a constrcutor sthat takes an int and
an object of std:vector<>::vector_type. This creates a vector with a
number of copies of this object.Ex:
std::vector<std::stringv(10, "Hello");
std::cout << v.at(2) << std::endl;//will print "Hello"

-Thomas

-Thomas

Aug 8 '06 #15

P: n/a

Phlip wrote:
Thomas wrote:
Does [reserve()] default-construct each element?
No! It only reserves the vector for a number of elements, but do not
create any objects in it. Se the member funtions size() and capacity().

So are these behaviors undefined?

vector<intv;
v.reserve(1);
v[0]; // <-- ?
int q;
q; // <-- ?
Yes, I think so too. Can't imagine that the vector should hava
different behavior with objects that POD's.
I am fairly certain q causes undefined behavior, because an integer might
contain garbage with an illegal bit pattern. Treating it as an rvalue, even
without using it, is undefined.

If so, is v[0] undefined?

If the vector instead contained objects with important default constructors,
v[0] would be undefined too, right?
Yes! That's true!
Try this:

#include <iostream>
#include <vector>
class foo_t
{
public:
foo_t(void)
{
std::cout << "foo_t(void)" << std::endl;
}
foo_t(const foo_t&)
{
std::cout << "foo_r(const foo_t)" << std::endl;
}
~foo_t(void)
{
std::cout << "~foo_t(void)" << std::endl;
}
};

int main(int /*argc*/, char** /*argv*/)
{

std::vector<foo_tv;
std::cout << "Before reserve" << std::endl;
v.reserve(10);
std::cout << "After reserve" << std::endl;
std::cout << "Size: " << v.size() << std::endl;
std::cout << "Capacity: " << v.capacity() << std::endl <<
std::endl;;

std::cout << "Starting std::vector<foo_tv2(10)" << std::endl;
std::vector<foo_tv2(10);
std::cout << "Ending std::vector<foo_tv2(10)" << std::endl;
std::cout << "Size(v2): " << v2.size() << std::endl;
std::cout << "Size(v2): " << v2.capacity() << std::endl <<
std::endl;

std::cout << "Ending main" << std::endl;

return 0;

}

Here you will se that after the reserve, the vector do not contain any
object. So of course you can not access any objects that don't exist.

std:vector<has a constructor that takes an int, an this creates
default constructed objects. It actually creates a, default constructed
object and copies this a number of times as you will se i you run my
example. std::vector<>, also has a constrcutor sthat takes an int and
an object of std:vector<>::vector_type. This creates a vector with a
number of copies of this object.Ex:
std::vector<std::stringv(10, "Hello");
std::cout << v.at(2) << std::endl;//will print "Hello"

-Thomas

-Thomas

Aug 8 '06 #16

P: n/a
Thomas wrote:
std::vector<std::vector<int v(10);
for(unsigned int i = 0; i < v.size(); ++i)
Another tip: Such code should make liberal use of typedefs and iterators.
Without them, we essentially devolve into C-style C++.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 8 '06 #17

P: n/a
Thomas wrote
v.reserve(10);
std::cout << "After reserve" << std::endl;
std::cout << "Size: " << v.size() << std::endl;
Tip: Always express such things as assertions (such as inside, I dunno,
maybe, unit tests?):

assert(10 == v.size());
Here you will se that after the reserve, the vector do not contain any
object. So of course you can not access any objects that don't exist.
Thank you!

Note to the newbies: Thomas's code is a valid experiment for this situation,
but in general you should never test your compiler to learn the language.
Your compiler and library might reveal an "alternative" interpretation!

A test that my 'q' generates undefined behavior is impossible on most
compilers, because integers have no illegal bit patterns.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 8 '06 #18

P: n/a

Phlip wrote:
Thomas wrote:
std::vector<std::vector<int v(10);
for(unsigned int i = 0; i < v.size(); ++i)

Another tip: Such code should make liberal use of typedefs and iterators.
Without them, we essentially devolve into C-style C++.
Yes! I agree! But I thought this would confuse newbies! Thank you for
pointing that out :)

-Thomas

Aug 8 '06 #19

P: n/a

Thomas wrote:
Phlip wrote:
Thomas wrote:
>Does [reserve()] default-construct each element?
No! It only reserves the vector for a number of elements, but do not
create any objects in it. Se the member funtions size() and capacity().
So are these behaviors undefined?

vector<intv;
v.reserve(1);
v[0]; // <-- ?
int q;
q; // <-- ?
Yes, I think so too. Can't imagine that the vector should hava
different behavior with objects that POD's.
I am fairly certain q causes undefined behavior, because an integer might
contain garbage with an illegal bit pattern. Treating it as an rvalue, even
without using it, is undefined.

If so, is v[0] undefined?

If the vector instead contained objects with important default constructors,
v[0] would be undefined too, right?
Yes! That's true!
Try this:

#include <iostream>
#include <vector>
class foo_t
{
public:
foo_t(void)
{
std::cout << "foo_t(void)" << std::endl;
}
foo_t(const foo_t&)
{
std::cout << "foo_r(const foo_t)" << std::endl;
}
~foo_t(void)
{
std::cout << "~foo_t(void)" << std::endl;
}
};

int main(int /*argc*/, char** /*argv*/)
{

std::vector<foo_tv;
std::cout << "Before reserve" << std::endl;
v.reserve(10);
std::cout << "After reserve" << std::endl;
std::cout << "Size: " << v.size() << std::endl;
std::cout << "Capacity: " << v.capacity() << std::endl <<
std::endl;;

std::cout << "Starting std::vector<foo_tv2(10)" << std::endl;
std::vector<foo_tv2(10);
std::cout << "Ending std::vector<foo_tv2(10)" << std::endl;
std::cout << "Size(v2): " << v2.size() << std::endl;
std::cout << "Size(v2): " << v2.capacity() << std::endl <<
std::endl;

std::cout << "Ending main" << std::endl;

return 0;

}

Here you will se that after the reserve, the vector do not contain any
object. So of course you can not access any objects that don't exist.

std:vector<has a constructor that takes an int, an this creates
default constructed objects. It actually creates a, default constructed
object and copies this a number of times as you will se i you run my
example. std::vector<>, also has a constrcutor sthat takes an int and
an object of std:vector<>::vector_type. This creates a vector with a
number of copies of this object.Ex:
std::vector<std::stringv(10, "Hello");
std::cout << v.at(2) << std::endl;//will print "Hello"
Have to correct my self:
First I wrote std::vector<>::vector_type it should be
std::vector<>::value_type,
The std::vector template has a typedef that says "typedef T value_type"
so if you have a vector , std::vector<std::stringthe value_type of
this vector is std::string.

And I said that it has a constructor that takes an int, and another
construtor that takes an int and a type of std::vector<>::value_type,
this is not so. The std::vector<>
has a constructor like this:
explicit vector(size_type n, const T& valu = T(), const Allocator& =
Allocator());

So i did not see the default parameter. Sorry! The two constructors I
talked about was one and the same.

-Thomas

Aug 8 '06 #20

P: n/a
Mark P wrote:
Phlip wrote:
mlimber wrote:
>vector<vector<int vv;
Yes, a two-dimensional vector -- i.e., a vector of vectors.
AKA a "ragged array"; halfway between a "sparse matrix" and a matrix.

BTW does anyone generally worry about the cost of deep-copies as such an
array gets populated?

The conservative move would be to reserve the space for the outer vector
at the outset so that only the 1D inner vectors need to be reallocated
dynamically.
Why not simply allocate the memory? You'll even get a nicely zeroed
array:
vector<vector<int v_v (10,5); // v_v contains 10 vectors of 5 ints
each.
This of course is not a ragged 2D vector (yet).

HTH,
Michiel Salters

Aug 8 '06 #21

P: n/a
Mi*************@tomtom.com wrote:
Mark P wrote:
>Phlip wrote:
>>mlimber wrote:

vector<vector<int vv;
Yes, a two-dimensional vector -- i.e., a vector of vectors.

AKA a "ragged array"; halfway between a "sparse matrix" and a
matrix.

BTW does anyone generally worry about the cost of deep-copies as
such an array gets populated?

The conservative move would be to reserve the space for the outer
vector at the outset so that only the 1D inner vectors need to be
reallocated dynamically.

Why not simply allocate the memory? You'll even get a nicely zeroed
array:
vector<vector<int v_v (10,5); // v_v contains 10 vectors of 5 ints
each.
It does? Have you tried? I thought it had to be

vector<vector<int v_v(10, vector<int>(5) );
This of course is not a ragged 2D vector (yet).
Right.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #22

P: n/a
Michiel.Salters wrote:
Why not simply allocate the memory? You'll even get a nicely zeroed
array:
vector<vector<int v_v (10,5); // v_v contains 10 vectors of 5 ints
each.
This of course is not a ragged 2D vector (yet).
Note that pushing back a single integer, into it, makes it ragged!

Raggedity refers to the vector's constructed contents, not its capacity...
;-)

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 8 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.