Stuff from demos

This commit is contained in:
Imbus 2025-01-11 17:45:38 +01:00
parent 1515fcd2cf
commit e1d1138653
5 changed files with 193 additions and 0 deletions

17
demos/comma.cpp Normal file
View file

@ -0,0 +1,17 @@
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
std::string s;
while (cin >> s, s.length() > 5) {
cout << s << " is longer than 5\n";
}
cout << s << " is too short\n";
for (int i = 0, j = 0; i != 5; ++i, ++j) {
cout << i << ", " << j << endl;
}
}

24
demos/file_io_example.cpp Normal file
View file

@ -0,0 +1,24 @@
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
void print_bytes(std::string infile) {
std::ifstream in{infile};
int c;
while ((c = in.get()) != EOF) {
std::cout << "read: " << std::setw(3) << c << " [" << char(c) << "]"
<< std::endl;
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " filename" << std::endl;
return 1;
}
print_bytes(argv[1]);
return 0;
}

123
demos/map_and_set.cpp Normal file
View file

@ -0,0 +1,123 @@
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <map>
#include <set>
void test_map() {
std::map<std::string, int> msi;
msi.insert(std::make_pair("Kalle", 1));
msi.emplace("Lisa", 2);
msi["Kim"] = 3;
for (const auto &a : msi) {
cout << a.first << " : " << a.second << endl;
}
cout << "Kim --> " << msi.at("Kim") << endl;
cout << "Lisa --> " << msi["Lisa"] << endl;
cout << "Hans --> " << msi["Hans"]
<< endl; // [] default-konstruerar ett värde
// för nycklar som inte finns
try {
cout << "Nisse --> " << msi.at("Nisse") << endl;
} catch (std::out_of_range &e) {
cout << "Nisse not found: " << e.what() << endl;
}
const auto &kim = msi.find("Kim");
if (kim != msi.end()) {
cout << "Kim : " << kim->second << endl;
} else {
cout << "Kim not found\n";
}
auto nisse = msi.find("Nisse");
if (nisse != msi.end()) {
cout << "Nisse : " << nisse->second << endl;
} else {
cout << "Nisse not found\n";
}
}
void test_set() {
std::set<int> ints{1, 3, 7};
ints.insert(5);
for (auto x : ints) {
cout << x << " ";
}
cout << endl;
auto has_one = ints.find(1);
if (has_one != ints.end()) {
cout << "one is in the set\n";
} else {
cout << "one is not in the set\n";
}
if (ints.count(1)) {
cout << "one is in the set\n";
} else {
cout << "one is not in the set\n";
}
auto has_two = ints.find(2);
if (has_two != ints.end()) {
cout << "two is in the set\n";
} else {
cout << "two is not in the set\n";
}
if (ints.count(2)) {
cout << "two is in the set\n";
} else {
cout << "two is not in the set\n";
}
auto has_five = ints.find(5);
if (has_five != ints.end()) {
cout << "five is in the set\n";
} else {
cout << "five is not in the set\n";
}
ints.erase(5);
auto has_five_now = ints.find(5);
if (has_five_now != ints.end()) {
cout << "five is in the set\n";
} else {
cout << "five is not in the set\n";
}
}
void test_set_bounds() {
std::set<int> ints{9, 1, 3, 7, 5};
auto lb3 = ints.lower_bound(3);
cout << "lb3: " << *lb3 << endl;
auto ub3 = ints.upper_bound(3);
cout << "ub3: " << *ub3 << endl;
auto lb4 = ints.lower_bound(4);
cout << "lb4: " << *lb4 << endl;
auto ub4 = ints.upper_bound(4);
cout << "ub4: " << *ub4 << endl;
auto er = ints.equal_range(7);
if (er.first == er.second) {
cout << "er.first == er.second";
}
cout << ", er.first: " << *er.first << ", er.second: " << *er.second
<< endl;
}
int main() {
test_map();
test_set();
test_set_bounds();
}

14
demos/print_seq.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef PRINT_SEQ_H
#define PRINT_SEQ_H
#include <iostream>
template <typename C> void print_seq(const C &c) {
using std::cout;
using std::endl;
cout << "[length: " << c.size() << "] ";
for (const auto &x : c) std::cout << x << " ";
cout << endl;
}
#endif

View file

@ -0,0 +1,15 @@
#include <iostream>
int main() {
int a{1234};
float f{1234.0};
std::cout << "sizeof(int): " << sizeof(int)
<< ", sizeof(float): " << sizeof(float) << std::endl;
std::cout << "static_cast: " << static_cast<float>(a) << " "
<< static_cast<int>(f) << std::endl;
std::cout << "reinterpret_cast: " << *reinterpret_cast<float *>(&a) << " "
<< *reinterpret_cast<int *>(&f) << std::endl;
}