1 Replies - 988 Views - Last Post: 21 September 2010 - 06:35 AM Rate Topic: -----

#1 mar11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-September 09

stl vector declaration

Posted 21 September 2010 - 06:02 AM

Hi,

I want to know if this approach is correct.


vecotr <vector<int> >
for (int i=0; i < 5; i++)
{
vector<int>  b ;
.... push values to b container 

a.push_back(B)/>;
...
}



A you see, for each iteration a new container from type will be declared, initialized and finally stored in the b container. Could anything go wrong in this approach. For example, id the data in a container will go lost by declaring a new one by the next iteration..

This post has been edited by mar11: 21 September 2010 - 06:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: stl vector declaration

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: stl vector declaration

Posted 21 September 2010 - 06:35 AM

Well it will work, but from one perspective there are two ways of thinking about vectors:

A. Dynamic data structures whose size can change dynamically.
B. Fancy arrays.

At times you use both, but when you only need a fixed size structure then you set your thinking to just that and try not to use the dynamic features. The reason is that the dynamic features actually have a cost albeit very small in this case.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int> > array(5, vector<int>(5, 0)); //create a 5x5 array of int's and initialize to 0;
    for(int i = 0; i < 5; ++i) {
        for(int j=0; j< 5; ++j) {
            array[i][j] = j + i;
        }
    }
    
    for(int i = 0; i < array.size(); ++i) {
        for(int j = 0; j< array[i].size(); ++j) {
            cout << array[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}


So if you know the size of your vector's you can create them all at once and not use the push_back() feature which often requires copying over all the old data into a new block of memory.

Note that you can STILL use push_back() if you ever need to dynamically create elements -- just because you changed how you were thinking about the vector does not mean you changed anything about the vector itself.

For example if you knew that the outside array was 5 elements long, but the inside vector was dynamic you might do something like this:
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

int main() {
    vector<vector<int> > array(5); //create and array of 5 vector<int>'s
    srand(time(0));
    for(int i = 0; i < 5; ++i) {
        int size = rand() % 10;
        for(int j=0; j < size; ++j) {
            array[i].push_back(j + i); //add data to those vector int's...
        }
    }
    
    for(int i = 0; i < array.size(); ++i) {
        for(int j = 0; j< array[i].size(); ++j) {
            cout << array[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1