Languages

[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);

[Python] Big5 and utf-8

中文的 windows cmd 編碼預設是Big5(cp950) ,而 Python3 的預設程式碼編碼是utf-8 (cp65001),如果在輸出時產生 「UnicodeEncodeError: ‘cp950’ codec can’t encode character … …」的錯誤必須在程式開頭加上:

# -*- coding: utf-8 -*-

若程式會在console輸出,則使用

print(mytext.encode(sys.stdin.encoding, "replace").decode(sys.stdin.encoding))

若使用檔案輸出,則必須在open中指定編碼參數:

out_file = open("File.txt","w",encoding="utf-8")

[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

[Python] operator.itemgetter

Word Counter Sorted by Times(Chinese Version):

import operator
in_file = open("input.txt")
raw_article = in_file.read()
table={}
for word in raw_article:
 if word in table:
   table[word] = table[word]+1
 else:
   table[word] = 1
sorted_table = sorted(table.items(),key=operator.itemgetter(1),reverse=True)
for pair in sorted_table:
 print(pair[0],":",pair[1])

[Python] Anaconda

  • numpy and matplotlib
import numpy as np
import matplotlib.pyplot as pt
x = np.arange(0,360)
y = np.sin(x*np.pi/180)
pt.plot(x,y)
pt.xlim(0,360)
pt.ylim(-2,2)
pt.title("SINE")
pt.show()
%e6%9c%aa%e5%91%bd%e5%90%8d

[Python] requests

import requests
www = requests.get("http://www.google.com")
print(www.text)

[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;
}

[C++] Operator overloading Part2

#include<iostream>
#include<cstring>
#define MAX_STRING 100
using namespace std;
class myString
{
    friend istream& operator >> (istream&, myString&);
    friend ostream& operator << (ostream&, myString&);
    private:
        char content[MAX_STRING];
    public:
        char operator [] (int);
        myString operator += (myString);
};
istream &operator >> (istream &in, myString &temps)
{
    in >> temps.content;
    return in;
}
ostream &operator << (ostream &out, myString &temps)
{
    out << temps.content;
    return out;
}
char myString::operator [] (int n)
{
    return content[n];
}
myString myString::operator += (myString s)
{
    strcat_s(content, s.content);
    return *this;
}
int main()
{
    myString s1,s2;
    cin >> s1 >> s2;
    cout << s1[0]<<endl;
    cout << (s1 += s2) << endl;
    system("pause"); 
    return 0;
}