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. C / C++ / MFC
  4. Vector question

Vector question

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphics
5 Posts 4 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.
  • R Offline
    R Offline
    RobJones
    wrote on last edited by
    #1

    Anyone know of an easy way to search a vector for the following: Lets say I have a vector with 80,000 structures in it and I want to search for duplicate strDevice strings in each struct. Is there a easy or fast way to do this with out pulling the first string and compairing it to the other 79,999 then pulling the second and third etc... const struct s_item { CString strDevice, strIP, strLID; }; std::vector v_items; Any ideas? Whoever said nothing's impossible never tried slamming a revolving door!

    M B S 3 Replies Last reply
    0
    • R RobJones

      Anyone know of an easy way to search a vector for the following: Lets say I have a vector with 80,000 structures in it and I want to search for duplicate strDevice strings in each struct. Is there a easy or fast way to do this with out pulling the first string and compairing it to the other 79,999 then pulling the second and third etc... const struct s_item { CString strDevice, strIP, strLID; }; std::vector v_items; Any ideas? Whoever said nothing's impossible never tried slamming a revolving door!

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      you could add them to a std::map and in each node, keep the index of the std::vector of the string ... for example ( with letters ) vector : a b c d a b a a c d e f a b loop the vector, for each item, insert in the map a (idx 0, 4, 6, 7, 12 ) b (idx 1, 5, 13 ) ... at the end, you have a map of all uniq words, with a vector of the index in the vector of each string ( or structure )


      Maximilien Lincourt Your Head A Splode - Strong Bad

      1 Reply Last reply
      0
      • R RobJones

        Anyone know of an easy way to search a vector for the following: Lets say I have a vector with 80,000 structures in it and I want to search for duplicate strDevice strings in each struct. Is there a easy or fast way to do this with out pulling the first string and compairing it to the other 79,999 then pulling the second and third etc... const struct s_item { CString strDevice, strIP, strLID; }; std::vector v_items; Any ideas? Whoever said nothing's impossible never tried slamming a revolving door!

        B Offline
        B Offline
        BlackDice
        wrote on last edited by
        #3

        using stl::find() algorithm seems to work fine for me. My articles BlackDice

        1 Reply Last reply
        0
        • R RobJones

          Anyone know of an easy way to search a vector for the following: Lets say I have a vector with 80,000 structures in it and I want to search for duplicate strDevice strings in each struct. Is there a easy or fast way to do this with out pulling the first string and compairing it to the other 79,999 then pulling the second and third etc... const struct s_item { CString strDevice, strIP, strLID; }; std::vector v_items; Any ideas? Whoever said nothing's impossible never tried slamming a revolving door!

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          First you need a way of comparing s_items for equality based on the strDevice string:

          struct DeviceIDsEqual : binary_function<s_item, s_item, bool>
          {
               bool operator()(const s_item& f, const s_item& s) const
               {
                   return f.strDevice == s.strDevice;
               }
          }
          

          We also need the vector sorted so we need to compare if one s_item is less the another based on the strDevice string.

          struct DeviceIDsLess : binary_function<s_item, s_item, bool>
          {
               bool operator()(const s_item& f, const s_item& s) const
               {
                   return f.strDevice < s.strDevice;
               }
          }
          

          Now we first sort then eliminate duplicates:

          typedef vector<s_item>::iterator VI;
          sort(v_items.begin(), v_items.end(), DeviceIDsLess());
          VI newEnd = unique(v_items.begin(), v_items.end(), DeviceIDsEqual());
          m_items.erase(newEnd, v_items.end());
          

          Of if you just want to find the duplicates you can use the adjacent_find algorithm. This code needs the following includes:  <functional>  <algorithm> and the following:  using namespace std; Steve -- modified at 23:48 Monday 6th February, 2006

          R 1 Reply Last reply
          0
          • S Stephen Hewitt

            First you need a way of comparing s_items for equality based on the strDevice string:

            struct DeviceIDsEqual : binary_function<s_item, s_item, bool>
            {
                 bool operator()(const s_item& f, const s_item& s) const
                 {
                     return f.strDevice == s.strDevice;
                 }
            }
            

            We also need the vector sorted so we need to compare if one s_item is less the another based on the strDevice string.

            struct DeviceIDsLess : binary_function<s_item, s_item, bool>
            {
                 bool operator()(const s_item& f, const s_item& s) const
                 {
                     return f.strDevice < s.strDevice;
                 }
            }
            

            Now we first sort then eliminate duplicates:

            typedef vector<s_item>::iterator VI;
            sort(v_items.begin(), v_items.end(), DeviceIDsLess());
            VI newEnd = unique(v_items.begin(), v_items.end(), DeviceIDsEqual());
            m_items.erase(newEnd, v_items.end());
            

            Of if you just want to find the duplicates you can use the adjacent_find algorithm. This code needs the following includes:  <functional>  <algorithm> and the following:  using namespace std; Steve -- modified at 23:48 Monday 6th February, 2006

            R Offline
            R Offline
            RobJones
            wrote on last edited by
            #5

            Thank you this is exactly what I was looking for. I need to brush up on templates and STL :) Whoever said nothing's impossible never tried slamming a revolving door!

            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