C++

[C++] Measure of time elapsed

#include <ctime>
using namespace std;

clock_t begin = clock();
target_func();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "Elapsed time: " << elapsed_secs << " sec." << endl;

[C++] Using msgpack-c with FIO

Output serialized objects to ofstream:

#include <msgpack.hpp>
 ofstream out("output.txt");
 msgpack::packer<std::ofstream> pk(&out);
 //packing an integer
 int n=10;
 pk.pack(n);
 //packing a integer list
 vector<int> num_list;
 num_list.push_back(1); num_list.push_back(2); num_list.push_back(3);
 pk.pack(num_list);
 //packing a integer list one by one 
 pk.pack_array(num_list.size());
 for(int j = 0;j<num_list.size();j++){
   pk.pack(num_list[i]);
 }

Input serialized object from ifstream:

ifstream in("input.txt",ios::binary);
stringstream buffer;
buffer << in.rdbuf();

msgpack::unpacker pac;
// feeds the buffer.
pac.reserve_buffer(buffer.str().size());
memcpy(pac.buffer(), buffer.str().data(), buffer.str().size());
pac.buffer_consumed(buffer.str().size());

// now starts streaming deserialization.
msgpack::object_handle oh;
while(pac.next(oh)) {
 std::cout << oh.get() << std::endl;
}

[C++] cout formatting

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

int main()
{
 float x = 123.123556;
 //output:123.124
 cout << fixed <<setprecision(3) << x << endl;
 return 0;
}

[C++] log(),log10()

#include<cmath>
//using natural log
cout<<log(10)<<endl;

//using 10 based log
cout<<log10(10)<<endl;

[C++] max_element()&min_element()

#include <algorithm>    // std::min_element, std::max_element
bool myfn(int a,int b){
  return a < b;
}
int myints[] = {3,7,2,5,6,4,9};

// using default comparison:
cout << "The smallest " << *min_element(myints,myints+7) << endl;
cout << "The largest "  << *max_element(myints,myints+7) << endl;

// using function myfn as comp:
cout << "The smallest " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest "  << *max_element(myints,myints+7,myfn) << endl;

[C++] map–usage

Search by key:

map<char,int>::iterator it;
it = mymap.find('b');
if (it != mymap.end())
    //found!
else
    //not found!

Traversal

for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    cout<<iter->first<<" "<<iter->second<<endl;

Sorting:

bool compFunc(pair<char, int=""> a, pair<char, int=""> b) {
   //sort whatever you want (by key, value or both)
}
map<char, int> lan_list;
vector< pair<char, int> > sort_tmp(lan_list.begin(), lan_list.end());
sort(sort_tmp.begin(), sort_tmp.end(), compFunc);

[C++] next_permutation

#include <iostream>     
#include <algorithm>    
using namespace std;
int main () {
  int myints[] = {1,2,3};

  sort (myints,myints+3);
  
  do {
    cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << endl;
  } while ( next_permutation(myints,myints+3) );

  cout << "After loop: " << myints[0] << ' ' << myints[1] 
        << ' ' << myints[2] << endl;

  return 0;
}

output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3

[C++] ctime–Finding Weekday

#include<iostream>
#include<ctime>
using namespace std;
int main() {
 tm time{0,0,0,21,1-1,2017-1900};
 mktime(&time);
 cout << time.tm_wday << endl;
 system("pause");
 return 0;
}
tm:
 int tm_sec; // seconds after the minute - [0, 60] including leap second
 int tm_min; // minutes after the hour - [0, 59]
 int tm_hour; // hours since midnight - [0, 23]
 int tm_mday; // day of the month - [1, 31]
 int tm_mon; // months since January - [0, 11]
 int tm_year; // years since 1900
 int tm_wday; // days since Sunday - [0, 6]
 int tm_yday; // days since January 1 - [0, 365]
 int tm_isdst; // daylight savings time flag

[C++] add_node

#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class node
{
    public:
    int num;
    node *next;
    node()
    {
        num=0;
        next=NULL;
    } 
};
node *add_node(node *mylist,int data)
{
    node *temp_node=mylist;
    if(mylist==NULL)
    {
        mylist=new node;
        mylist->num=data;
        return mylist;
    }
    while(temp_node->next!=NULL)
    {
        temp_node=temp_node->next;
    }
    temp_node->next=new node;
    temp_node->next->num=data;
    return mylist;
}
void show(node *mylist)
{
    while(mylist!=NULL)
    {
        cout<<mylist->num<<"\n";
        mylist=mylist->next;
    }
    return;
}
int main()
{
    node *list=NULL;
    for(int i=0;i<5;i++)
    {
        list=add_node(list,i);
    }
    show(list);
    system("pause");
    return 0;
}