Skip to content

Instantly share code, notes, and snippets.

@abdoulayegk
Created January 11, 2022 05:34
Show Gist options
  • Save abdoulayegk/737b1302f61c6c26ef0d230364958006 to your computer and use it in GitHub Desktop.
Save abdoulayegk/737b1302f61c6c26ef0d230364958006 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
bool f(int x, int y) { return x > y; }
// Note that all the operation is perform in O(logn)
void vector_demo() {
std::vector<int> A = {11, 2, 5, 15, 3};
std::cout << A[1] << std::endl;
// sorting the vector it takes O(nlogn);
sort(A.begin(), A.end()); // nlogn
// use binary search to find wether an element is in it or not
// O(nlogn)
bool present = std::binary_search(A.begin(), A.end(), 3);
for (int i = 0; i < A.size(); ++i) {
std::cout << A[i] << " ";
}
std::cout << "\n" << std::endl;
std::cout << present << std::endl; // return True
A.push_back(50);
A.push_back(100);
A.push_back(100);
A.push_back(100);
A.push_back(120);
A.push_back(125);
std::cout << A[A.size() - 1] << std::endl;
std::cout << A.capacity() << std::endl;
std::cout << A.max_size() << std::endl;
std::cout << A.empty() << std::endl;
// using upper bound and lower bound
std::vector<int>::iterator it = std::lower_bound(A.begin(), A.end(), 100);
std::vector<int>::iterator it1 = std::upper_bound(A.begin(), A.end(), 100);
std::cout << *it << std::endl;
std::cout << *it1 << std::endl;
// find the number of time 100 occurs in this std::vector<> ;
int occurence = it1 - it;
std::cout << "The number of times 100 occurs is: " << occurence << std::endl;
// sorting the vector in asscending order
sort(A.begin(), A.end(), f);
// To print the sorted vector
std::cout << "The elements of the vector in asscending order is: "
<< std::endl;
std::vector<int>::iterator it3;
for (it3 = A.begin(); it3 != A.end(); it3++) {
std::cout << *it3 << std::endl;
}
/* We can also perform the same traversal operation with using the following:
we can change iterator to auto like this:
- auto it = lower_bound(A.begin(), A.end(),100)
- auto it1 = upper_bound(A.begin(), A.end(),100)
Now the for loop will be:
for(int &x:A) // reference
{
x++
}
for(int x :A){
std::cout << x << std::endl;
}
*/
}
// set demo
void setDemo() {
std::set<int> s;
s.insert(1);
s.insert(-7);
s.insert(5);
s.insert(-2);
s.insert(8);
s.insert(9);
for (int x : s) {
std::cout << x << std::endl;
}
// to find if -2 is in the set
auto it = s.find(-2);
if (it == s.end()) {
std::cout << "Not present" << std::endl;
} else {
std::cout << "Present" << std::endl;
std::cout << *it << std::endl;
}
// now let's find all the element strictly greater than -2
auto it2 = s.upper_bound(-2);
auto it3 = s.lower_bound(5);
std::cout << *it2 << " " << *it3 << std::endl;
// Note: if we try to use upper_bound on something that doesn't exit then it
// will go to end()
}
// map demo
void mapDemo() {
std::map<int, int> A;
A[1] = 100;
A[2] = -1;
A[1] = 10;
std::cout << A[2] << std::endl;
// we can do it for char also
std::map<char, int> cnt;
std::string x = "Abdoulaye Balde";
for (char c : x) {
std::cout << cnt['a'] << " " << cnt['z'] << std::endl;
}
}
void find_in_dequeue() {
deque<int> coll{2, 4, 6, 8, 19, -5};
for (int i = 0; i < 10; i++) {
auto result = std::find(coll.cbegin(), coll.cend(), i);
if (result != coll.cend()) {
cout << "Was found " << *result << "\n";
} else {
cout << "Was not found"
<< "\n";
}
}
}
int main(int argc, char *argv[]) {
find_in_dequeue();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment