Sponsored Content
Top Forums Programming multidimensional array using c++ vector Post 302307677 by carl.alv on Thursday 16th of April 2009 04:27:30 AM
Old 04-16-2009
multidimensional array using c++ vector

Hi! I need to make dynamic multidimensional arrays using the vector class. I found in this page How to dynamically create a two dimensional array? - Microsoft: Visual C++ FAQ - Tek-Tips the way to do it in 2D, and now i'm trying to expand it to 3D but i don't understand how is the operator[] working, so i have problems. Here is my code:

Code:
#include <iostream>
#include <vector>
using namespace std;

template <typename T>
class Matrix {
protected:
  std::vector<std::vector<T> > m_data;
public:
  Matrix() {}
  Matrix(int rows, int cols) : m_data(rows, std::vector<T>(cols)) {}
  ~Matrix() {}

  inline void get_size(int& rows, int& cols) {
    rows = m_data.size();
    cols = m_data[0].size(); }
  //operators
  inline std::vector<T> & operator[](int i) { return m_data[i]; }
  inline const std::vector<T> & operator[] (int i) const { return m_data[i]; }
};

template <typename T>
class MatrixTD {
protected:
  Matrix<std::vector<T> > m_data; 
  
public:
  MatrixTD() {}
  MatrixTD(int rows, int cols, int lays) : m_data(rows, cols) {
    for(int i = 0; i < rows; ++i)
      for(int j = 0; j < cols; ++j)
	m_data[i][j].resize(lays); }
  MatrixTD(int rows, int cols, int lays, T val) : m_data(rows, cols) {
    for(int i = 0; i < rows; ++i){
      for(int j = 0; j < cols; ++j){
	m_data[i][j].resize(lays);
	for(int k = 0; k < lays; ++k)
	  m_data[i][j][k] = val;
      }
    } }
  ~MatrixTD() {}
  
  inline void get_size(int& rows, int& cols, int& lays) {
    m_data.get_size(rows, cols);
    lays = m_data[0][0].size(); }
  //operators
  inline std::vector<T> & operator[](int i) { return m_data[i]; }
  inline const std::vector<T> & operator[] (int i) const { return m_data[i]; }
};

int main(){
  int size = 5;
  int s1, s2, s3;
  MatrixTD<int> m3Dd(size, size, size, 0);
  m3Dd.get_size(s1, s2, s3);
  for(int i = 0; i < s1; ++i){
    for(int j = 0; j < s2; ++j){
      for(int k = 0; k < s3; ++k){
	cout<<m3Dd[i][j][k]<<" ";
      }
      cout<<endl;
    }
    cout<<endl;
  }

  return 0;
}

And the compiler tels me:

vector3D.cpp: In member function ‘std::vector<T, std::allocator<_CharT> >& MatrixTD<T>::operator[](int) [with T = int]':
vector3D.cpp:113: instantiated from here
vector3D.cpp:47: error: invalid initialization of reference of type ‘std::vector<int, std::allocator<int> >&' from expression of type ‘std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >'

Note: I can access the data if I make m_data public and I use m_data[][][], but the operator[] doues not work in the 3D case.

Thank you in advance for any help!

Last edited by Yogesh Sawant; 04-17-2009 at 06:02 AM.. Reason: removed smilies in text
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

multidimensional array in perl

i'm trying to open a file with three or more columns and an undetermined, but finite number of rows. I want to define an array for each row with each element of the row as a sub array. The columns are separated by tabs or spaces. Here's the file: 12x3.12z34b.342sd3.sds 454.23.23.232 ... (9 Replies)
Discussion started by: prkfriryce
9 Replies

2. Shell Programming and Scripting

Awk multidimensional Array

Hello Experts,, Can anybody give me a brief idea what is following bold letter statement is for!! what is the term called so that I can google for it.. It seems to be an array inside another array.. awk' /TXADDR/ { txaddr=$NF } ##understood /TXDATA/ { txdata]=$NF... (1 Reply)
Discussion started by: user_prady
1 Replies

3. Shell Programming and Scripting

AWK multidimensional array

