boost::array call by reference...



  • hi!
    ich versuch gerade das 2 dim. boost array via call by reference mit nem pointer zu übergeben...mit ner referenz funzt es...aber pointer nicht;-(

    cu

    #include <algorithm>
    #include <functional>
    #include <string>
    #include <iostream>
    #include <boost/array.hpp>
    
    typedef boost::array<boost::array<std::string,4>,4> boost4x4;
    
    void DoSomething(const boost4x4& array)
    {
    	std::cout << array[0][0] << std::endl;
    }
    
    void DoSomething(boost4x4* array)
    {
             std::cout << *array[0][0] << std::endl;
    }
    
    int main()
    {
        // array of arrays of seasons
        boost4x4 seasons_i18n = {
            { { { "spring", "summer", "autumn", "winter", } },
              { { "Fruehling", "Sommer", "Herbst", "Winter" } }
            }
        };
    
    	DoSomething(seasons_i18n);
    	DoSomething2(&seasons_i18n);
    
    	seasons_i18n[2][0] = "1";
    	seasons_i18n[2][1] = "2";
    	seasons_i18n[2][2] = "3";
    	seasons_i18n[2][3] = "4";
    
        // for any array of seasons print seasons
        for (unsigned i=0; i<seasons_i18n.size(); ++i) {
            boost::array<std::string,4> seasons = seasons_i18n[i];
            for (unsigned j=0; j<seasons.size(); ++j) {
                std::cout << seasons[j] << " ";
            }
            std::cout << std::endl;
        }
    
        // print first element of first array
        std::cout << "first element of first array: "
                  << seasons_i18n[0][0] << std::endl;
    
        // print last element of last array
        std::cout << "last element of last array: "
                  << seasons_i18n[seasons_i18n.size()-1][seasons_i18n[0].size()-1]
                  << std::endl;
    
    	std::cin.get();
    
        return 0;
    }
    


  • void DoSomething2(boost4x4* array)
    {
    	std::cout << array->size() << std::endl;
    }
    

    ich hab so das gefühl das der operator[][] da nicht verwendet werden kann?

    cu



  • boost_coder schrieb:

    ich hab so das gefühl das der operator[][] da nicht verwendet werden kann?

    und ist er nicht willig, so versuch's mit klammern.

    //ungetestet, aber sieht rgendwie lecker aus
    (*array)[0][0]
    


  • nett (aber fast unbekannt) sind auch lokale referenzen

    void DoSomething(boost4x4* array) 
    { 
             boost4x4& a=*array;//only for easy handling
             std::cout << a[0][0] << std::endl; 
    }
    


  • ach danke;-)

    void DoSomething2(boost4x4* array)
    {
        array::const_iterator pos;
    
        for (pos=array->begin(); pos<array->end(); ++pos) {
            std::cout << *pos << ' ';
        }
    }
    
    c:\Informatik\Temp3\main.cpp(104) : fatal error C1001: INTERNAL COMPILER ERROR
            (compiler file 'msc1.cpp', line 2701) 
             Please choose the Technical Support command on the Visual C++ 
             Help menu, or open the Technical Support help file for more information
    
    gemeint ist line: array::const_iterator pos;
    

    hm 😕

    thx..cu



  • boost4x4::const_iterator pos;



  • boost_coder schrieb:

    for (pos=array->begin(); pos<array->end(); ++pos) {
    

    man nimmt lieber != statt <, denn != klappt effizient bei den verrücktesten iteratoren, während < bei manchen iteratoren lahm ist. willst ja nicht immer nachdenken müssen, ob jetzt != oder < gut ist.



  • leider werd ich da nun 2 iteratoren brauchen?
    damit ich dann in den arrays eines arrays weiter gehen kann?

    cu



  • volkard schrieb:

    man nimmt lieber != statt <, denn != klappt effizient bei den verrücktesten iteratoren, während < bei manchen iteratoren lahm ist. willst ja nicht immer nachdenken müssen, ob jetzt != oder < gut ist.

    Vor allem muss es dann nicht zwangsläufig ein Random Access Iterator sein.



  • boost4x4::const_iterator pos;
    
    for (pos=array->begin(); pos<array->end(); ++pos) {
    	std::cout << (*pos)[0] <<' ' << (*pos)[1] <<' ' << (*pos)[2] <<' ' << (*pos)[3] << std::endl; 
        }
    

    ich hab das etwas kompliziert...wie kann ich da mit einen 2ten interator auf die arrays eines array zugreifen?

    cu



  • for (pos = array->begin(); pos != array->end(); ++pos) {
    

    cu



  • typedef boost4x4::const_iterator It; 
    
    for (It pos1=array->begin(); pos1!=array->end(); ++pos1) 
      for (It pos2=pos1->begin(); pos2 != pos1->end(); ++pos2) { 
        std::cout << (*pos) <<' '; 
      }
    std::cout << std::endl;
    

    So zum Beispiel?



  • Sorry, hab mich vertan.

    typedef boost4x4::const_iterator It1; 
    typedef boost::array<std::string,4>::const_iterator It2;
    
    for (It1 pos1=array->begin(); pos1!=array->end(); ++pos1) 
      for (It2 pos2=pos1->begin(); pos2 != pos1->end(); ++pos2) { 
        std::cout << (*pos2) <<' '; 
      }
    std::cout << std::endl;
    


  • boost4x4 *DoSomething(boost4x4* array)
    {
    	if((*array)[0][0] == "spring")
    		return (&array)[0];
    
    	return (&array)[3];
    }
    

    aufruf:

    std::cout << &DoSomething(&seasons_i18n)[0] << std::endl;
    

    nun will ich einen pointer auf das erste array zurückgeben...
    wenn ich nun so wie oben die funktion aufrufe, warum wird dann eine adresse ausgegeben? will nun das erste element ausgeben auf das array der pointer zeigt...

    cu



  • return &(*array)[3];
    

    Und &DoSomething ist auch nicht richtig, du meintest wohl eher

    std::cout << (*DoSomething(&seasons_i18n))[0] << std::endl;
    


  • hm...klappt leider auch nicht!!

    bei: return &(*array)[0];
    c:\Informatik\Temp3\main.cpp(102) : error C2440: 'return' : cannot convert from 'boost::array<T,N> *__w64 ' to 'boost4x4 *'
            with
            [
                T=std::string,
                N=4
            ]
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    
    bei: bei: return &(*array)[3];
    c:\Informatik\Temp3\main.cpp(104) : error C2440: 'return' : cannot convert from 'boost::array<T,N> *__w64 ' to 'boost4x4 *'
            with
            [
                T=std::string,
                N=4
            ]
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    
    bei: std::cout << (*DoSomething(&seasons_i18n))[0] << std::endl;
    c:\Informatik\Temp3\main.cpp(137) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'boost::array<T,N>' (or there is no acceptable conversion)
            with
            [
                T=std::string,
                N=4
            ]
    


  • (sachen, die ich nicht mag, hab ich gelöscht)
    volkard


Anmelden zum Antworten