среда, 5 августа 2015 г.

c++

Обзор с++11 от Страуструпа: ссыль

generate(vec.begin(),vec.end(),[](){srand(clock());return rand()%10;}); remove_copy_if(vec.begin(),vec.end(),back_inserter(res),[](int i){return i>2;}); copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));
copy(res.begin(),res.end(),ostream_iterator<int>(cout," "));

среда, 13 мая 2015 г.

Вложенность произвольных контейнеров

#include <iostream>
#include <vector>
#include <list>
#include <deque>
using namespace std;

template <class T>
struct inCount{
    enum {result = 0};  
};

template<class T,template <class TT,class alloc = allocator<TT> > class CON>
struct inCount<CON<T> > {
    enum {result = inCount<T>::result +1};
};

template <class T>
int foo(T const & t) {
    return inCount<T>::result;
}

int main() {
    // your code goes here
    vector< vector< list<vector <list<deque<int> > > > > > v;
  
    cout << foo(v);
    return 0;
}

Узнать вложенность вектора

#include <iostream>
#include <vector>
using namespace std;

template <class T>
struct inCount{
    enum {result = 0};   
};

template<class T>
struct inCount<vector<T> > {
    enum {result = inCount<T>::result +1};
};

template <class T>
int foo(T const & t) {
    return inCount<T>::result;
}

int main() {
    // your code goes here
    vector< vector< vector<int> > > v;
   
    cout << foo(v);
    return 0;
}

вторник, 24 марта 2015 г.

Проверить, является ли тип абстрактным

#include <iostream>
using namespace std;

template <class T>
class is_abstr {

typedef char yes[1];
typedef char no[2];

template <class C> static no& test(C (*)[1]);
template <class> static yes& test(...);
   
public:   
    static const bool value = sizeof(test<T>(0 )) == sizeof(yes);

};

class A {
public:
    virtual int foo() = 0;
   
};

class B : public A {
public:
    int foo() {cout << "foo";}

};


int main() {
    cout.setf(ios::boolalpha);
    cout << is_abstr<B>::value;
    return 0;
}

суббота, 6 декабря 2014 г.

Подключение геймпада Dualshock 3

1. Должны быть загружены модули joydev и uinput
2. Нажимаем PS на джойстике и подключаем
3. выполняем sudo xboxdrv -d --mimic-xpad --silent --evdev /dev/input/event12 --config ~/xboxdrv/ds3.xboxdrv

в ds3.xboxdrv должно быть примерно следующее

# Playstation 3 Wireless Controller (sixad)
# ========================
[xboxdrv]
# Using the 'by-id' name is recomment, as it is static, while an
# /dev/input/eventX name can change depending on what other USB
# devices you use.
#evdev = /dev/input/event10
# This displays events received from the controller, if you are
# working on a configuration you want to set this to true:
#evdev-debug = true
# Grabbing the device prevents other applications from accessing it,
# this is needed most of the time te prevent applications from
# receiving events twice.
evdev-grab = true
[evdev-absmap]
ABS_X = X1
ABS_Y = Y1
ABS_Z = X2
ABS_RX = Y2
ABS_#12 = LT
ABS_#13 = RT
[evdev-keymap]
BTN_TL = LT
BTN_TR = RT
BTN_DEAD = X
KEY_#300 = Y
KEY_#302 = A
KEY_#301 = B
BTN_BASE5 = LB
BTN_BASE6 = RB
BTN_THUMB = TL
BTN_THUMB2 = TR
BTN_TOP = start
BTN_TRIGGER = back
BTN_A = guide
BTN_TOP2    = du
BTN_PINKIE  = dr
BTN_BASE    = dd
BTN_BASE2   = dl
[axismap]
-Y1=Y1
-Y2=Y2
[calibration]
LT=0:127:255
RT=0:127:255
4. играем