Showing posts with label Find. Show all posts
Showing posts with label Find. Show all posts

Tuesday, 5 April 2011

string::find_next()

I met this new starter who has come from Java background. He was upset because he couldn't find string::find_next() as C++ function. He said Java is much more flexible this way. In reality, its not that bad if you look at the definition of find carefully.

size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;

The first one works as find_next as I show in the example below:

//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy

#include <iostream>
#include <string>

using namespace
std;

int
main()
{

string someString("Tom is not in. Infact Tom is not going to be in Tomorrow or day after Tomorrow");
string findTheString("Tom");

unsigned
position = 0;

position = someString.find(findTheString);
cout<<"First position of " << findTheString << " is " << position << endl;

//We want to find all the next positions which is after position + findTheString.length()
while(position != string::npos)
{

position = someString.find(findTheString, (position + findTheString.length()));
cout<<"Next position of " << findTheString << " is " << position << endl;
}


return
0;
}



The output is as follows:

Exercise for new programmers:
  1. The last two Tom's are part of 'Tomorrow', how can you make sure they are not printed
  2. The last Tom which is 4294... is equal to -1 or string::npos. How can you stop that being printed without making another check for string::npos
Please dont post answers as they should be trivial exercise and you should be able to figure out without much problems.

Wednesday, 19 May 2010

std::find and std::find_if for finding information in vectors

Here is a simple program that demonstrates how to use find and find_if with vectors.




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include <iostream>
#include <vector>
#include <algorithm> //needed for std::find

using namespace
std;

bool
isOdd(int i)
{

return
((i%2)==1);
}


int
main()
{

vector<int> v1;
v1.push_back(88);
v1.push_back(99);
v1.push_back(22);
v1.push_back(33);
v1.push_back(44);
v1.push_back(55);
v1.push_back(66);
v1.push_back(77);

//Demonstrating Find - Find number 55 and its position, if present
vector<int>::const_iterator it = find(v1.begin(), v1.end(), 55);
if
(it != v1.end())
{

cout<<"Found: "<<*it<<endl;
cout<<"Position = "<<it - v1.begin() + 1<<endl;
}

else

{

cout<<"Not Found"<<endl;
cout<<"Position = -1"<<endl;
}


//Demonstrating Find IF - Find the first Odd number and its position
it = find_if(v1.begin(), v1.end(), isOdd);
if
(it != v1.end())
{

cout<<"Found: "<<*it<<endl;
cout<<"Position = "<<it - v1.begin() + 1<<endl;
}

else

{

cout<<"No Odd Numbers Found"<<endl;
cout<<"Position = -1"<<endl;
}


return
0;
}






The output is as follows:

Tuesday, 3 November 2009

Creating specialised strings using 'find_first_not_of'

Sometimes we want to create specialised strings that have a particular characteristic. For example a BitString that can only contain '0' and '1'. Or we may want to create a Hex String that can contain '0-9' and 'a-f'. We can also define other specialised types like numeric strings, octal strings, etc. The main thing would be to check to make sure that the string only contains valid characters. We can do this basic check using find_first_not_of function.

Here is a simple program to show its use:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Program to test specialised strings

#include <iostream>
#include <string>

using namespace
std;

bool
checkIfBitString(string testString)
{

return
(testString.find_first_not_of("01?") == std::string::npos);
}


bool
checkIfHexString(string testString)
{

return
(testString.find_first_not_of("0123456789aAbBcCdDeEfF?") == std::string::npos);
}


int
main()
{

cout<<endl;
string str1("010011000111010101");

if
(checkIfBitString(str1))
cout<<"str1 is a BitString"<<endl;
else

cout<<"str1 is not a BitString"<<endl;

if
(checkIfHexString(str1))
cout<<"str1 can also be a HexString"<<endl;
else

cout<<"str1 is not a HexString"<<endl;

cout<<endl;
string str2("010aBcDeF345678010101");

if
(checkIfBitString(str2))
cout<<"str2 is a BitString"<<endl;
else

cout<<"str2 is not a BitString"<<endl;

if
(checkIfHexString(str2))
cout<<"str2 is a HexString"<<endl;
else

cout<<"str2 is not a HexString"<<endl;

cout<<endl;
string str3("Banana");

if
(checkIfBitString(str3))
cout<<"str3 is a BitString"<<endl;
else

cout<<"str3 is not a BitString"<<endl;

if
(checkIfHexString(str3))
cout<<"str3 is a HexString"<<endl;
else

cout<<"str3 is not a HexString"<<endl;

return
0;
}






The output is as follows: