Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 31.05.2014, 23:42   #1
Вероника99
Форумчанин
 
Регистрация: 15.12.2013
Сообщений: 414
По умолчанию Выделение памяти. Создать шаблонный класс-матрица

Я считываю с файла значения от 1 до 1000000,а потом в методе "copycolumn" копирую одну колонку из матрицы в другой массив. Программа запускается,долго выполняется и потом выдает сообщения,что там users32.dll нарушено... В чем проблема?
Код:
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <locale.h>
using namespace std;
#define K 1000010
template <typename T>
class array
{
private:
    T **p;
    int size;
    int row,column;
    T *mtx;
public:
 
    array(int a,int b)
    {
        row=a;column=b;
        size=a*b;
        //cout<<" a= "<<a;
        p=new T *[a*b];
        for(int i=0;i<(a*b);i++)
        {
            p[i]=new T[(a*b)];
        
        }
    /*  mtx=new T*[a];
        for(int i=0;i<(a*b);i++)
        {
            mtx[i]=new T[a];
        }
 
    }*/
    }
    ~array(){for(int i=0;i<row*column;i++) delete [] p[i]; cout<<"Done"; };
//  void enter();
    void enter( ifstream &,int);
    void copycolumn(int);
    int GetRow(){return row;}
 
    void show();
 
    int GetColumn(){return column;}
 
    
};
 
 
int main()
{
    setlocale(LC_ALL,"Rus");
    int u=1000,v=1000;
 
    array<int>myArray(u,v);
     int sym=1;
 
    
    ifstream ifile;        
    
    if(sym==1)
    {
        
        char fname[30]="D:\\100.txt";
        ifile.open(fname); // вхідний потік
         myArray.enter(ifile,sym);  
         ifile.close();  
    }
    
    //myArray.show();
    myArray.copycolumn(u);
 
}
 
template <typename T>
void array<T>::copycolumn(const int u)
{
    //string **c=new string*[column*row];
    T *c[K]; //Здесь вместо К я пробовала просто колонки*строки,но компилер выдает:неизвестный размер 
    for(int i=0;i<row;i++)
        {
            c[i]=new T[u];
 
        
        }
    int j=1;
    cout<<"\nCopy column:\n";
    for(int i=0;i<row;i++)
    {   
        c[i]=p[i];
        cout<<c[i][j]<<" ";
        delete [] c[i];
        cout<<"\n";
    }
 
    /*  for(int i=0;i<row;i++)
        {
            c[i]=delete c[i];
 
        
        }*/
 
 
 
/*  char fname1[30]="D:\\6.txt";
    ofstream ofs(fname1,ios::out);
                if(!ofs)
            {
                cout<<"Not found file name\n";
                exit(1);
            }
                for(int i=0;i<row;i++)
    {   
        c[i]=p[i];
        ofs<<c[i][j]<<" ";
        ofs<<"\n";
    }
                ofs.close();*/
}
 
 
 
template <typename T>
void array<T>::show()
{
    int temp;
    for(int i=0;i<row;i++)
                {
                    for(int j=0;j<column;j++)
                
                    cout<<p[i][j]<<" ";
                    cout<<"\n";
                
                }
                
}
template <typename T>             
void array<T>::enter(ifstream& ifile,int sym)
{       
        if(sym==1)
        {
            for(int i=0;i<row;i++)
          for( int j=0;j<column;j++)
        {            
            
            ifile>>p[i][j];
    //      cout<<" "<<p[i][j];
        
        }
        
        }
 
        
            
}
Изображения
Тип файла: jpg 4.JPG (19.5 Кб, 65 просмотров)
Вероника99 вне форума Ответить с цитированием
Старый 01.06.2014, 01:41   #2
Вероника99
Форумчанин
 
Регистрация: 15.12.2013
Сообщений: 414
По умолчанию

Никто не знает?((((
Вероника99 вне форума Ответить с цитированием
Старый 01.06.2014, 03:18   #3
BDA
МегаМодератор
СуперМодератор
 
Аватар для BDA
 
Регистрация: 09.11.2010
Сообщений: 7,342
По умолчанию

Код:
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <locale.h>
using namespace std;

template <typename T>
class array
{
private:
    T **p;
    int row, column;
public:
    array(int a,int b)
    {
        row = a;
        column = b;
        p = new T*[row];
        for (int i = 0; i < row; ++i)
            p[i] = new T[column];
    }
    ~array()
    {
        for (int i = 0; i < row; ++i)
            delete[] p[i];
        delete[] p;
    };
    void enter(ifstream &ifile, int sym)
    {
        if (sym == 1)
            for(int i = 0; i < row; ++i)
                for(int j = 0; j < column; ++j)
                    ifile >> p[i][j];
    }
    void copycolumn(T *a, int u)
    {
        for (int i = 0; i < row; ++i)
            a[i] = p[i][u];
    }
    int GetRow(){return row;}
    int GetColumn(){return column;}
    void show()
    {
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < column; ++j)
                cout << p[i][j] << " ";
            cout << endl;
        }
    }
};

int main()
{
    setlocale(LC_ALL,"Rus");
    int u = 1000, v = 1000;
    array<int>myArray(u, v);
    int sym = 1;
    ifstream ifile;        
    if (sym == 1) {
        char fname[30] = "D:\\100.txt";
        ifile.open(fname);
        myArray.enter(ifile, sym);  
        ifile.close();  
    }
    myArray.show();
    int *s = new int[u];
    myArray.copycolumn(s, 0);
    cout << "first column:" << endl;
    for (int i = 0; i < u; ++i)
        cout << s[i] << endl;
    delete[] s;
}
Пишите язык программирования - это форум программистов, а не экстрасенсов. (<= это подпись )
BDA вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Создать шаблонный класс - двумерный динамический массив Вероника99 Помощь студентам 0 28.04.2014 20:51
Создать Класс матрица Olga@->-->-- Помощь студентам 0 26.11.2012 23:29
Класс string, выделение памяти Стремящийся Общие вопросы C/C++ 1 17.03.2012 14:21
Шаблонный класс С++ MAO25 Помощь студентам 0 21.05.2010 23:46