STL passing vector fastly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Giulio

    STL passing vector fastly

    I'm doing a class who takes in constructor a vector<int> keeps and makes
    some statistic on the vector whitout modifiing it.

    I would like not to make a copy of the vector for the class. to let the full
    process be faster a smaller in memory.

    I tried to pass the vector by reference using * but that operator doesn't
    exist.

    my ask is: how the vector are passed to functions?

    thanx alot

    Giulio


  • Giulio

    #2
    Re: STL passing vector fastly

    passing simply the vectors I need it makes me an error in the while
    argument.
    how to solve this?

    thanx
    Giulio
    ---------------------------------
    std::vector<tip i::prezzo>::ite rator pr = prezzo_.begin() ;
    std::vector<tip i::operazione>: :iterator op = op_.begin();
    std::vector<tip i::prezzo>::ite rator finePr = prezzo_.end();
    std::vector<tip i::operazione>: :iterator fineOp = op_.end();

    while( op!=finePr && pr!=fineOp ) {

    ---------------------------------
    Compiler: Default compiler
    Building Makefile: "G:\bor\Makefil e.win"
    Executing make...
    make.exe -f "G:\borsa\Makef ile.win" all
    g++.exe -D__DEBUG__ -c valuta.cpp -o
    aluta.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C
    :/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" -g3

    valuta.cpp: In member function `attendibilita valuta::getAtte ndibilita()':
    valuta.cpp:33: no match for `__gnu_cxx::__n ormal_iterator< operazione*,
    std::vector<ope razione, std::allocator< operazione> > >& !=
    __gnu_cxx::__no rmal_iterator<p rezzo*, std::vector<pre zzo,
    std::allocator< prezzo> > >&' operator
    C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(cons t
    GUID&,
    const GUID&)
    valuta.cpp:33: no match for `__gnu_cxx::__n ormal_iterator< prezzo*,
    std::vector<pre zzo, std::allocator< prezzo> > >& !=
    __gnu_cxx::__no rmal_iterator<o perazione*, std::vector<ope razione,
    std::allocator< operazione> > >&' operator
    C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(cons t
    GUID&,
    const GUID&)
    valuta.cpp:45: warning: assignment to `int' from `attendibilita'
    valuta.cpp:45: warning: argument to `int' from `attendibilita'

    make.exe: *** [valuta.o] Error 1

    Execution terminated
    -----------------------------------


    Comment

    • David White

      #3
      Re: STL passing vector fastly

      Giulio <giulio.gL E V A@email.it> wrote in message
      news:oU3Ma.4237 $FI4.113215@tor nado.fastwebnet .it...[color=blue]
      > I'm doing a class who takes in constructor a vector<int> keeps and makes
      > some statistic on the vector whitout modifiing it.
      >
      > I would like not to make a copy of the vector for the class. to let the[/color]
      full[color=blue]
      > process be faster a smaller in memory.
      >
      > I tried to pass the vector by reference using * but that operator doesn't
      > exist.
      > my ask is: how the vector are passed to functions?[/color]

      Example:
      SomeClass::Some Class(const vector<int> &v)
      {
      vector<int>::co nst_iterator iv = v.begin();
      while(iv != v.end())
      {
      // do something
      ++iv;
      }
      }

      David



      Comment

      • David White

        #4
        Re: STL passing vector fastly


        Giulio <giulio.gL E V A@email.it> wrote in message
        news:X%3Ma.4240 $FI4.113145@tor nado.fastwebnet .it...[color=blue]
        > passing simply the vectors I need it makes me an error in the while
        > argument.
        > how to solve this?
        >
        > thanx
        > Giulio
        > ---------------------------------
        > std::vector<tip i::prezzo>::ite rator pr = prezzo_.begin() ;
        > std::vector<tip i::operazione>: :iterator op = op_.begin();
        > std::vector<tip i::prezzo>::ite rator finePr = prezzo_.end();
        > std::vector<tip i::operazione>: :iterator fineOp = op_.end();
        >
        > while( op!=finePr && pr!=fineOp ) {
        >
        > ---------------------------------
        > Compiler: Default compiler
        > Building Makefile: "G:\bor\Makefil e.win"
        > Executing make...
        > make.exe -f "G:\borsa\Makef ile.win" all
        > g++.exe -D__DEBUG__ -c valuta.cpp -o
        >[/color]
        luta.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C[color=blue]
        > :/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include" -g3
        >
        > valuta.cpp: In member function `attendibilita valuta::getAtte ndibilita()':
        > valuta.cpp:33: no match for `__gnu_cxx::__n ormal_iterator< operazione*,
        > std::vector<ope razione, std::allocator< operazione> > >& !=
        > __gnu_cxx::__no rmal_iterator<p rezzo*, std::vector<pre zzo,
        > std::allocator< prezzo> > >&' operator
        > C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(cons t
        > GUID&,
        > const GUID&)
        > valuta.cpp:33: no match for `__gnu_cxx::__n ormal_iterator< prezzo*,
        > std::vector<pre zzo, std::allocator< prezzo> > >& !=
        > __gnu_cxx::__no rmal_iterator<o perazione*, std::vector<ope razione,
        > std::allocator< operazione> > >&' operator
        > C:/Dev-Cpp/include/objbase.h:64: candidates are: BOOL operator!=(cons t
        > GUID&,
        > const GUID&)
        > valuta.cpp:45: warning: assignment to `int' from `attendibilita'
        > valuta.cpp:45: warning: argument to `int' from `attendibilita'[/color]

        Looks like type mismatches, but it's hard to tell exactly where. Please post
        the definitions of tipi::prezzo, tipi::operazion e, prezzo_ and op_ and it
        might be easier to see what is wrong.

        David



        Comment

        • Mike Wahler

          #5
          Re: STL passing vector fastly


          Giulio <giulio.gL E V A@email.it> wrote in message
          news:oU3Ma.4237 $FI4.113215@tor nado.fastwebnet .it...[color=blue]
          > I'm doing a class who takes in constructor a vector<int> keeps and makes
          > some statistic on the vector whitout modifiing it.
          >
          > I would like not to make a copy of the vector for the class. to let the[/color]
          full[color=blue]
          > process be faster a smaller in memory.
          >
          > I tried to pass the vector by reference using * but that operator doesn't
          > exist.[/color]

          The * operator does indeed exist, but what you're after
          is a reference:

          #include <vector>

          class X
          {
          public:
          X(const std::vector<int >& arg) : v(arg) { }
          private:
          const std::vector<int >& v;
          };
          [color=blue]
          >
          > my ask is: how the vector are passed to functions?[/color]

          You can pass them by value, by reference, or by pointer.

          -Mike



          Comment

          • MiniDisc_2k2

            #6
            Re: STL passing vector fastly


            "Giulio" <giulio.gL E V A@email.it> wrote in message
            news:oU3Ma.4237 $FI4.113215@tor nado.fastwebnet .it...[color=blue]
            > I'm doing a class who takes in constructor a vector<int> keeps and makes
            > some statistic on the vector whitout modifiing it.
            >
            > I would like not to make a copy of the vector for the class. to let the[/color]
            full[color=blue]
            > process be faster a smaller in memory.
            >
            > I tried to pass the vector by reference using * but that operator doesn't
            > exist.[/color]

            What you're saying is pass a reference to a pointer:

            void f (std::vector<in t>& *a);

            which is certainly not what you meant. You meant to say pass the vector by
            reference, or pass it using *, not both.
            [color=blue]
            >
            > my ask is: how the vector are passed to functions?
            >
            > thanx alot
            >
            > Giulio
            >
            >[/color]

            Not quite sure why your compiler wouldn't let you point to the vector, it is
            perfectly legal:

            void f(const std::vector<int > *a);

            but what you probably wanted was a constant reference to the vector:

            void f(const std::vector<int > &a);

            which allows you not to copy the vector, and ensure it doesn't change.
            Moreover, it is much faster, as you wanted. And, unlike pointers, it doesn't
            deal with mucking about with dereferencing, etc.

            -- MiniDisc_2k2
            To reply, replace nospam.com with cox dot net.


            Comment

            • Andrew Heath

              #7
              Re: STL passing vector fastly

              Giulio wrote:
              [color=blue]
              > passing simply the vectors I need it makes me an error in the while
              > argument.
              > how to solve this?
              >
              > thanx
              > Giulio
              > ---------------------------------
              > std::vector<tip i::prezzo>::ite rator pr = prezzo_.begin() ;
              > std::vector<tip i::operazione>: :iterator op = op_.begin();
              > std::vector<tip i::prezzo>::ite rator finePr = prezzo_.end();
              > std::vector<tip i::operazione>: :iterator fineOp = op_.end();
              >
              > while( op!=finePr && pr!=fineOp ) {[/color]


              I think what you mean is:

              while( op!=fineOp && pr!=finePr ) {

              Comment

              • Jerry Coffin

                #8
                Re: STL passing vector fastly

                In article <oU3Ma.4237$FI4 .113215@tornado .fastwebnet.it> , "Giulio"
                <giulio.gL E V A@email.it> says...[color=blue]
                > I'm doing a class who takes in constructor a vector<int> keeps and makes
                > some statistic on the vector whitout modifiing it.[/color]

                I would not pass the vector itself at all. Instead, I'd consider
                passing a pair of iterators, one to the beginning and the other to the
                end of the vector.

                --
                Later,
                Jerry.

                The universe is a figment of its own imagination.

                Comment

                • Giulio

                  #9
                  Re: STL passing vector fastly

                  > Instead, I'd consider[color=blue]
                  > passing a pair of iterators, one to the beginning and the other to the
                  > end of the vector.[/color]
                  wow this is a great idea...

                  thanx to all for the good answers
                  Giulio


                  Comment

                  Working...