Skip to content

Instantly share code, notes, and snippets.

View ashwin's full-sized avatar

Ashwin Nanjappa ashwin

View GitHub Profile
@ashwin
ashwin / num_to_string.cpp
Created September 17, 2014 02:29
Convert int or float to string in C++
#include <iostream>
#include <string>
int main()
{
int i = 9876;
float f = 3.14;
std::string si = std::to_string(i);
std::string sf = std::to_string(f);
@ashwin
ashwin / CompilingQtProgram.cmake
Last active August 29, 2015 14:07
Sample CMakeLists.txt to compile a program that uses Qt
# Check if Qt4 is present
find_package(Qt4 REQUIRED)
# Add Qt headers for compiling
include(
${QT_USE_FILE}
)
# Add Qt definitions for compilation
add_definitions(
@ashwin
ashwin / how_to_use_shared_ptr.cpp
Last active August 29, 2015 14:07
Sample C++ code to illustrate usage of std::shared_ptr
// Sample C++ code to illustrate usage of std::shared_ptr
// Compile with --std=c++11 option
#include <iostream>
#include <memory>
class Foo
{
public:
@ashwin
ashwin / emplace_back.cpp
Created October 9, 2014 15:26
Sample code to demonstrate emplace_back operation of vector
// Sample code to demonstrate emplace_back operation of vector
#include <iostream>
#include <vector>
struct Creature
{
Creature() : is_mammal_(false), age_(0), population_(0)
{
std::cout << "Default constructor called\n";
@ashwin
ashwin / multithread.py
Created December 31, 2014 01:53
Execute function in parallel across multiple input data items
# Class to execute a function in parallel across multiple data
# Adapted from code in Sec 9.5 of book Python Cookbook (2 Ed)
import threading
import time
import Queue
class MultiThread(object):
def __init__(self, function, argsVector, commonArgs, maxThreads=5, queue_results=False):
@ashwin
ashwin / matherror.cpp
Created January 5, 2015 02:26
How to check for math error in C++
// Checking for math error in C++
#include <cerrno>
#include <cmath>
#include <cstring>
#include <iostream>
std::cout << log(0) << std::endl;
if (errno)
std::cout << std::strerror(errno) << std::endl;
@ashwin
ashwin / disable_caps_lock.reg
Created January 12, 2015 09:52
Registry file to disable caps lock in Windows
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,00,00,3a,00,00,00,00,00
@ashwin
ashwin / mutex_queue.cpp
Created January 14, 2015 01:44
Example of using mutex to control concurrent access to a queue
#include <mutex>
#include <queue>
std::queue<int> q; // Queue which multiple threads might add/remove from
std::mutex m; // Mutex to protect this queue
void AddToQueue(int i)
{
m.lock();
q.push(i);
@ashwin
ashwin / lock_guard_queue.cpp
Created January 14, 2015 01:53
Example of using lock_guard to control concurrent access to a queue
#include <mutex>
#include <queue>
std::queue<int> q; // Queue which multiple threads might add/remove from
std::mutex m; // Mutex to protect this queue
void AddToQueue(int i)
{
std::lock_guard<std::mutex> lg(m); // Lock will be held from here to end of function
q.push(i);
@ashwin
ashwin / replace_full_word.vim
Created February 12, 2015 10:17
How to search and replace full word in Vim
:%s/\<foo\>/abracadabra/