|
-
Mar 31st, 2003, 07:46 PM
#1
Thread Starter
Frenzied Member
Vector Searching
I have the following structures:
Code:
struct UTM
{
string sLID;
string sCanal;
string sStation;
string sOutlet;
string sService;
string sTypeCode;
string sSize;
};
struct LM
{
string sLID;
string sStreetNumber;
string sStreet;
string sStreetSufix;
};
There are two vectors that uses those:
vector<UTM> vecUTM105AP;
vector<LM> vecLMBAREP;
They both get filled using the same way as my other post . Everything in vecLMBAREP gets sorted by the sLid. Then I uses the following code to see if the vecUTM105AP sLID is in the vecLMBAREP vector.
Code:
struct IsSlidEqual
{
IsSlidEqual( const std::string &s ) : m_s(s) { }
operator () ( const LM& obj )
{
return obj.sLID == m_s;
}
private:
std::string m_s;
};
vector<UTM>::iterator lst;
for (lst = vecUTM105AP.begin(); lst != vecUTM105AP.end(); lst++)
{
UTM* V;
V = (UTM*)(&(*lst));
vector<LM>::iterator it = std::find_if( vecLMBAREP.begin(), vecLMBAREP.end(), IsSlidEqual(V->sLID));
if ( it != vecLMBAREP.end())
{
iBC = atoi(V->sLID.c_str());
}
This works, but the problem I have is it is slow. The vecUTM105AP vector is only ~7700 elements and the other is ~13,000 and it takes 3-4 minutes at least. Granted thats not bad considering but I am going to have another vector that is going to be 34,000 elements and waiting 10-20 minutes is going to suck. Is there a faster/better way to do this?
Last edited by Technocrat; Mar 31st, 2003 at 07:50 PM.
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

-
Apr 15th, 2003, 01:39 AM
#2
Use a map container that matches sLIDs to the whole structs. A vector requires linear search, which is slow, especially when string comparison is done.
Another thing: what is this good for?
Code:
UTM* V;
V = (UTM*)(&(*lst));
You can use the iterator directly:
Code:
IsSlidEqual(lst->sLID)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 15th, 2003, 10:06 AM
#3
Thread Starter
Frenzied Member
I figured it out by sorting, then using a binary search on it using lower_bound.
I didnt know I could do lst-> at the time, I have since figured it out
MSVS 6, .NET & .NET 2003 Pro
I HATE MSDN with .NET & .NET 2003!!!
Check out my sites:
http://www.filthyhands.com
http://www.techno-coding.com

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|