In a single dim. awk array, we can use : <index> in <array name> to determine whether a particualar index exists in the array or not. Is there a way to achieve this in a awk multi dim. array ? (4 Replies)
Discussion started by: sinpeak
4 Replies

4. Shell Programming and Scripting

multidimensional array in awk

Hi, I was trying to process a file with the help of awk. I want to first display all the rows that contains 01 and at the end of processing I have to print some portion of all the lines. like below. Output expected: (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

5. Programming

Multidimensional array of strings with vector.

I've been struggling with this for quite some time. I decided I should get some help with this. Nothing is working. I'm getting a segmentation fault or out of bounds error when I try to load the entries in the for loop.I'm really frustrated. :mad: Compiling isn't the problem. It's crapping out on... (5 Replies)
Discussion started by: sepoto
5 Replies

6. Programming

Sorting a multidimensional vector by a specific field.

In some cases I would like to sort by index, in some cases by color and in some cases by Callsign. Can this be done? :D vector< vector<string> > table; vector<string> row; row.push_back("1");row.push_back("green");row.push_back("alpha"); table.push_back(row);... (0 Replies)
Discussion started by: sepoto
0 Replies

7. Shell Programming and Scripting

gawk - How to loop through multidimensional array?

I have an awk script that I am writing and I needed to make use of a multidimensional array to hold some data... Which is all fine but I need to loop through that array now and I have no idea how to do that. for a regular array, the following works: ARRAY for(var in ARRAY) { ... } ... (5 Replies)
Discussion started by: trey85stang
5 Replies

8. Shell Programming and Scripting

Multidimensional array:awk error

awk -F'\t' -v OFS='\t' ' { if($2 in arr) { #print "Sahi", NR,arr for(k=2;k<=NF;k++){ # sum]+=$2 } } else { arr=NR #print "awk",NR for (k=3;k<=NF ; k++){ sum=$k } } } (7 Replies)
Discussion started by: genome
7 Replies

9. Shell Programming and Scripting

Multidimensional array

I am learning about bash system variables, such as $ , @ and #. I have this piece of script implementing an array and it is doing its job just fine. This is not the only array I will be using. Just for ease of maintenance and more coding I would like to have the arrays in two dimensional... (4 Replies)
Discussion started by: annacreek
4 Replies

10. Shell Programming and Scripting

Sort multidimensional Array

Hello I have a problem. I create a Multidimensional Array Like this: ENTRY="$kunnum-$host" ENTRY="$host" ENTRY="# $3" for key in "${!ENTRY}"; do ENTRIES=${ENTRY} # INDEX=IP(5) donedeclare -p declare -A ENTRIES=(="unas15533" ="unas" ="# RDP-Terminal 2"... (12 Replies)
Discussion started by: Marti95
12 Replies
menu_format(3X) 														   menu_format(3X)

NAME
menu_format - set and get menu sizes SYNOPSIS
#include <menu.h> int set_menu_format(MENU *menu, int rows, int cols); void menu_format(const MENU *menu, int *rows, int *cols); DESCRIPTION
The function set_menu_format sets the maximum display size of the given menu. If this size is too small to display all menu items, the menu will be made scrollable. If this size is larger than the menus subwindow and the subwindow is too small to display all menu items, post_menu() will fail. The default format is 16 rows, 1 column. Calling set_menu_format with a null menu pointer will change this default. A zero row or column argument to set_menu_format is interpreted as a request not to change the current value. The function menu_format returns the maximum-size constraints for the given menu into the storage addressed by rows and cols. RETURN VALUE
These routines returns one of the following: E_OK The routine succeeded. E_SYSTEM_ERROR System error occurred (see errno). E_BAD_ARGUMENT Routine detected an incorrect or out-of-range argument. E_POSTED The menu is already posted. E_NOT_CONNECTED No items are connected to the menu. SEE ALSO
curses(3X), menu(3X). NOTES
The header file <menu.h> automatically includes the header file <curses.h>. PORTABILITY
These routines emulate the System V menu library. They were not supported on Version 7 or BSD versions. AUTHORS
Juergen Pfeifer. Manual pages and adaptation for new curses by Eric S. Raymond. menu_format(3X)
All times are GMT -4. The time now is 04:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy