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

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2005, 09:33 AM   #16 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
By the way, the implementation of the inspector get_internal_rep() is out of order! My bad.
Change full header to this:
Code:
////////////////////////////////////////////////
// A 2D matrix, demonstrating the following:  //
// - a nested std::vector in std::vector.     //
// - various basic iterator techniques.       //
// - basic exception error handling.          //
////////////////////////////////////////////////

#ifndef C2DMATRIX_H
#define C2DMATRIX_H

#include <vector>
#include <string>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <iterator>

using namespace std;

class C2DMatrixError
{
public:
  C2DMatrixError(std::string msg) : sMsg_(msg)
    {}
  virtual std::string what() = 0;
protected:
  std::string sMsg_;
};

//--

class BadSize : public C2DMatrixError
{
public:
  BadSize(std::string msg) : C2DMatrixError(msg)
    {}
  std::string what()
  {
    return sMsg_;
  } 
};

//--

class BoundsViolation : public C2DMatrixError
{
public:
  BoundsViolation(std::string msg) : C2DMatrixError(msg)
    {}
  std::string what()
  {    
    return sMsg_;
  }
};

//---------------------- C2DMatrix --------------------------

template<typename T>
class C2DMatrix 
{
public:
  C2DMatrix() {}
  C2DMatrix(unsigned, unsigned );
  
  T& operator() (unsigned, unsigned);
  const T& operator() (unsigned , unsigned) const;
   size_t nRows_;
   size_t nCols_;
   
   // Remember, "typename" is required because "T" can't be resolved yet.
  const typename std::vector<std::vector<T> >::iterator first_row()
  {
    return data_.begin();
  }
  const typename std::vector<std::vector<T> >::iterator last_row()
  {
    data_.end();
  }
   
private:
  vector<vector<T> > data_;
  template <typename _T>
  friend ostream& operator<<(ostream&  os, C2DMatrix<_T>& c2d );
};

//--------------------------------------------------------------
 
template<typename T>
C2DMatrix<T>::C2DMatrix(unsigned rows, unsigned cols)  
{
  if (rows == 0 || cols == 0)
  {
    throw BadSize("ERROR: Bad size");
  }
  data_.resize(rows);
  nRows_=rows;
  nCols_=cols;
  for (unsigned i = 0; i < rows; ++i)
  {
    data_[i].resize(cols);
  }
}
 
//----------------------------

template<typename T>
inline T& C2DMatrix<T>::operator() (unsigned row, unsigned col)
{
  if (row >= nRows_ || col >= nCols_ ) 
  {
    throw BoundsViolation("ERROR in non-const operator(): Bounds Violation");
  }
  return data_[row][col];
} 
 
//----------------------------

template<typename T>
inline const T& C2DMatrix<T>::operator() (unsigned row, unsigned col) const
{
  if (row >= nRows_ || col >= nCols_)
  {
    throw BoundsViolation("ERROR in const operator()const: Bounds Violation");
  }
  return data_[row][col];
}
 
//-----------------------------

template <typename T>
class print_column
{
public:
   void operator()(const std::vector<T>& v)
   {
      std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
      std::cout << endl;
   }
};

//-------------------------------

template <typename T>
ostream& operator<< (std::ostream&  os, C2DMatrix<T>& matrix)
{
  std::for_each( matrix.first_row(), matrix.last_row(), print_column<T>() );
  return os; 
}

#endif //C2DMATRIX_H
__________________
Valmont is offline   Reply With Quote
Old 02-26-2005, 04:40 AM   #17 (permalink)
udayankhurana
Registered User
 
Join Date: Feb 2005
Posts: 1
udayankhurana is on a distinguished road
Soln

Dear,

Make your last statement as :

*start->*array[1][2]=2;//Works!

In place of ;

*start->array[1][2]=2;//Does not work!
udayankhurana 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
Different string types in C: char pointer, char array, etc. Sleep Standard C, C++ 0 01-23-2005 08:40 PM
working with Multi Dimensional Arrays sde PHP 7 07-11-2004 01:43 PM
sorting multi dimensional arrays sde PHP 4 10-01-2003 05:52 PM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 02:06 PM.


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