473,467 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2725

"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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.