STL



 DEVELOP > c-Plus-Plus > STL

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Michael"
Date: 07 Aug 2006 02:58:20 PM
Object: STL
Hi,
What does the following code mean? It's a two-dimensional vector?
#include <vector>;
vector<vector<int> > vv;
Thanks,
Michael
.

User: "Bo Persson"

Title: Re: STL 07 Aug 2006 03:19:22 PM
"Michael" <michaelnx@gmail.com> skrev i meddelandet
news:1154980700.635942.11730@b28g2000cwb.googlegroups.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

.

User: "mlimber"

Title: Re: STL 07 Aug 2006 03:20:55 PM
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
.
User: "Phlip"

Title: Re: STL 07 Aug 2006 03:33:30 PM
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!!!
.
User: "Michael"

Title: Re: STL 07 Aug 2006 03:44:53 PM
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
.
User: "Howard"

Title: Re: STL 07 Aug 2006 03:49:28 PM
"Michael" <michaelnx@gmail.com> wrote in message
news:1154983493.757271.30600@m73g2000cwd.googlegroups.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
.
User: "Michael"

Title: Re: STL 07 Aug 2006 03:53:32 PM
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
.
User: "Howard"

Title: Re: STL 07 Aug 2006 04:11:29 PM
"Michael" <michaelnx@gmail.com> wrote in message
news:1154984012.050725.221410@n13g2000cwa.googlegroups.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.
.



User: "Thomas"

Title: Re: STL 08 Aug 2006 12:39:42 AM
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
.
User: "Phlip"

Title: Re: STL 08 Aug 2006 01:38:48 AM
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!!!
.
User: "Thomas"

Title: Re: STL 08 Aug 2006 01:51:14 AM
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
.




User: "Mark P"

Title: Re: STL 07 Aug 2006 04:51:47 PM
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?
.
User: ""

Title: Re: STL 08 Aug 2006 11:13:28 AM
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
.
User: "Victor Bazarov"

Title: Re: STL 08 Aug 2006 11:16:26 AM
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
.

User: "Phlip"

Title: Re: STL 08 Aug 2006 11:17:32 AM
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!!!
.



User: "fungus"

Title: Re: STL 07 Aug 2006 07:04:20 PM
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
.
User: "Phlip"

Title: Re: STL 07 Aug 2006 08:46:25 PM
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!!!
.
User: "Thomas"

Title: Re: STL 08 Aug 2006 12:17:34 AM
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
.
User: "Phlip"

Title: Re: STL 08 Aug 2006 12:23:30 AM
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<int> v;
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!!!
.
User: "Thomas"

Title: Re: STL 08 Aug 2006 01:30:54 AM
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<int> v;
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_t> v;
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_t> v2(10)" << std::endl;
std::vector<foo_t> v2(10);
std::cout << "Ending std::vector<foo_t> v2(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::string> v(10, "Hello");
std::cout << v.at(2) << std::endl;//will print "Hello"
-Thomas
-Thomas
.

User: "Thomas"

Title: Re: STL 08 Aug 2006 01:31:06 AM
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<int> v;
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_t> v;
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_t> v2(10)" << std::endl;
std::vector<foo_t> v2(10);
std::cout << "Ending std::vector<foo_t> v2(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::string> v(10, "Hello");
std::cout << v.at(2) << std::endl;//will print "Hello"
-Thomas
-Thomas
.
User: "Phlip"

Title: Re: STL 08 Aug 2006 01:44:06 AM
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!!!
.

User: "Thomas"

Title: Re: STL 08 Aug 2006 01:58:03 AM
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<int> v;
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_t> v;
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_t> v2(10)" << std::endl;
std::vector<foo_t> v2(10);
std::cout << "Ending std::vector<foo_t> v2(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::string> v(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::string> the 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
.









  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER