Skip to content

Commit

Permalink
New arsenal in cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Feb 21, 2024
1 parent 2f79c41 commit 306bdb9
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
145 changes: 145 additions & 0 deletions EDA/STL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#include <bits/stdc++.h>
using namespace std;

// Pairs
void explainPair(){

pair<int,int> p = {1,3};

cout << p.first << " " << p.second << endl;

pair<int, pair<int, int>> p2 = {1, {3,4}};

cout << p2.first << " " << p2.second.first << " " << p2.second.second << endl;

pair<int, int> arr[] = {{1,2}, {2,5}, {5,1}};

cout << arr[1].first << endl;

}

// Vectors
void explainVector(){

vector<int> v;

v.push_back(1);
v.emplace_back(2);

vector<pair<int,int>> vec;

// Different in sintax, and emplace creates dinamically
vec.push_back({1,2});
vec.emplace_back(1,2);

// Creates a container with 5 spaces, and all've 100 value
vector<int> v2(5,100);

// Creates a container with 5 spaces, all're 0
vector<int> v3(5);

// Copy v2 to v4
vector<int> v4(v2);


// interator is a pointer
vector<int>::iterator it = v2.begin();
cout << *(it) << " ";

for(vector<int>::iterator it = v2.begin(); it != v2.end(); ++it)
cout << *(it) << endl;

for(auto it = v4.begin(); it != v4.end(); ++it)
cout << *(it) << endl;

// For each loop
for(auto x : v4)
cout << x << endl;

// Delete a determinate element
v.erase(v.begin()+1);

// Deleting a range of elements
v.erase(v.begin() + 1, v.begin() + 4);


// Insert Function
vector<int> vp(5,100); // {100, 100, 100, 100, 100}
vp.insert(v.begin(), 300); // {300, 100, 100, 100, 100, 100}
vp.insert(v.begin()+1, 2, 10); // {300, 10, 10, 100, 100...}

// Getting the size of vector
vp.size();

// Deleting the last element
vp.pop_back();

// Change the values of two vectors
vp.swap(v);

}

// Lists
void explainList(){

list<int> ls;

ls.push_back(2);
ls.emplace_back(4);

ls.push_front(5);
ls.emplace_front();

// Same functions as vector
// We can put elements in front of list

}


// Deque
void explainDeque(){

deque<int> dq;
dq.push_back(1);
dq.emplace_back(2);
dq.push_front(4);
dq.emplace_front(3);

dq.pop_back();
dq.pop_front();

cout << dq.back() << endl;
dq.back();
cout << dq.front() << endl;
dq.front();


// Same funcitons as vector
// This strcutre allows us to put elements in front of it
// and remove elements in front, like a double linked list

}

// Priority Queue
void explainPQ(){

// The maximum value is in the top of heap
priority_queue<int> pq;

pq.push(5); // {5}
pq.push(2); // {5,2}
pq.push(3); // {5,3,2}
pq.push(10); // {10,5,3,2}

cout << pq.top() << endl; // prints{10};
pq.pop(); // {5,3,2} -> Remove the element in first index

// Minimum heap
priority_queue<int, vector<int>, greater<int>> ppq;

ppq.push(5); // {5}
ppq.push(2); // {2,5}
ppq.push(3); // {2,3,5}
ppq.push(10); // {2,3,5,10}

}
19 changes: 19 additions & 0 deletions EDA/maxXor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>

int main(){

int x, y;
int max = 0;
std::cin >> x >> y;

for(int i = x; i <= y; ++i) {
for(int j = x; j <= y; ++j) {
int currentMax = (i^j);
if(currentMax > max)
max = currentMax;
}
}

std::cout << max << std::endl;

}
36 changes: 36 additions & 0 deletions EDA/pair_tuples.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>

int main(void) {

// Pairs

std::pair<std::string, int> mypair1 = std::make_pair("Name", 19);
std::cout << mypair1.first << " " << mypair1.second << std::endl;

mypair1.first = "Its possible change the value after declare a pair";

std::pair<std::string,std::string> mypair2 = std::make_pair(
mypair1.first,"Copyyyy"
);

std::cout << mypair2.first << " " << mypair2.second << std::endl;

/* OUTPUT
Name 19
Its possible change the value after declare a pair Copyyyy
*/

// Tuples
int a = 3, b = 4, c = 5;
std::tuple<int, int, int> t{3,4,5};

// Other form to declare
t = std::tie(a,b,c);

// outputing a tuple
std::cout << std::get<0>(t) << std::get<1>(t) << std::get<2>(t) <<
std::endl;



}
33 changes: 33 additions & 0 deletions EDA/playingWithBits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <bitset>

int countSetBits(int n) {

int count = 0;
while(n > 0) {
count += (n&1);
n = n >> 1;
}

return count;
}

int main(void) {

// Basic problems envolving bitwise operations
// 1.. Finding the unique number at a sequence
// 2.. S compliment
//3.. odd or even

// Playing with bits, how many bits have (a to b)
int a,b;
int count = 0;
std::cin >> a >> b;

for(int i = a; i <= b; ++i)
count += countSetBits(i);

std::cout << count << std::endl;


}

0 comments on commit 306bdb9

Please sign in to comment.