Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-25-2005, 11:04 AM   #1 (permalink)
ziggi
Registered User
 
Join Date: Mar 2005
Posts: 7
ziggi is on a distinguished road
Problem with C++ Vector Size

Hey guys,

my code which was running fine with 2D arrays until I replaced arrays by 2D vectors and now it's giving segmentation fault. To spot out the problems i tried to find the size of vector but it's "0", i do not understand why it's 0 as it shud b 5x2 or 10???? even when i try to print the capacity of vector A (which being 2D vector)
shud b 5x2 or 10 is coming 5 ????

Plz help !

part of code is

Code:
int Nv = 5;
int Nevx = 2;
int MAX = 99999;

vector<vector<int> > A;
  A.reserve(Nv);
  for (int i=0;i<Nv;i++){ 
    A[i].reserve(Nevx);
    for (int j=0;j<Nevx;j++){
      A[i][j] = MAX;
    }
  }

cout <<"A size = " << A.size()<<endl;
cout <<"A capacity = " << A.capacity()<<endl;

Last edited by Valmont; 03-25-2005 at 04:25 PM.
ziggi is offline   Reply With Quote
Old 03-25-2005, 06:03 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
For the outer std::vector you can't use "reserve()" because the std::vector can't know how to align the memory. Realize that the inner std::vector can vary in size per outer element!

Here are a few solutions.

1) use resize():
Code:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
  int Nv = 5;
  int Nevx = 2;
  int MAX = 99999;
  
  vector<vector<int> > A;
  
  A.resize(Nv);
  for(unsigned i=0; i<Nv; ++i)
  {
    for (int j=0; j<Nv; ++j)
    {
      A[j].reserve(Nevx);
      A[i][j] = MAX;
      cout<<"A["<<i<< "]["<< j<< "]= "<<A[i][j]<<endl;
    }
  }  
    
  return 0;
}
2) Resize at creation
Code:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
  int Nv = 5;
  int Nevx = 2;
  int MAX = 99999;
  
  vector<vector<int> > A(Nv);
  
  for(unsigned i=0; i<Nv; ++i)
  {
    for (int j=0; j<Nv; ++j)
    {
      A[j].reserve(Nevx);
      A[i][j] = MAX;
      cout<<"A["<<i<< "]["<< j<< "]= "<<A[i][j]<<endl;
    }
  }
  
  return 0;
}
TIP 1:
Besides, if you are absolutely sure that your 2D vector is going to hold numbers only, then consider learning the ways of std::valarray as the inner of std::vector.

TIP 2:
Try to use "unsigned" integers for vector indices by heart. If your vector contains more elements then the signed integer can handle then weird things will happen. A vector guarantees access times of O(n) so it is very feasable to assume that vectors will have lots of elements. Be prepared.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 10:43 PM
C problem saurabh1905 Standard C, C++ 2 06-11-2004 02:00 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 01:22 AM
edit? anon Lounge 10 11-21-2002 04:02 PM


All times are GMT -8. The time now is 08:56 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting