Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A More Efficient Search

search c++ string

  • Please log in to reply
19 replies to this topic

#1 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 09 July 2012 - 12:17 AM

Hello, I'm trying to find a substring in a string. For example I search for "50:67:f0:e7:d9:1c" in the "27.5.2012/11:6:40 bahadirtr 50:67:f0:e7:d9:1c N/A 0.0 0.0" and then I get the word after "50:67:f0:e7:d9:1c".
I try to search like this :

string array[20000]; // creates array to hold names
char strength[7];

	   for(int i = 0;i<85; i++)
		   {
			if((array[2][i]=='5') && (array[2][i+1] == '0') && (array[2][i+2] ==':') && (array[2][i+3] =='6') && (array[2][i+4] == '7') &&
			(array[2][i+5] ==':') && (array[2][i+6] =='f') && (array[2][i+7] =='0') && (array[2][i+8] ==':') && (array[2][i+9] =='e') && (array[2][i+10] =='7') && (array[2][i+11] ==':') &&
			(array[2][i+12] =='d') && (array[2][i+13] =='9') && (array[2][i+14] ==':') && (array[2][i+15] =='1') && (array[2][i+16] =='c' ))
			{
					for(int j = 0; j<6;j++)
				 strength[j]=array[2][i+j+17];
			}
		   }
		   cout<<strength<<endl;
	}

This searching method seems to work. But I have a lot of substrings to search. Is there a more efficient way to do this?
Thanks in advance!
  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 09 July 2012 - 02:38 AM


#include <string>

std::string array[20000];
std::string strength;

size_t index = array[2].find("50:67:f0:e7:d9:1c");

// Check to see if we found the desired MAC address
if( index != std::string::npos )
    // We did, copy over the substring. Add 18 to skip over the MAC address and the next space,
    // and copy 7 characters after that.
    strength = array[2].substr(index + 18, 7);

Only problem is if there's more than one space separating the MAC address and the next field, then the 18 won't work.

Edited by dargueta, 09 July 2012 - 02:41 AM.
Formatting & caveat

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 09 July 2012 - 06:28 AM

Thank you, there is always one space between them
  • 0

#4 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 09 July 2012 - 10:39 PM

if( index != std::string::npos )

what does this line do?
  • 0

#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 09 July 2012 - 10:43 PM

If you look at the documentation here, the find() function returns string::npos if it didn't find the substring. Here we're making sure that we actually found the substring before doing anything else.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#6 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 09 July 2012 - 10:46 PM

I mean I have a lot of mac addresses to search, How can I say, if you can't find "50:67:f0:e7:d9:1c", try searching for "00:24:6c:86:0d:71"
  • 0

#7 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 09 July 2012 - 11:01 PM

Something like this, perhaps? It doesn't check if you've found the same MAC address twice, and iterates through all 20000 entries regardless of whether you've already found all your desired MAC addresses. For that you'd have to use a set.
std::string array[20000];

// MAC addresses to search for
const char *search_macs[X] = {
    "50:67:f0:e7:d9:1c",
    "00:24:6c:86:0d:71"
};

for( int i = 0; i < 20000; ++i )
{
    std::string strength = "";

    for( int j = 0; j < X; ++j )
    {
        size_t index = array[i].find(search_macs[j]);

        // Search for another MAC address if we didn't find the current one
        if( index == std::string::npos )
            continue;

        strength = array[i].substr(index + 18, 7);
        break;
    }

    // If we get out here and strength is still an empty string, then none of
    // the MACs we were looking for were in array[i].
}

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#8 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 09 July 2012 - 11:06 PM

Thank you but I think of such a thing. Might this work?

size_t index1 = array[i].find("00:24:6c:86:17:c2");
size_t index2 = array[i].find("50:67:f0:e7:d9:1c");


if( index1 != std::string::npos )
{
bir[a]=array[i].substr(index1 + 18, 6);
a++;
continue;

}
else if( index2 != std::string::npos )
{
iki[b]=array[i].substr(index2 + 18, 6);
b++;
continue;

}

  • 0

#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 09 July 2012 - 11:19 PM

Don't do any more computation than you have to; only search for the second MAC address if you haven't found the first:
size_t index;

index = array[i].find("00:24:6c:86:17:c2");

if( index != std::string::npos )
{
    bir[a] = array[i].substr(index + 18, 6);
    ++a;
}
else
{
    index = array[i].find("50:67:f0:e7:d9:1c");
    if( index != std::string::npos )
    {
        iki[b] = array[i].substr(index + 18, 6);
        ++b;
    }
    // If you want to handle a case where neither is found, put that code in
    // an else block here.
}

Trust me, in a loop with worst-case 40000 searches to do, you want to do as few as necessary.
  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 09 July 2012 - 11:24 PM

thank you so much

I have to search for 20+ mac addresses, in your case, will I have to make 20+ nested if statements?
  • 0

#11 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 09 July 2012 - 11:41 PM

Nonononono. Never hard-code things like that, it'll get incredibly messy and if you ever need to add or remove a MAC address it'll get ugly. Use the loop like I suggested above. If you ever have to do something repeatedly on different inputs, put it in a loop; if the loop gets too big, break it up into a few functions and call them in a smaller loop.
  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'


#12 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 09 July 2012 - 11:54 PM

Thank you so much for the help. I appreciate..

One more thing, I need to put the values for each mac addresses to a different string array. I couldn't figure out how I can do that in that loop code.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download