zeiger für ein mehrdimensionales Array



  • wie ich auf ein Eindimensionales Array in einer Funktion zugreife ist mir klar.
    Einfach den Zeiger übergeben.

    void main()
    {
       string array[1];
       funk(array);
    }
    void funk(string *array)
    {
      array[0]="abc";
    }
    

    wie geht das bei mehrdimensionalen Arrays ?

    void main()
    {
       string array[1][1];
       funk(array);
    }
    void funk(string *array)
    {
      array[0][0]="abc";
    }
    

    funktioniert leider nicht.



  • weiß also keiner oder geht nich.



  • Das mußt du etwas anders angehen.

    void funk(std::string &array)
    {
        array = "abc";
    }
    
    void main()
    {
        std::string array[1][1];
        funk(array[0][0]);
    }
    

    Ausserdem solltest du dir std::vector anschauen, damit könntest du dein Problem auch lösen.

    [ Dieser Beitrag wurde am 27.05.2003 um 23:22 Uhr von Artchi editiert. ]



  • @Artchi
    Das ist nun aber was anderes 😉

    @Newbie

    void funk(string array[][1]);

    So müsste es gehen. Es muss mindestens eine Dimension von der Größe bekannt sein.



  • dass es immer noch void main gibt...
    und es muss nicht mindestens eine dimension bekannt sein, es muessen alle bis auf die aeusserste bekannt sein.



  • hi, so geht's z.b. für drei dimensionen... ist zwar kompliziert, aber es geht.
    ich habe mal eine n-dimensionale gitterstruktur mit templates implementiert 🕶 . wenn du sie willst kannst du sie haben... anonsten versuch es mal so...

    float *** A;
    
      A = new float ** [N];  
    
      for (i = 0; i<N; i++) {
        A[i] = new float * [N];
        for (j = 0; j<N; j++)    
          A[i][j] = new float [N];  
      }
    

    greets psi



  • #include <vector>
    #include <memory>
    #include <string>
    using namespace std;
    
    int main () {
    
      vector<auto_ptr<vector<auto_ptr<vector<auto_ptr<string> > > > > > multi_vec;
    
      //mit new etc. füllen
    
      void fubar (vector<auto_ptr<vector<auto_ptr<vector<auto_ptr<string> > > > > > &);
    
      //und aufrufen:
      fubar(multi_vec);
    }
    


  • Original erstellt von psi:
    ich habe mal eine n-dimensionale gitterstruktur mit templates implementiert 🕶 . wenn du sie willst kannst du sie haben...

    Echt, cool? Kannst du deinen Header hier mal posten?



  • Ja klar. Hier ist der Code (habe die Klasse ohne spezielle Zusatzfunktionen kopiert, sonst wäre es doch etwas zu lang...). Unten ist noch eine Beispiel .cpp File angegeben, das erklärt wie man sie benutzt... thy & have fun! 🙂

    #include<iostream>
    #include<cmath>
    #include<vector>
    
    using namespace std;
    
    //---------------------------------------------------------------------
    // LATTICE: by psi2@freesurf.ch
    //---------------------------------------------------------------------
    template <class T> class lattice{
      private:
        vector<T> L;                  //the lattice
        long N_SITES;           //number of sites of the lattice
      public:
        const int D;                        //dimensions
        const int N;                        //size of the lattice
        lattice(const int,const int);   //constructor
        void store(const vector<int>&, T);    //store a value of type T at position (pos definded by vector, i,j,k,...)
        T get(const vector<int>&);        //get a value of type T at position (pos defined by vector, i,j,k,..)
        long index(const vector<int>&);   //returns the index of the lattice vector in function of i,j,k,...
        void run_tl(const vector<T>&, lattice<T>&, const T (*func)(const vector<T>&, const vector<T>&, lattice<T>&)); 
                            //run through lattice with pointer to function to do
    };
    
    //---------------------------------------------------------------------
    // CONSTRUCTOR: build of the lattice (vector)
    //---------------------------------------------------------------------
    template <class T> lattice<T>::lattice(const int dim, const int size) : N(size), D(dim){
      int resize_L=int(pow(double(N),double(D)));
      L.resize(L.size()+resize_L);
      N_SITES=L.size();
    }
    
    //---------------------------------------------------------------------
    // STORE: stores a value at the chosen point of type T
    //---------------------------------------------------------------------
    template <class T> inline void lattice<T>::store(const vector<int>& indvec, const T value){
      long int_index= 0;
      int_index= index(indvec);
      L[int_index]=value;
    }
    
    //--------------------------------------------------------------------- 
    // GET: returns a value at the chosen point
    //---------------------------------------------------------------------
    template <class T > inline T lattice<T>::get(const vector<int>& indvec){
      long int_index= 0;
      int_index= index(indvec);
      return L[int_index];
    }
    
    //---------------------------------------------------------------------
    // INDEX: returns the vector index (long int) due to matrix indizes i,j,k,..
    //---------------------------------------------------------------------
    template <class T> inline long lattice<T>::index(const vector<int>& indvec){
      long int_index=0;
      int N_pow=1;                       // for generating 1,N,N^2, ...
      for(int i=0;i<D;i++){
        int_index+=indvec[i]*N_pow;      // a(i,j,k,l,...)=i + N*j + N^2*k + N^3*l
        N_pow*=N;                        // generates: 1, N, N^2, ...
      }
      return int_index;
    }
    
    //---------------------------------------------------------------------
    // RUN_TL: run through the lattice        
    //---------------------------------------------------------------------
    template <class T> inline void lattice<T>::run_tl(const vector<T>& pars, lattice<T>& l, const T (*func)(const vector<T>&, const vector<T>&, lattice<T>&)){
      vector<int> ind(D);;
      for(long cnt=0;cnt<N_SITES;++cnt){
        if(ind[0]!=0 && ind[0]%(N)==0){
          ind[0]=0;
          for(int t=1;t<D;t++){
            if(ind[t]!=0 && ind[t]%(N-1)==0){
              ind[t]=0;
              }
            else {ind[t]++; break;}
              }
            }
          // statement starts here
          func(pars,ind,l);
          // statement stops here
          ind[0]=ind[0]+1;
        }   
      }
    
    // -------------------------------------------------------
    // AN EASY EXAMPLE HOW TO USE THE HEADER 
    
    // #include</home/dani/data/ethz/st/landau/p_lattice.h>
    
    // const int fill(const vector<int>& pars, const vector<int>& ind, lattice<int>& lat){
    //   lat.store(ind,pars[0]+pars[1]+pars[2]); // store the sum over all parameters...
    // }
    // 
    // 
    // int main(){
    // int size=20;
    // int dim=5;
    // // Initializes the Lattice
    // lattice<int> lat(dim,size);
    // // Build a Index Vector for 5 Dim
    // vector<int> index(dim);        // i,j,k,l,m, for 5 dimensions
    // index[0]=3;          // i=3
    // index[1]=13;         // j=13
    // index[2]=5;          // k=5 etc...
    // index[3]=17;
    // index[4]=8;
    // 
    // lat.store(index,25);     // stores the value 25 at the point 3,13,4,5,17,8
    // cout << lat.get(index) << '\n'; // gets the value
    // 
    // 
    // // ADVANCED ;-)
    // // this is a run through the whole lattice (run_tl) applying the function fill which is defined above... fill
    // // In other words apply at each point of the lattice the function fill!
    // // the number of parameters you need can be defined by the size of the vector pars. see below...
    // 
    // const int (*ptfun)(const vector<int>&, const vector<int>&, lattice<int>& );
    // ptfun = &fill;
    // vector<int> pars(3);
    // pars[0]=3; pars[1]=2; pars[2]=5;  // arbitrary parameters
    // lat.run_tl(pars,lat,ptfun);
    // 
    // cout << lat.get(index) << '\n';
    // cout << "Total Number of Sites = " << "N^D= " << lat.N << "^" << lat.D << "= " << int(pow(double(lat.N),double(lat.D))) << "!!! ;-)" << '\n';
    // 
    // 
    // 
    // }
    

Anmelden zum Antworten