473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

STL

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
22 2718

"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
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
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

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

"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

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

"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
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
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
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

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

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

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

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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.