Vector question
-
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!
-
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!
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
-
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!
using stl::find() algorithm seems to work fine for me. My articles BlackDice
-
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!
First you need a way of comparing
s_item
s for equality based on thestrDevice
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 thestrDevice
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 -
First you need a way of comparing
s_item
s for equality based on thestrDevice
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 thestrDevice
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