Skip to content

Instantly share code, notes, and snippets.

std::vector<int> v(N);
// Initialize the array with random elements between 0 and 100
for_each(v.begin(), v.end(), [](int& cur){ cur = rand()%100; });
auto print_vec = [](std::ostream& out, const std::vector<int>& vec) {
std:copy(v.begin(), v.end(), std::ostream_iterator<int>(out, ", "));
out << std::endl
};
template <int Begin, int End, int Size, class Enable=void>
struct partition;
template <int BeginIt, int EndIt, int Size>
struct partition<BeginIt, EndIt, Size, typename std::enable_if<(BeginIt<EndIt)>::type> {
template <class T>
inline static void go(T* vec, const T& pivot) {
if (vec[BeginIt] <= pivot) {
std::auto_ptr<int> a( new int ),
b( new int );
*a = *b = 0;
a = b;
*b; // SEGFAULT
#include <iostream>
#include <vector>
class Instance {
const Instance* ref;
int value;
public:
Instance (const Instance* other, int value) :
ref(ref), value(value) { }
};
#include <iostream>
#include <vector>
#include <memory>
class Instance {
std::shared_ptr<const Instance> ref;
int value;
public:
Instance (const std::shared_ptr<const Instance>& other, int value) :
ref(other), value(value) { }
#include <memory>
class Instance {
std::weak_ptr<const Instance> ref;
int value;
public:
Instance (const std::shared_ptr<const Instance>& other, int value) :
ref(other), value(value) { }
void setRef(const std::shared_ptr<const Instance>& ref) {
@motonacciu
motonacciu / get_size
Last active December 17, 2015 09:48
get_size predicate returns the size in bytes of the object being serialized
#include <tuple>
#include <numeric>
#include <vector>
#include <string>
namespace detail {
template <class T>
struct get_size_helper;
#include <tuple>
#include <numeric>
#include <vector>
#include <string>
// forward declaration of get_size() method
template <class T>
size_t get_size(const T& obj);
namespace detail {
#include <gtest/gtest.h>
#include "utils/serialize.h"
TEST(get_size, Ints) {
uint16_t v1 = 2u;
EXPECT_EQ(sizeof(decltype(v1)), get_size(v1));
EXPECT_EQ(2u, get_size(v1));
uint32_t v2 = 10u;
EXPECT_EQ(sizeof(decltype(v2)), get_size(v2));
namespace detail {
template <class T>
class serialize_helper;
template <class T>
void serializer(const T& obj, StreamType::iterator&);
template <class tuple_type>
inline void serialize_tuple(const tuple_type& obj, StreamType::iterator& res, int_<0>) {