Showing posts with label Search. Show all posts
Showing posts with label Search. Show all posts

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:

Searching for BASIC

When people search for BASIC online using Google or Bing, or whatever, what keywords do they search with? Some of the obvious choices to me are:

  • basic
  • basic for windows
  • qbasic
  • visual basic
  • vb

How would you search for BASIC?

Write a program to search an element in an array (Binary searching)

Size of array is taken from user.. Elements are entered by user in order (ascending or descending).. user is asked for the element to be searched... Search is performed..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<conio.h>

  2. #include<iostream.h>

  3. void main()

  4. {

  5. clrscr();

  6. int a[100],i,loc,mid,beg,end,n,flag=0,item;

  7. cout<<"How many element: ";

  8. cin>>n;

  9. cout<<"Enter (IN ORDER) the elements of array: "<<endl;

  10. //Coding by: Snehil Khanor

  11. //http://WapCPP.blogspot.com

  12. for(i=0;i<n;i++)

  13. cin>>a[i];

  14. cout<<"Enter the element to be searched: ";

  15. cin>>item;

  16. loc=0;

  17. beg=0;

  18. end=n-1;

  19. while((beg<=end)&&(item!=a[mid]))

  20. {

  21. mid=(beg+end)/2;

  22. if(item==a[mid])

  23. {

  24. cout<<"Search successful :)";

  25. loc=mid;

  26. cout<<"Position of the Item: "<<loc+1;

  27. flag=flag+1;

  28. }

  29. else if(item<a[mid])

  30. end=mid-1;

  31. else

  32. beg=mid+1;

  33. }

  34. if(flag==0)

  35. {

  36. cout<<"Search NOT sucessfull :( ";

  37. }

  38. getch();

  39. }

Write a program to search an element in an array (Linear searching)

Size of array is taken from user.. Elements are entered.. Then user is asked to enter element to be searched.. Search is performed..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>

  2. #include<conio.h>

  3. void main()

  4. {

  5. clrscr();

  6. int ar[10],n,num,no;

  7. cout<<"Enter size of the array: ";

  8. cin>>n;

  9. cout<<"Enter array element: "<<endl;

  10. for(int i=0;i<n;i++)

  11. {

  12. cout<<"Enter element "<<i<<": ";

  13. cin>>ar[i];

  14. }

  15. //Coding by: Snehil Khanor

  16. //http://WapCPP.blogspot.com

  17. cout<<"Elements of array: "<<endl;

  18. for(i=0;i<n;i++)

  19. cout<<"Element at index number "<<i<<" is: "<<ar[i]<<endl;

  20. //////To search////

  21. cout<<"Enter number to search: ";

  22. cin>>num;

  23. for(i=0;i<n;i++)

  24. {

  25. if(num==ar[i])

  26. {

  27. cout<<"Found at index number: "<<i;

  28. no=0;

  29. break;

  30. }

  31. else

  32. {

  33. no=1;

  34. continue;

  35. }

  36. }

  37. if(no==1)

  38. cout<<"Sorry your search returned NO results. :(";

  39. getch();

  40. }

Check out this stream