Skip to content

Instantly share code, notes, and snippets.

@MohamedTaha98
Created June 13, 2018 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohamedTaha98/24d40758ca9e8390936b2acf8fda229e to your computer and use it in GitHub Desktop.
Save MohamedTaha98/24d40758ca9e8390936b2acf8fda229e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <valarray>
#include <cmath>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int, int>> vp;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)(v.size()))
void vectorBasicTest();
void vectorManipulation();
void queueStackBasicTest();
void setAndPairAndMapBasicTest();
void stringBasicTest();
void valArrayTest();
void printVecArray(vi v, string s);
void printVecIterator(vi v, string s);
void printPairVec(vp v, string s);
void printValArray(valarray<int> v, string s);
template <class T> string toStr(T value);
int toInt(string s);
bool isOdd(const int n);
bool isEqual(const int n, const int m);
bool pairCmp(const pair<int, int> &a, const pair<int, int> &b);
int main()
{
//vectorBasicTest();
//vectorManipulation();
//queueStackBasicTest();
//setAndPairAndMapBasicTest();
//stringBasicTest();
valArrayTest();
return 0;
}
void vectorBasicTest()
{
vi v(10);
vi v2(10, 7);
const int length = 6;
int arr[length] = {2, 3, 1, 5, 4, 6};
vi v3(arr, arr + length);
printVecArray(v3, "v3");
cout << "v3 Front: " << v3.front() << " v3 Back: " << v3.back() << "\n";
swap(v3.front(), v3.back());
printVecIterator(v3, "v3 With Swapped Front & Back");
vi v4(v3.begin(), v3.end());
v2.resize(15, 5);
printVecArray(v2, "v2 After Resize: ");
sort(v3.begin(), v3.end());
printVecArray(v3, "v3 Sorted");
sort(v3.rbegin(), v3.rend());
printVecArray(v3, "v3 Reversely Sorted");
vector<vector<bool>> vbs(10, vector<bool>(20, true));
if (v > v2) cout << "v1 is bigger than v2\n";
else if (v < v2) cout << "v2 is bigger than v1\n";
else cout << "v1 equals v2\n";
v3.insert(v3.begin() + 1, 3);
printVecIterator(v3, "Inserted 3 in the 2nd Place");
v3.erase(v3.begin() + 2);
printVecArray(v3, "Erased the 3rd Place");
if (find(all(v2), 7) != v2.end()) cout << "7 is in v2\n";
else cout << "7 is not in v2\n";
v3.erase(find(all(v3), 3));
printVecIterator(v3, "Erased 3 from v3 using find");
reverse(all(v3));
printVecArray(v3, "v3 Reversed");
random_shuffle(all(v3));
printVecIterator(v3, "v3 Shuffled");
v3.pop_back();
printVecArray(v3, "v3 Popped Last Element");
v3.clear();
printVecIterator(v3, "v3 Cleared");
}
void vectorManipulation()
{
const int length = 10;
int ar[length] = {2, 3, 2, 5, 4, 6, 2, 7, 16, 9};
vi v1(ar, ar + length);
vi v2(v1.begin() + 3, v1.begin() + 5);
printVecIterator(v1, "v1");
printVecArray(v2, "v2");
cout << "Distance: " << distance(v1.begin(), v1.begin() + 5) << "\n";
cout << "Distance: " << distance(v1.begin() + 6, v1.begin() + 1) << "\n";
vi v3 = v1;
printVecIterator(v3, "Copy of v1 into v3");
replace(all(v3), 2, 7);
printVecArray(v3, "After Replacement");
replace_if(all(v3), isOdd, 15);
printVecIterator(v3, "More Replacement");
replace(v3.begin(), v3.begin() + 3, 15, 3);
printVecArray(v3, "More Replacement");
vi::iterator itr = search_n(all(v3), 2, 15);
if (itr != v3.end()) cout << "Found 3 15s from index " << distance(v3.begin(), itr);
replace(all(v2), 5, 15);
printVecArray(v2, "\nv2 After Replacement");
itr = search(all(v3), all(v2));
if (itr != v3.end()) cout << "Found v2 at v3 at index: " << itr - v3.begin() << "\n";
sort(v3.begin(), v3.end());
printVecIterator(v3, "v3 Sorted");
cout << "Searching for 15\n";
if (binary_search(all(v3), 15)) cout << "Found 15\n";
itr = lower_bound(all(v3), 9);
if (itr != v3.end())
cout << "The first number >= 9 is " << (*itr) << " Found at index " << distance(v3.begin(), itr) << "\n";
itr = upper_bound(all(v3), 9);
if (itr != v3.end())
cout << "The first number > 9 is " << (*itr) << " Found at index " << itr - v3.begin() << "\n";
v3.resize(unique(all(v3)) - v3.begin());
printVecArray(v3, "Removing Duplicates");
sort(all(v2));
vi v;
printVecArray(v2, "v2"); printVecIterator(v3, "v3");
set_intersection(all(v3), all(v2), back_inserter(v));
printVecIterator(v, "Intersection Between v2, v3");
v.clear();
set_difference(all(v3), all(v2), back_inserter(v));
printVecArray(v, "Difference Between v3, v2");
v.clear();
set_difference(all(v2), all(v3), back_inserter(v));
printVecIterator(v, "Difference Between v2, v3");
v.clear();
set_union(all(v2), all(v3), back_inserter(v));
printVecArray(v, "Union Between v2, v3");
v.clear();
int mx = *max_element(all(v3));
int mn = *min_element(v3.begin(), v3.end());
cout << "Max of v3 = " << mx << ", Min of v3 = " << mn << endl;
v2.push_back(3);
v2.push_back(5);
printVecIterator(v2, "v2"); printVecIterator(v3, "v3");
swap_ranges(v2.begin(), v2.begin() + 3, v3.end() - 3);
cout << "Swapping Ranges between v2, v3";
printVecArray(v2, "v2"); printVecArray(v3, "v3");
cout << "Sum of v2: " << accumulate(all(v2), 0) << ", Product of v3: " << accumulate(all(v2), 1, multiplies<int>()) << "\n";
cout << "Sum of Products of v2: " << inner_product(all(v2), v2.begin(), 0);
cout << ", Products of Sum of v3: " << inner_product(all(v3), v3.begin(), 1, multiplies<int>(), plus<int>()) << endl;
v.clear();
partial_sum(all(v2), back_inserter(v));
printVecIterator(v, "v2 Accumulative Array");
v.clear();
partial_sum(all(v2), back_inserter(v), minus<int>());
printVecArray(v, "v2 Subtracting Array");
v.clear();
ostringstream oss;
partial_sum(all(v2), ostream_iterator<int>(oss, " "));
cout << "Accumulative Array Using Ostream: " << oss.str() << endl;
adjacent_difference(all(v2), back_inserter(v));
printVecIterator(v, "v2 Adjacent Differences");
v.clear();
adjacent_difference(all(v2), back_inserter(v), plus<int>());
printVecArray(v, "v2 Adjacent Sums");
v.clear();
itr = adjacent_find(all(v2), isEqual);
if (itr != v2.end()) cout << "The 2 adjacent values are " << *itr << ", " << *(itr + 1) << endl;
else cout << "No adjacent values found\n";
v3.insert(v3.begin(), 6);
v3.insert(v3.begin() + 3, 6);
v3.push_back(6);
printVecArray(v3, "v3");
cout << "Count of 6 in v3 = " << count(all(v3), 6) << "\n";
cout << "Count of Odds in v3 = " << count_if(all(v3), isOdd) << "\n";
v3.erase(remove(all(v3), 6), v3.end());
printVecIterator(v3, "After Deleting all 6s");
rotate(v3.begin() + 1, v3.begin() + 2, v3.end());
printVecArray(v3, "After Rotation");
sort(all(v3));
do
{
printVecIterator(v3, "Perm");
}
while(next_permutation(all(v3)));
}
void queueStackBasicTest()
{
queue<int> q;
q.push(20);
q.push(10);
q.push(30);
cout << "Last element in the queue: " << q.back() << "\n";
cout << "Queue Elements: ";
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
deque<int> dq;
dq.push_front(20);
dq.push_back(10);
priority_queue<int> pq;
pq.push(20);
pq.push(10);
pq.push(30);
cout << "\nPriority Queue Elements: ";
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
stack<int> s;
s.push(20);
s.push(30);
s.push(10);
cout << "\nStack Elements: ";
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
cout << "\n";
}
void setAndPairAndMapBasicTest()
{
pair<int, string> p1 = make_pair(7, "CR");
pair<string, pair<int, string>> p2 = make_pair("Ronaldo", p1);
cout << p2.first << endl;
cout << p2.second.second << p2.second.first << endl;
vector<pair<int, int>> vpa;
vpa.push_back(make_pair(1, 2));
vpa.push_back(make_pair(4, 4));
sort(all(vpa), pairCmp);
printPairVec(vpa, "vp");
set<string> strSet;
strSet.insert("Mohamed");
strSet.insert("Mostafa");
if (strSet.count("Taha")) cout << "Taha is in strSet\n";
else cout << "Taha is not in strSet\n";
if (strSet.count("Mohamed")) cout << "Mohamed is in strSet\n";
map<int, string> ma;
ma[10] = "Mostafa";
ma[20] = "Mohamed";
ma[30] = "Ahmed";
vector<pair<int, string>> mptov(all(ma));
cout << "map ma:\n";
for (int i = 0; i < (int) mptov.size(); i++)
cout << mptov[i].first << " " << mptov[i].second << endl;
}
void stringBasicTest()
{
string str2 = ("Hello");
string test = ("hi abc abc abc abc");
cout << test.substr(3) << endl;
cout << test.substr(3, 5) << endl;
cout << "Mostafa at: " << test.find("Mostafa") << "\n";
cout << "Cast to int: " << (int) test.find("Mostafa") << "\n";
cout << "abc at: " << (int) test.find("abc", 5) << "\n";
cout << test.find_last_of("cab") << "\n";
cout << test.find_first_of("aic") << endl;
cout << test.find_first_of("aic", 4) << endl;
cout << test.find_first_of("aic", 4, 6) << endl;
cout << test.find_first_not_of("aic") << "\n";
cout << test.replace(4, 3, "XXX") << "\n";
char ar[5] = {'a', 'b', 'c', 'd', '\0'};
string s = ar;
cout << s << " Array to string\n";
const char* t = s.c_str();
cout << t << " Array to const char*\n";
cout << test.erase(8) << " Erased from 8 to end\n";
cout << test.erase(4, 2) << " Erased 2 chars from 4\n";
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
ostringstream oss;
copy(all(v), ostream_iterator<int>(oss, ", "));
cout << oss.str() << "\n";
istringstream iss("10 20 30");
int x;
while(iss >> x)
cout << "x Read: " << x << endl;
cout << toStr(32) << endl;
cout << toInt("45") + toInt("5") << endl;
}
void valArrayTest()
{
int val[] = {1, -2, 3, -4, 5};
valarray<int> v1(val, 5);
printValArray(v1, "v1");
valarray<int> v2 = abs(v1);
printValArray(v2, "v2");
cout << "Sum of V2: " << v2.sum() << endl;
valarray<bool> v3(v1 == v2);
cout << v3.min() << " " << v3.max() << "\n";
valarray<int> v4 = 2 * (v1 + v2);
printValArray(v4, "v4");
double arr[] = {0, 1, 2, 3};
valarray<double> v5(arr, 4);
valarray<double> v6 = 2.0 * (3.0 * v5 + v5);
cout << pow(2.0, v5).sum() << endl;
cout << pow(v5, 2.0).sum() << "\n";
cout << pow(2.5 * v5, 2.0).sum() << "\n";
}
template <class T> string toStr(T value)
{
ostringstream oss;
oss << value;
return oss.str();
}
int toInt(string s)
{
int n;
istringstream iss(s);
iss >> n;
return n;
}
bool pairCmp(const pair<int, int> &a, const pair<int, int> &b)
{
return a.first * b.second < a.second * b.first;
}
void printVecArray(vi v, string s)
{
cout << s << ": ";
for (int i = 0; i < (int) v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
void printVecIterator(vi v, string s)
{
cout << s << ": ";
vi::iterator it = v.begin();
while (it != v.end())
{
cout << (*it) << " ";
it++;
}
cout << "\n";
}
void printPairVec(vp v, string s)
{
cout << s << ":\n";
vp::iterator it = v.begin();
while (it != v.end())
{
cout << it->first << " " << it->second << endl;
it++;
}
}
void printValArray(valarray<int> v, string s)
{
cout << s << ": ";
for (int i = 0; i < (int)v.size(); i++)
cout << v[i] << " ";
cout << "\n";
}
bool isOdd(const int n)
{
return n % 2 != 0;
}
bool isEqual(const int n, const int m)
{
return n == m;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment