Skip to content

Commit 4cf4ce9

Browse files
committed
Categorization | Structural update ..
1 parent 2d38d39 commit 4cf4ce9

40 files changed

+1808
-0
lines changed

deques/deque_ex1.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <deque>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
template<typename T>
7+
void show(const char* msg, deque<T> q)
8+
{
9+
cout << msg << endl;
10+
for(unsigned i=0; i<q.size(); i++)
11+
cout << q[i] << " ";
12+
cout << endl;
13+
}
14+
15+
16+
main()
17+
{
18+
deque<int> dq(10);
19+
for(unsigned i=0; i<dq.size(); ++i) dq[i] = i*i;
20+
show("Contents of dq: ", dq);
21+
22+
int sum = 0;
23+
for(unsigned i=0;i<dq.size();++i) sum += dq[i];
24+
double avg = sum / dq.size();
25+
cout << "The average of dq = " << avg << endl;
26+
27+
for(unsigned i=0;i<dq.size();++i) dq.pop_back();
28+
show("After poping dq contents are: ", dq);
29+
30+
cout << "\n" << *dq.begin() << "\t" << *(dq.end()-1) << endl << endl;
31+
cout << "\n" << *dq.rbegin() << "\t" << *(dq.rend()-1) << endl << endl;
32+
33+
cout << "\n\n";
34+
deque<int> dq2(dq.begin()+2, dq.end()-2); // Keep only element 4
35+
dq2.push_front(3); // add 3 in the beginning
36+
dq2.push_back(5); // add 5 in the end
37+
show("Contents of dq2 are: ", dq2);
38+
}

deques/stack.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include "stack.h"
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
Stack< double > doubleStack;
9+
const size_t doubleStackSize = 5;
10+
double doubleValue = 1.1;
11+
12+
cout << "Pushing elements onto doubleStack\n";
13+
14+
for(size_t i=0; i<doubleStackSize; ++i)
15+
{
16+
doubleStack.push(doubleValue);
17+
cout << doubleValue << " ";
18+
doubleValue += 1.1;
19+
}
20+
21+
cout << "\n\nPoping elements from doubleStack\n";
22+
while( !doubleStack.isEmpty() ) {
23+
cout << doubleStack.top() << " ";
24+
doubleStack.pop();
25+
}
26+
cout << "\nStack is now empty. Can not pop.\n\n";
27+
28+
cout << "\n\nINTEGERS STACK NOW\n\n";
29+
Stack< int > intStack;
30+
const size_t intStackSize = 10;
31+
int intValue = 1;
32+
33+
cout << "Pushing elements onto intStack\n";
34+
for(size_t i =0; i<intStackSize; ++i) {
35+
intStack.push( intValue );
36+
cout << intValue++ << " ";
37+
}
38+
39+
cout << "\n\nPoping elements from intStack\n";
40+
while( !intStack.isEmpty() ) {
41+
cout << intStack.top() << " ";
42+
intStack.pop();
43+
}
44+
cout << "\nIntStack is now empty. Cannot pop anymore...\n";
45+
46+
return 0;
47+
}

deques/stack.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef stack_h
2+
#define stack_h
3+
4+
#include <deque>
5+
6+
template<typename T>
7+
class Stack {
8+
private:
9+
std::deque< T > stack;
10+
public:
11+
// Return the up element of the stack
12+
T& top() {
13+
return stack.front();
14+
}
15+
// Add an element to the stack
16+
void push( const T &pushValue ) {
17+
stack.push_front( pushValue );
18+
}
19+
// Remove an element from the stack
20+
void pop() {
21+
stack.pop_front();
22+
}
23+
// Check if the stack is empty
24+
bool isEmpty() const {
25+
return stack.empty();
26+
}
27+
// Return the size of the stack
28+
size_t size() const {
29+
return stack.size();
30+
}
31+
};
32+
33+
/*
34+
template<c class T >
35+
inline void Stack< T >:: pop()
36+
{
37+
return stack.pop_front();
38+
}
39+
*/
40+
#endif

io/append_to_file.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
#define FAIL 1
8+
#define SUCCESS 0
9+
#define FILENAME "/home/diman91/Documents/Cpp-STL-Examples/Firewall.txt"
10+
11+
bool cat_file(const char* filename)
12+
{
13+
char ch;
14+
15+
ifstream in_file;
16+
in_file.open(filename);
17+
18+
if(!in_file.is_open()) {
19+
cout << "File was not opened! ERROR! Aborting...\n";
20+
return FAIL;
21+
}
22+
23+
do {
24+
in_file.get(ch);
25+
26+
if(!in_file.eof() && ( in_file.fail() || in_file.bad() )) {
27+
cout << "ERROR! Aborting ...";
28+
in_file.close();
29+
return FAIL;
30+
}
31+
32+
if(!in_file.eof())
33+
cout << ch;
34+
} while(!in_file.eof());
35+
36+
in_file.clear(); // needed for eof and fail
37+
38+
in_file.close();
39+
40+
if(!in_file.good()){
41+
cout << "ERROR! ... in closing file ... Aborting ...";
42+
return FAIL;
43+
}
44+
45+
return SUCCESS;
46+
}
47+
48+
49+
// Function to write contents to a file
50+
bool write_file(const char* filename, const char* content)
51+
{
52+
ofstream out_file;
53+
out_file.open(filename, ios::out | ios::app);
54+
55+
if(!out_file.is_open()) {
56+
cout << "File was not opened! ERROR! Aborting...\n";
57+
return FAIL;
58+
}
59+
60+
out_file << content << endl;
61+
62+
out_file.close();
63+
64+
if(!out_file.good()){
65+
cout << "ERROR! Aborting ...";
66+
return FAIL;
67+
}
68+
69+
return SUCCESS;
70+
}
71+
72+
73+
int main()
74+
{
75+
vector<const char*> commands = {"iptables -A INPUT -p tcp -s 192.168.16.0/16 --dport 24 -j ACCEPT",
76+
"iptables -A INPUT -p tcp -s 192.168.17.0/16 --dport 24 -j ACCEPT",
77+
"iptables -A INPUT -p tcp -s 192.168.16.0/16 --dport 21 -j ACCEPT",
78+
"iptables -A INPUT -p tcp -s 192.168.17.0/16 --dport 21 -j ACCEPT",
79+
"iptables -A INPUT -j DROP"
80+
};
81+
vector<const char*>::iterator itr = commands.begin();
82+
while(itr != commands.end()) {
83+
//cout << *itr << endl;
84+
write_file(FILENAME, *itr);
85+
itr++;
86+
}
87+
88+
cat_file(FILENAME);
89+
90+
return 0;
91+
}

lambdas/lambda1.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm> // for_each
4+
5+
using namespace std;
6+
7+
auto test = []( const vector<int>& vec ) {
8+
for(auto val : vec)
9+
cout << val << " ";
10+
cout << endl;
11+
};
12+
13+
auto AVG = [](const vector<int>& vec) {
14+
int sum_=0;
15+
for_each( vec.begin(), vec.end(),
16+
[&sum_](int elem) { sum_+=elem; }
17+
);
18+
cout << "AVG = " << sum_/vec.size() << endl;
19+
};
20+
21+
int main()
22+
{
23+
vector<int> nums{};
24+
for(size_t i=100; i>=50; i-=10) nums.push_back( i );
25+
test(nums);
26+
27+
AVG(nums);
28+
29+
return 0;
30+
}

lists/lst.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <list>
3+
#include <algorithm> // copy
4+
#include <iterator> // ostream_iterator
5+
6+
using namespace std;
7+
8+
template<class T>
9+
class LIST {
10+
template<typename TT>
11+
friend ostream& operator<<( ostream& , LIST<TT>& );
12+
private:
13+
list<T> lst;
14+
T limit;
15+
public:
16+
LIST(T lm) : limit(lm) {}
17+
~LIST() {
18+
cout << "Poping elements...\n";
19+
while( !lst.empty() ) {
20+
cout << lst.back() << " ";
21+
lst.pop_back();
22+
}
23+
cout << "\n";
24+
}
25+
void push() {
26+
cout << "Pushing elements...\n";
27+
for(T i=0; i<limit; i++)
28+
lst.push_back(i);
29+
}
30+
};
31+
32+
template<typename TT>
33+
ostream& operator<<( ostream& out, LIST<TT> &listRef ) {
34+
ostream_iterator<TT> output( out, " " );
35+
copy( listRef.lst.begin(), listRef.lst.end(), output );
36+
out << "\n";
37+
return out;
38+
}
39+
40+
main() {
41+
42+
LIST<int> LI(21);
43+
LI.push();
44+
cout << LI;
45+
46+
return 0;
47+
}

maps/map1.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
class Catalogue {
8+
private:
9+
string name_;
10+
map<string, size_t> addr_;
11+
string tel;
12+
public:
13+
Catalogue(const string& name, const string& addr_a, const size_t addr_nu, const string& tel_num)
14+
: name_(name), tel(tel_num) {
15+
addr_.insert({addr_a, addr_nu});
16+
}
17+
~Catalogue() {}
18+
void show(ostream& );
19+
};
20+
21+
void Catalogue::show(ostream& out) {
22+
map<string, size_t>::iterator itr = (this->addr_).begin();
23+
out << "Name: " << this-> name_ << endl;
24+
out << "Address: " << itr->first << " " << itr->second << endl;
25+
out << "Telephone number: " << this->tel << endl;
26+
}
27+
28+
29+
int main()
30+
{
31+
Catalogue cat1("Zinedin Zidan", "New York Avenue", 888, "0123456789");
32+
cat1.show(cout);
33+
// map test
34+
return 0;
35+
}

maps/map_ex_1.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <map>
4+
5+
using namespace std;
6+
7+
void show(const char* msg, map<string, int> mp)
8+
{
9+
map<string, int>::iterator itr;
10+
11+
cout << msg << "\n";
12+
for(itr=mp.begin();itr!=mp.end();++itr) {
13+
cout << "KEY: " << itr->first << "\t->\tVALUE: " << itr->second << "\n";
14+
}
15+
cout << endl;
16+
}
17+
18+
int main()
19+
{
20+
map<string, int> m;
21+
m.insert(pair<string, int>("Alpha", 100));
22+
m.insert(pair<string, int>("Gamma", 300));
23+
m.insert(pair<string, int>("Delta", 400));
24+
m.insert(pair<string, int>("Beta", 200));
25+
26+
//
27+
map<string, int> m2(m);
28+
29+
map<string, int>::iterator itr;
30+
31+
itr = m.begin();
32+
cout << "FIrst key/value pair in m is: " << itr->first << ", " << itr->second << endl;
33+
34+
itr = m.end();
35+
--itr;
36+
cout << "\nThe last key/value pair in m is: " << itr->first << ", " << itr->second << "\n\n";
37+
38+
show("Contents of m are: ", m);
39+
m.clear();
40+
show("M is now empty: ", m);
41+
42+
m2.erase("Delta");
43+
m2.erase("Alpha");
44+
show("M2 after erasing two key/values is: ", m2);
45+
46+
m.swap(m2);
47+
show("\nM2 after swap is: ", m2);
48+
show("\nM after swap is: ", m);
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)