473,396 Members | 1,713 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,396 software developers and data experts.

vector of vectors push_back question

Hello-

Ive never used a vector or vectors in C++ so I have a question for you
all.

I know I can dynamically create the size I need upfront, but is it
possible to
create them on the fly dynamically? That is, for a 2 dim array, I want
to create
the first element and then push_back the columns after that:

This code obviously does not work, but this is how I percieve it to
create the
initial 'x' or row element, then add each column after that.

int hist_idx = 0;
vector< <vector<int> > history;

history.push_back(hist_idx);

then

history[hist_idx].push_back(5);
history[hist_idx].push_back(2);
etc

If I have to statically create it upfront, is it possible to re-size it
when I reach
the limit without losing the current data already in 'history'? If so
how?

Thanks,
Jeff

Mar 2 '06 #1
9 15859
Jeff wrote:
Hello-

Ive never used a vector or vectors in C++ so I have a question for
you all.

I know I can dynamically create the size I need upfront, but is it
possible to create them on the fly dynamically? That is, for a 2 dim
array, I want to create the first element and then push_back the
columns after that:

This code obviously does not work, but this is how I percieve it to
create the initial 'x' or row element, then add each column after
that.

int hist_idx = 0; vector< <vector<int> > history;

history.push_back(hist_idx);
hist_idx is an int. history requires a vector<int>

vector<int> v;
history.push_back(v);
then

history[hist_idx].push_back(5); history[hist_idx].push_back(2); etc
Thats looks ok.
If I have to statically create it upfront, is it possible to re-size
it when I reach the limit without losing the current data already in
'history'? If so how?


push_back adds an element on the end, and deals with any resizing if
required. All existing elements remain, but iterators may be invalidated.

A vector will have a capacity and a size, if the size is equal to
capacity, and an element is added, the vector is usually reallocated to
twice the original capacity, the elements copied over, and the old
elements deleted.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 2 '06 #2
On 2006-03-02, Jeff <je**@rahul.net> wrote:
I know I can dynamically create the size I need upfront, but is it
possible to create them on the fly dynamically? That is, for a 2
dim array, I want to create the first element and then push_back the
columns after that:

This code obviously does not work, but this is how I percieve it to
create the initial 'x' or row element, then add each column after
that.
You are very close to working code.
int hist_idx = 0;
vector< <vector<int> > history;

history.push_back(hist_idx);
then

history[hist_idx].push_back(5);
history[hist_idx].push_back(2);
etc


You have options.

Here's one:

vector< vector<int> > history(10, vector<int>(2));
/* Storage for 10 vectors of vectors of 2 ints. */
history[0][0] = 5;
history[0][1] = 2;
/* etc... */
history[9][0] = 7;

Here's another, if you want a dynamically growing number of vectors of
dynamically growing number of ints:

vector< vector<int> > history;

/* add a vector */
history.push_back(vector<int>());
history[0].push_back(5);
history[0].push_back(2);
history.push_back(vector<int>());
history[1].push_back(4);
history[1].push_back(8);
....
history[9].push_back(7);

--
Neil Cerutti
Mar 2 '06 #3
Jeff wrote:

[...]
int hist_idx = 0;
vector< <vector<int> > history;
Ok, you have defined a vector called history, whose element type is
vector<int>.
history.push_back(hist_idx);
And now you are trying to push an int onto that vector.

[...]
If I have to statically create it upfront, is it possible to re-size it
when I reach
the limit without losing the current data already in 'history'? If so
how?


If you know the number of "lines" upfront (i.e., the number of vectors
that will be in the history vector), you can supply that number upon
construction:

const int NUM_LINES = 10;
vector<vector<int> > history(NUM_LINES);

From that point on, you can safely use push_back on each of history's
elements, which are vectors themselves. E.g.:

history[0].push_back(5);
history[0].push_back(22);
history[2].push_back(90);
history[7].push_back(2);
history[7].push_back(86);
history[7].push_back(9);
history[8].push_back(71);

Just realize history is actually a container of vectors which allows you
to access them through their respective numeric index.

I hope that helps a bit.

Regards,

--
Ney André de Mello Zunino
Mar 2 '06 #4
awesome! I got it now, thanks guys!

Jeff

Mar 2 '06 #5
Jeff wrote:
I've never used a vector or vectors in C++ so I have a question for you
all.


