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

how to resize a 2D vector?

4
How can I resize a 2D vector of int? initially i have declared vector<vector<int> > value;
I saw that
Expand|Select|Wrap|Line Numbers
  1. void resize(n, t = T())
should be used, but I can't seem to understand how it works. I want to initialize the added elements initialed to some integer value, say 1. how can I do that?

Thanks for the help.
Jan 27 '07 #1
1 12211
horace1
1,510 Expert 1GB
How can I resize a 2D vector of int? initially i have declared vector<vector<int> > value;
I saw that
Expand|Select|Wrap|Line Numbers
  1. void resize(n, t = T())
should be used, but I can't seem to understand how it works. I want to initialize the added elements initialed to some integer value, say 1. how can I do that?

Thanks for the help.
you need to resize the vector and its elements, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.   vector < vector < int > > value(5, vector <int>(10,0));
  8.   cout << "value capacity " << value.capacity() << endl;
  9.   for(int i=0; i<value.capacity();i++)
  10.      cout << "  elment capacity " << (value.at(i)).capacity() << endl;
  11.   // resize value
  12.   value.resize(10, vector <int>(10,0));
  13.   cout << "value capacity " <<  value.capacity() << endl;
  14.   for(int i=0; i<value.capacity();i++)
  15.      cout << "  elment capacity " << (value.at(i)).capacity() << endl;
  16.   // resize element
  17.   for(int i=0; i<value.capacity();i++)
  18.      (value.at(i)).resize(20, 5);
  19.   cout << "value capacity " <<  value.capacity() << endl;
  20.   for(int i=0; i<value.capacity();i++)
  21.      cout << "  elment capacity " << (value.at(i)).capacity() << endl;
  22.   cin.get();
  23. }
  24.  
Jan 27 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Peter Mrosek | last post by:
Hello, I have the following declaration in an header file (abbreviated version): typedef struct { unsigned char rgbBlue; unsigned char rgbGreen; unsigned char rgbRed; unsigned char...
9
by: Alex Vinokur | last post by:
--------- #include <vector> using namespace std; struct Foo { Foo (int) {} }; int main () { vector<Foo> v1;
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
2
by: mj | last post by:
Hi, I recently have found it necessary to move from fortran to c++ for scientific programming... I'm working on a program that needs to resize a 2d vector of vectors within a function... This...
2
by: mrbrightsidestolemymoney | last post by:
Hi, I'm having a problem resizing a (very big) nested vector. It's not the most streamlined piece of code ever but I need this array to avoid having to recalculate the same quantity millions of...
3
by: Jim Langston | last post by:
I really am not sure if this question belongs in this newsgroup, but not sure where else to ask it. There is someone working on a game that I tested, and it was taking >30 seconds to load. He...
5
by: atreya | last post by:
Hi, I'm trying to figure out if there are any differences between the implementation of resize(0) and clear() that a programmer using std::vector should be aware of! Could there be any...
6
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an...
17
by: toton | last post by:
Hi, I am using a vector to reserve certain amount of memory, and reuse it for new set of data, to avoid reallocation of memory. so the call is something like vector<my_datav;///the temporary...
3
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...

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.