init commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
|
||||
class string
|
||||
{
|
||||
int len; // количество памяти
|
||||
char* str;
|
||||
|
||||
public:
|
||||
string(const char* s = "") {
|
||||
len = strlen(s) + 1;
|
||||
str = new char[len];
|
||||
strcpy(str, s);
|
||||
}
|
||||
|
||||
char&
|
||||
operator[](int n) {
|
||||
return str[n];
|
||||
}
|
||||
|
||||
string
|
||||
operator+(string s) {
|
||||
string tmp;
|
||||
tmp.len = s.len + len - 1;
|
||||
tmp.str = new char[tmp.len];
|
||||
|
||||
strcpy(tmp.str, str);
|
||||
strcpy(tmp.str + len - 1, s.str);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
// string
|
||||
// operator=(string s) {
|
||||
// if (s.str == str) {
|
||||
// return s;
|
||||
// }
|
||||
|
||||
// delete[] str;
|
||||
// len = s.len;
|
||||
// str = new char[len];
|
||||
// strcpy(str, s.str);
|
||||
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
// ~string() { delete[] str; } почему-то с ним не работает (?)
|
||||
|
||||
void
|
||||
print() {
|
||||
std::cout << str << " " << len << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
main() {
|
||||
string s1("abcd"), s2("qw"), s3;
|
||||
s2[1] = 'A';
|
||||
s2.print();
|
||||
s3 = s1 + s2;
|
||||
|
||||
s3.print();
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// -> написать шаблонную функцию, которая ищет макс значение в константном или неконстантном контейнере
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <class Container>
|
||||
typename Container::value_type Max(const Container& c) {
|
||||
typename Container::value_type max = *c.cbegin();
|
||||
|
||||
for (auto it = c.cbegin(); it != c.cend(); ++it) {
|
||||
if (*it > max) {
|
||||
max = *it;
|
||||
}
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void dbl(T& c) {
|
||||
auto it = c.begin();
|
||||
|
||||
while (it != c.end()) {
|
||||
it = c.insert(++it, *it);
|
||||
++it;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void rever(T b, T e) {
|
||||
if (b == e) return;
|
||||
|
||||
--e;
|
||||
|
||||
while (b != e) {
|
||||
swap(*b++, *e);
|
||||
|
||||
if (b == e) break;
|
||||
|
||||
--e;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
struct Pred {
|
||||
bool operator()(int x) {
|
||||
return x % 3;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, typename P> // T - указатель P - предикат
|
||||
void f(T b, T e, P pred, typename T::value_type val = {}) {
|
||||
while (b != e) {
|
||||
if (pred(*b)) {
|
||||
*b = val;
|
||||
}
|
||||
++b;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
list<int> l = { 1, 2, 3, 4, 5 };
|
||||
cout << Max<list<int>>(l) << endl;
|
||||
|
||||
f(l.begin(), l.end(), Pred());
|
||||
for (const auto& elem : l) {
|
||||
cout << elem << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
// dbl(l);
|
||||
// rever(l.begin(), l.end());
|
||||
// for (const auto& elem : l) {
|
||||
// cout << elem << " ";
|
||||
// }
|
||||
// cout << endl;
|
||||
|
||||
vector<long> v = { 1, 3, 3, 4, 5 };
|
||||
cout << Max<vector<long>>(v) << endl;
|
||||
|
||||
// dbl(v);
|
||||
rever(v.begin(), v.end());
|
||||
for (const auto& elem : v) {
|
||||
cout << elem << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user