A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Linear Search in C++ page! Here, you’ll find the source code for this program as well as a description of how the program works.
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
string error = "Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")";
if (argc != 3)
{
cout << error << endl;
return 1;
}
int numberLength = strlen(argv[1]);
int keyLength = strlen(argv[1]);
if (numberLength == 0 || keyLength == 0)
{
cout << error << endl;
return 1;
}
vector<int> arr;
string temp = "";
for (int i = 0; i < numberLength; i++)
{
if (argv[1][i] == ',')
{
arr.push_back(stoi(temp));
temp = "";
}
else
{
temp = temp + argv[1][i];
}
}
arr.push_back(stoi(temp));
int key = stoi(argv[2]);
bool found = false;
for (int i = 0; i < arr.size(); i++)
{
if (arr[i] == key)
{
found = true;
}
}
if (found)
{
cout << "true";
}
else
{
cout << "false";
}
}
Linear Search in C++ was written by:
If you see anything you’d like to change or update, please consider contributing.
Note: The solution shown above is the current solution in the Sample Programs repository as of Jan 29 2023 21:43:56. The solution was first committed on Oct 03 2022 18:59:58. As a result, documentation below may be outdated.
No ‘How to Implement the Solution’ section available. Please consider contributing.
No ‘How to Run the Solution’ section available. Please consider contributing.