This is probably getting a little off topic, but you might also
want to look into a library like Blitz++, which makes working with
matricies much nicer...

http://www.oonumerics.org/blitz/

Greg Buchholz

Mar 2 '06 #6
Jeff wrote:
Hello-

int hist_idx = 0;
vector< <vector<int> > history;

history.push_back(hist_idx);


Use

map<int, vector<int> > history;

if you want your columns to be named. Then you can do next:

history[hist_idx].push_back(...);
Mar 2 '06 #7
Ok, actually thats a good point about map, I do need a vector of
maps as well I think:

std::vector< std::map< int, std::vector<int> > > connected;

I have another need for a history list of items that have multiple
indexes.

It looks like this:

history idx object idx list of connected objects
------------ ---------- ----------------------------
0 1 3 5 9 10
1 2 4
2 3 5 9 10
and so on

So to initialize the first element I tried:

connected.push_back( map<int, vector<int>() > );

then to push them:

map <int, vector<int> > con_map;
con_map.insert(1,3);
con_map.insert(1,5);
con_map.insert(1,9);
con_map.insert(1,10);

int hist = 0;
connected.push_back(hist, con_map);

Thanks again for helping me do this and learning more about STL
Jeff

Mar 3 '06 #8
Ok, actually thats a good point about map, I do need a vector of
maps as well I think:

std::vector< std::map< int, std::vector<int> > > connected;

I have another need for a history list of items that have multiple
indexes.

It looks like this:

history idx object idx list of connected objects
------------ ---------- ----------------------------
0 1 3 5 9 10
1 2 4
2 3 5 9 10
and so on

So to initialize the first element I tried:

connected.push_back( vector<int>(), map<int, vector<int>() > );

then to push them:

map <int, vector<int> > con_map;
con_map.insert(1,3);
con_map.insert(1,5);
con_map.insert(1,9);
con_map.insert(1,10);

int hist = 0;
connected.push_back(hist, con_map);

Thanks again for helping me do this and learning more about STL
Jeff

Mar 3 '06 #9

Jeff wrote in message
<11**********************@v46g2000cwv.googlegroups .com>...
awesome! I got it now, thanks guys!

Jeff


You got it? They just gave me heartburn!! <G>

size_t rows( 6 ); // use unsigned type
size_t cols( 4 ); // ...to avoid surprises
// an 'int rows = -1;' will be a *huge* size in vector init.
// if you must use 'int', make it 'const' and be sure the
// value is what you want

std::vector<std::vector<int> > vec2D( rows, cols ); // 6x4 init'ed to 0's
std::vector<std::vector<int> > vec2D7( rows,
std::vector<int>( cols, int(7) ) ); // 6x4 init'ed to 7's
// ---------------
#include <stdexcept> // out_of_range
try{
if( 4 < rows ){
vec2D.at( 4 ).push_back( 22 ); // add an col to 5th row
// use 'at()' to index when not positive it's in range
}

for(size_t i(0); i < vec2D.size(); ++i){
for(size_t j(0); j < vec2D.at(i).size(); ++j){
std::cout<<" vec2D.at("<<i<<").at("<<j<<")= "
<<vec2D.at( i ).at( j )<<std::endl;
// either kind of index would be OK here because
// the vectors size() controls range.
// std::cout<<" vec2D["<<i<<"].at("<<j<<")= "
// <<vec2D[ i ].at( j )<<std::endl;
} //for(j)
} //for(i)

// this next line will 'throw out_of_range()' ( from 'at()' )
std::cout<<" vec2D.at(6).at(4)= "
<<vec2D.at(6).at(4)<<std::endl;

} //try
catch(std::out_of_range &Oor){
std::cout<<"caught "<<Oor.what()<<std::endl;
}

If you don't understand all this, stash the post for later, or ask for more
info.
--
Bob R
POVrookie
Mar 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
4
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a...
3
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
13
by: Winbatch | last post by:
Is there a function that I can use that given a few vectors which elements are common to all? for example vector<int> a; vector<int> b; a.push_back( 1 ); a.push_back( 2 ); a.push_back( 3 );
3
by: Jeff | last post by:
I have a question about potential memory leakage in a 2 dim array I have. If I say: vector< vector<int> > vec; vec.push_back( vector<int>() ); // initialize the row vec.push_back(1);...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
9
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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,...

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.