Wednesday, 26 May 2010

std::search example on vector and string

Continuing from last weeks theme, here is a simple example of std::search.




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

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 Search - Matching Case
vector<int> v2;
v2.push_back(22);
v2.push_back(33);
v2.push_back(44);

vector<int>::const_iterator it = search(v1.begin(), v1.end(), v2.begin(), v2.end());
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 Search - Non-Matching Case
vector<int> v3;
v3.push_back(22);
v3.push_back(33);
v3.push_back(444); //non matching

it = search(v1.begin(), v1.end(), v3.begin(), v3.end());
if
(it != v1.end())
{

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

else

{

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


//Use the search to find strings
string s1="Hello, this is Zahid";

//Matching case
string s2="Zahid";

string::const_iterator it2 = search(s1.begin(), s1.end(), s2.begin(), s2.end());
if
(it2 != s1.end())
{

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

else

{

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


//Non-Matching case
string s3="None";

it2 = search(s1.begin(), s1.end(), s3.begin(), s3.end());
if
(it2 != s1.end())
{

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

else

{

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


return
0;
}






The output is as follows:

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:

Wednesday, 12 May 2010

Interesting Challenging problem on Code Complexity

Picked up this interesting problem from the following book:



How many execution paths can this simple three line code take:



String EvaluateSalaryAndReturnName( Employee e )
{

if
( e.Title() == "CEO" || e.Salary() > 100000 )
{

cout << e.First() << " " << e.Last() << " is overpaid" << endl;
}

return
e.First() + " " + e.Last();
}






The answer may surprise you. I have embedded the actual pages from Google books. Luckily the whole problem and solution is given. See Item 18.

Wednesday, 5 May 2010

The size_t confusion

Apparently there could be a lot of confusion with regards to size_t. In 'C' the size_t was defined as an unsigned integer. 'C++' defines it as unsigned integer as well but it has a namespace of std around it. Some compilers may want #include<cstdlib> to use std::size_t.

In any case, it can cause quite some confusion among programmers as to why it is used and what is the size of it. Instead we can #define a type, give it a meaningful name and use it accordingly. Here is my simple example:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>
#include<cstdlib> //Used for std::size_t ... on some compilers

#define u16 unsigned short
#define u32 unsigned //integer is implicit

int
main()
{

size_t size = sizeof(size_t);
std::cout << "Size of size_t is " << size << std::endl;

std::size_t size2 = sizeof(std::size_t);
std::cout << "Size of std::size_t is " << size2 << std::endl;

size_t size3 = sizeof(u16);
std::cout << "Size of u16 is " << size3 << std::endl;

size_t size4 = sizeof(u32);
std::cout << "Size of u32 is " << size4 << std::endl;

return
0;
}





The output is as follows: