seminar04: fixes & 5

master
nihonium 2 years ago
parent 52ca816253
commit f8c3ca0535
No known key found for this signature in database
GPG Key ID: 0251623741027CFC

@ -15,14 +15,11 @@ Time::Time(int hours, int minutes, int seconds)
Time::Time(string_view s) Time::Time(string_view s)
{ {
string buf; string buf;
buf[0] = s[0]; buf = s.substr(0, 2);
buf[1] = s[1];
mHours = stoi(buf); mHours = stoi(buf);
buf[0] = s[3]; buf = s.substr(3, 2);
buf[1] = s[4];
mMinutes = stoi(buf); mMinutes = stoi(buf);
buf[0] = s[6]; buf = s.substr(6, 2);
buf[1] = s[7];
mSeconds = stoi(buf); mSeconds = stoi(buf);
} }
@ -62,7 +59,7 @@ vector < Time > getTimesFromString(string_view s)
return res; return res;
} }
Time sumTimes(vector < Time > &v) Time sumTimes(const vector < Time > &v)
{ {
Time res; Time res;
for (int i = 0, size = v.size(); i < size; ++i) { for (int i = 0, size = v.size(); i < size; ++i) {

@ -17,5 +17,5 @@ public:
friend std::ostream& operator<<(std::ostream& out, Time t); friend std::ostream& operator<<(std::ostream& out, Time t);
}; };
Time sumTimes(vector<Time>& v); Time sumTimes(const vector<Time>& v);
vector<Time> getTimesFromString(string_view s); vector<Time> getTimesFromString(string_view s);

@ -0,0 +1,34 @@
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using std::string, std::pair, std::cout, std::endl, std::vector;
template <typename T>
T maximum(const vector<T>& v) {
T max;
for(int i = 0, size = v.size(); i < size; ++i) {
if (v[i] > max)
max = v[i];
}
return max;
}
int main() {
vector<int> int_v {1,3,5,3,23,113,34,54};
cout << maximum(int_v) << endl;
vector<float> float_v {1.5,5.64,5.67,45.65,113,67.5,98.12};
cout << maximum(float_v) << endl;
vector<string> string_v {"aaaa", "dfg", "dsfdgjb", "meow", "dsfewvcv", "klafdn"};
cout << maximum(string_v) << endl;
vector<pair<int, int>> pair_v {
{113, 1},
{12, 3},
{45, 34},
{113, 113},
{112, 12233}
};
pair<int, int> res = maximum(pair_v);
cout << "(" << res.first << ", " << res.second << ")" << endl;
}

@ -1,55 +0,0 @@
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <utility>
using std::cout, std::endl;
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& v)
{
out << "[";
for (size_t i = 0; i < v.size() - 1; ++i)
out << v[i] << ", ";
if (!v.empty())
{
out << v.back();
}
out << "]";
return out;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U>& p)
{
out << "(" << p.first << ", " << p.second << ")";
return out;
}
// Нужно написать функцию pairing
int main()
{
std::vector v {10, 20, 30, 40, 50, 60};
cout << pairing(v) << endl;
std::array<std::string, 7> a {"cat", "dog", "mouse", "elephant", "tiget", "axolotl", "wolf"};
cout << pairing(a) << endl;
std::string s {"Cats and dogs!"};
cout << pairing(s) << endl;
}
/*
Программа должна напечатать:
[(10, 20), (30, 40), (50, 60)]
[(cat, dog), (mouse, elephant), (tiget, axolotl), (wolf, )]
[(C, a), (t, s), ( , a), (n, d), ( , d), (o, g), (s, !)]
*/