Skip to content

Instantly share code, notes, and snippets.

View zhangyuchi's full-sized avatar

Terrell Franzman zhangyuchi

View GitHub Profile
// sock is bound AF_INET socket, usually SOCK_DGRAM
// include struct in_pktinfo in the message "ancilliary" control data
setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt));
// the control data is dumped here
char cmbuf[0x100];
// the remote/source sockaddr is put here
struct sockaddr_in peeraddr;
// if you want access to the data you need to init the msg_iovec fields
struct msghdr mh = {
.msg_name = &peeraddr,
#include <iostream>
#include <memory>
struct Foo {
Foo() { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n"; }
};
struct D {
void operator() (Foo* p) {
@zhangyuchi
zhangyuchi / transform.cc
Last active August 29, 2015 14:09
transform vector<string> to vector<int>
std::transform(DataNumbers.begin(), DataNumbers.end(), std::back_inserter(Data),
[](const std::string& str) { return std::stoi(str); });
#include <functional>
typedef int(*stoi_type)(const std::string&, std::size_t*, int);
std::transform(DataNumbers.begin(), DataNumbers.end(), std::back_inserter(Data),
std::bind(static_cast<stoi_type>(&std::stoi),
std::placeholders::_1, nullptr, 10));
@zhangyuchi
zhangyuchi / template_alias.cc
Created November 13, 2014 13:48
alias of template
//c++98
template<typename T>
class SomeClass;
template<typename T>
struct MyTypeDef
{
typedef SomeClass<T> type;
};
@zhangyuchi
zhangyuchi / clock.c
Created November 14, 2014 06:16
get high jingdu time
#define _POSIX_C_SOURCE 199309L /*note it*/
#include <time.h>
#include <stdio.h>
int main()
{
float f=123.013;
for(int j=0; j<2; ++j)
{
@zhangyuchi
zhangyuchi / boostbind.cc
Last active August 29, 2015 14:09
boost transform bind sample
struct Myclass
{
std::string getFirstString() {return string1}
std::string getSecondString() {return string2}
private:
std::string string1;
std::string string2;
}
@zhangyuchi
zhangyuchi / array_address.c
Created November 18, 2014 02:46
pointer and array in struct
#include <stdio.h>
struct str{
int len;
char s[0];
};
struct foo {
struct str *a;
};
@zhangyuchi
zhangyuchi / templatearray.cc
Last active August 29, 2015 14:09
template usage
// return size of an array as a compile-time constant. (The
// array parameter has no name, because we care only about
// the number of elements it contains.)
template<typename T, std::size_t N> // see info
constexpr std::size_t arraySize(T (&)[N]) noexcept // below on
{ // constexpr
return N; // and
}
template<typename Container, typename Index> // final
@zhangyuchi
zhangyuchi / lambda.cc
Created November 18, 2014 10:10
c++14 lambda auto params
auto derefLess = // C++14 comparison
[](const auto& p1, // function for
const auto& p2) // values pointed
{ return *p1 < *p2; }; // to by anything
// pointer-like
@zhangyuchi
zhangyuchi / alloctor.cc
Last active August 29, 2015 14:10
allocator of shared
#include <memory>
//full version
template<typename T>
struct Allocator
{
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;