class C
{
public:
C(){}
};
class B
{
C *_c;
public:
B(){}
void setC(const C &c)
{
_c = const_cast<C*>(&c);
}
};
class A
{
B *_b;
C _c2;
C *_c;
public:
A():_b{new B()}, _c{new C()}
{
_b->setC(*_c);
_b->setC(_c2);
}
};
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
template<typename T>
struct Foo
{
T x;
T y;
Foo(T _x = {}, T _y = {}): x{_x}, y{_y}
{}
};
bool myPredicate(Foo<int>& f)
{
if(f.x == 7 || f.y == 7)
{
return true;
}
return false;
}
template<typename Container, class Predicate>
bool remove_value_if(Container& c, Predicate p)
{
if(auto it{find_if(begin(c), end(c), p)}; it != end(c))
{
c.erase(it);
return true;
}
return false;
}
int main()
{
list<Foo<int>> myList {{1,7},{3,4},{7,6},{7,8},{7,10}};
const int value = 7;
while(remove_value_if(myList, myPredicate))
{
cout << "removed value: " << value << endl;
}
for(auto& v:myList)
{
std::cout << v.x << ' ' << v.y << ' ';
}
}
template<typename T>
bool operator==(const Foo<T> &a, const Foo<T> &b)
{
return (a.x == b.x && a.y == b.y);
}
...
list<Foo<int>> myList {{1,7},{3,4},{7,7},{7,8},{7,10}};
auto myFoo = Foo<int>(7,7);
while( remove_value_if(myList, [&](auto& v){ return (myFoo == v);}) )
{
cout << "removed value" << endl;
}
етосразу режет глаза.
начать програмировать самостоятельното же.
char* get_string(char* str, int a)
{
if (str[0] == ' ') //
{
return NULL;
}
int i = 0;
int count = 0;
int bool = 0;
unsigned index = 0;
char* result = malloc(a);
for (int i=0; i <= a; ++i)
{
if (0 != i && str[i] == ' ')
{
break;
}
else if (str[i] == '.')
{
bool = 1;
result[index] = str[i];
index += 1;
}
else if (isdigit(str[i]))
{
if (bool == 1)
{
count += 1;
if (count < 3)
{
result[index] = str[i];
index += 1;
}
}
else
{
result[index] = str[i];
index += 1;
}
}//isdigit(str[i])
else
{
bool = 0;
result[index] = str[i];
index += 1;
}
}//end for
str[a] = '\0';
return result;
}
тогда