Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Is it to return the address of an element in a vector

Is it to return the address of an element in a vector

Scheduled Pinned Locked Moved ATL / WTL / STL
graphicsquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    std::vector<myclass> vec;
    //...
    MyClass & get_selected()
    {
    std::list<myclass>::iterator it=vec.begin();
    for (;it!=vec.end();++it) {
    if (it->is_selected())
    return &*it; // Is it the address of the element in the vector,
    // and is it safe?
    }

    return NULL;
    

    }

    R S J M 4 Replies Last reply
    0
    • F followait

      std::vector<myclass> vec;
      //...
      MyClass & get_selected()
      {
      std::list<myclass>::iterator it=vec.begin();
      for (;it!=vec.end();++it) {
      if (it->is_selected())
      return &*it; // Is it the address of the element in the vector,
      // and is it safe?
      }

      return NULL;
      

      }

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      followait wrote:

      Is it the address of the element in the vector, and is it safe?

      Yes, &*it is the address of the vector element that the iterator refers to. Whether it's "safe" or not depends on what you mean by "safe". It is safe regarding if the address returned really is the address of the contained element. But considering that you are handing out a non-const pointer or reference to the object, the caller may change it at will. This could of course be considered as "not safe" in some way. However, your code snippet doesn't seem to compile correctly. In your function declaration you say that the return type is a reference to a myclass object, but you actually return a pointer to it unless you've omitted something important. The return statement should read 'return *it;'.

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      1 Reply Last reply
      0
      • F followait

        std::vector<myclass> vec;
        //...
        MyClass & get_selected()
        {
        std::list<myclass>::iterator it=vec.begin();
        for (;it!=vec.end();++it) {
        if (it->is_selected())
        return &*it; // Is it the address of the element in the vector,
        // and is it safe?
        }

        return NULL;
        

        }

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        Design suggestion: you're probably better off following what the STL algorithms do in this case, and return an iterator:

        std::vector<myclass> vec;
        //...
        std::vector<myclass>::iterator get_selected()
        {
          for (std::vector<myclass>::iterator it=vec.begin();it!=vec.end();++it)
          {
            if (it->is_selected())
              return it;
          }
          return vec.end();
        }
        

        or even use the algorithms provided in the STL...

        std::vector<myclass> vec;
        //...
        std::vector<myclass>::iterator get_selected()
        {
          return std::find_if(vec.begin(), vec.end(), std::mem_fun_ref(&myclass::is_selected));
        }
        
        F 1 Reply Last reply
        0
        • S Stuart Dootson

          Design suggestion: you're probably better off following what the STL algorithms do in this case, and return an iterator:

          std::vector<myclass> vec;
          //...
          std::vector<myclass>::iterator get_selected()
          {
            for (std::vector<myclass>::iterator it=vec.begin();it!=vec.end();++it)
            {
              if (it->is_selected())
                return it;
            }
            return vec.end();
          }
          

          or even use the algorithms provided in the STL...

          std::vector<myclass> vec;
          //...
          std::vector<myclass>::iterator get_selected()
          {
            return std::find_if(vec.begin(), vec.end(), std::mem_fun_ref(&myclass::is_selected));
          }
          
          F Offline
          F Offline
          followait
          wrote on last edited by
          #4

          Thanks, this is better.

          1 Reply Last reply
          0
          • F followait

            std::vector<myclass> vec;
            //...
            MyClass & get_selected()
            {
            std::list<myclass>::iterator it=vec.begin();
            for (;it!=vec.end();++it) {
            if (it->is_selected())
            return &*it; // Is it the address of the element in the vector,
            // and is it safe?
            }

            return NULL;
            

            }

            J Offline
            J Offline
            Jurgen Jung
            wrote on last edited by
            #5

            I think you can't save this address, for example as a class member. If you add new elements to the vector it may need additional memory, which means he must allocate new memory for the whole vector and copy all his data. So every address to the old memory will be invalid afterwards.

            1 Reply Last reply
            0
            • F followait

              std::vector<myclass> vec;
              //...
              MyClass & get_selected()
              {
              std::list<myclass>::iterator it=vec.begin();
              for (;it!=vec.end();++it) {
              if (it->is_selected())
              return &*it; // Is it the address of the element in the vector,
              // and is it safe?
              }

              return NULL;
              

              }

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              Even if you change to returning an iterator, be careful about doing any operations that invalidate the iterator.

              --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups