目前分類:[技術分享] (126)

瀏覽方式: 標題列表 簡短摘要
 #include <iostream>  
 #include <string>  
 #include <vector>  
 #include <set>  
 #include <map>  
 #include <unordered_map>  
 using namespace std;  
 int main()  
 {  
      map<string, int>dict;  
      // 基本操作  
      // 1. Insert data  
      // 2. 判斷是否有資料  
      // 3. 查找  
      // 1. Insert data  
      dict.insert(pair<string, int>("cat", 2));  
      dict.insert(map<string, int>::value_type("banana", 3));  
      dict["apple"] = 10;  
      // 2. 判斷是否有資料  
      if (dict.empty())  
           cout << "dict is not empty" << endl;  
      else  
           cout << "dict size is : " << dict.size() << endl;  
      for (auto D : dict)  
      {  
           // 這邊Print的值(value)會按照字母順序(因為map是有序的)  
           // 輸出順序為 :apple(10)->banana(3)->cat(2)  
           cout << D.first << "=>" << D.second << endl;  
      }  
      map<string, int>::iterator iter;  
      // 3. 查找, 返回一個迭代器指向鍵值為key的元素,如果沒找到就返回end()  
      if ((iter = dict.find("banana")) != dict.end())   
      {  
           cout << "Find key[banana], value: " << iter->second;  
      }  
      else  
      {  
           cout << "Can't find the key[banana]" << endl;                    
      }  
      // unordered_map  
      /*  
            template < class Key,             // unordered_map::key_type  
            class T,                   // unordered_map::mapped_type  
            class Hash = hash<Key>,            // unordered_map::hasher  
            class Pred = equal_to<Key>,          // unordered_map::key_equal  
            class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type  
            > class unordered_map;  
            Unordered Map  
            Unordered maps are associative containers that store elements   
            formed by the combination of a key value and a mapped value,   
            and which allows for fast retrieval of individual elements based on their keys.  
            In an unordered_map, the key value is generally used to uniquely identify the element,  
            while the mapped value is an object with the content associated to this key.  
            Types of key and mapped value may differ.  
            Internally, the elements in the unordered_map are not sorted in any particular   
            order with respect to either their key or mapped values, but organized into buckets  
            depending on their hash values to allow for fast access to individual  
            elements directly by their key values (with a constant average time complexity on average).  
            unordered_map containers are faster than map   
            containers to access individual elements by their key,   
            although they are generally less efficient for range iteration through  
            a subset of their elements.  
            Unordered maps implement the direct access operator   
            (operator[]) which allows for direct access of the mapped value   
            using its key value as argument.  
            Iterators in the container are at least forward iterators.  
      */  
      unordered_map<string, int>unordered_dict;  
      unordered_dict.insert(pair<string, int>("apple", 2));  
      unordered_dict.insert(unordered_map<string, int>::value_type("orange", 3));  
      unordered_dict["banana"] = 10;  
      if (unordered_dict.empty())  
      {  
           cout << "No elements" << endl;  
      }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
      else  
      {  
           cout << "unorderd_dict size is : " << unordered_dict.size() << endl;   
      }  
      for (auto D : unordered_dict)  
      {  
           cout << D.first << "=>" << D.second << endl;  
      }  
      /*  
      *     output:  
      *               apple=>2  
      *               orange=>3  
      *               banana=>10  
      */  
 }  

 

 

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

 

會有這一篇的產生,是因為早上在刷 leetcode的時候,突然想說是不是好想沒有更新文章了,也好久沒有時間靜下來寫教學了,所以,乾脆就拿今天解題的方法來寫一篇文章算了。

文章標籤

Eric 發表在 痞客邦 留言(1) 人氣()

Given a matrix A, return the transpose of A.

The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column indices of the matrix.

Eric 發表在 痞客邦 留言(0) 人氣()

Given a string S of lowercase letters, a duplicate removalconsists of choosing two adjacent and equal letters, and removing them.

We repeatedly make duplicate removals on S until we no longer can.

Eric 發表在 痞客邦 留言(0) 人氣()

A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.  For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.

A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.

Eric 發表在 痞客邦 留言(0) 人氣()

Given a valid (IPv4) IP address, return a defanged version of that IP address.

A defanged IP address replaces every period "." with "[.]".

Eric 發表在 痞客邦 留言(0) 人氣()

  /*  
  * main.cpp  
  *  
  * Created on: 2014.06.08  
  *   Author: Spike  
  */  
 /*vs 2012*/  
 #include <iostream>  
 #include <string>  
 #include <map>  
 #include <windows.h>  
 #include <TlHelp32.h>  
 #include <vector>   
 #include <clocale>  
 using namespace std;  
 // wstring -> string  
 std::string& to_string(std::string& dest, std::wstring const & src)  
 {  
      std::setlocale(LC_CTYPE, "");  
      size_t const mbs_len = wcstombs(NULL, src.c_str(), 0);  
      std::vector<char> tmp(mbs_len + 1);  
      wcstombs(&tmp[0], src.c_str(), tmp.size());  
      dest.assign(tmp.begin(), tmp.end() - 1);  
      return dest;  
 }  
 bool traverseProcesses(std::map<std::string, int>& _nameID)   
 {  
      PROCESSENTRY32 pe32;  
      pe32.dwSize = sizeof(pe32);  
      HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
      if(hProcessSnap == INVALID_HANDLE_VALUE) {  
           std::cout << "CreateToolhelp32Snapshot Error!" << std::endl;;  
           return false;  
      }  
      BOOL bResult =Process32First(hProcessSnap, &pe32);  
      int num(0);  
      while(bResult)   
      {  
           std::wstring name = pe32.szExeFile;  
           int id = pe32.th32ProcessID;  
           string strName;  
           to_string(strName,name);  
           cout << "[" << ++num << "] : " <<"Process Name:"   
                << strName << " " << "ProcessID:" << id<< std::endl;  
           _nameID.insert(std::pair<string, int>(strName, id));   
           bResult = Process32Next(hProcessSnap,&pe32);  
      }  
      CloseHandle(hProcessSnap);  
      return true;  
 }  
 int main()  
 {  
      std::map<std::string, int> _nameID;  
      if (!traverseProcesses(_nameID)) {  
           cout << "Start Process Error!" << endl;  
      }  
      return 0;  
 }  

 

 

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

 

 

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

 

按下斷點,然後在Debug模式下,會不能停在斷點的地方,其提示 :

Eric 發表在 痞客邦 留言(0) 人氣()

 

 

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

 

Eric 發表在 痞客邦 留言(0) 人氣()

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Eric 發表在 痞客邦 留言(0) 人氣()

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Eric 發表在 痞客邦 留言(0) 人氣()

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Eric 發表在 痞客邦 留言(0) 人氣()

C++11 標準中所新增的 lambda expression 語法,可以讓函數的定義與使用更加有彈性,程式碼看起來也更簡潔。

C++11 的標準中加入了一個新的 lambda expression 語法,如果您有一陣子沒有注意最新的 C++ 標準,看到這樣的寫法可能會感覺很奇怪,以下我們將介紹 lambda expression 的使用方式與時機,並提供幾個範例作為參考。

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

如今的程式是一場工程師和上帝的競賽,工程師要開發出更大更好、傻瓜都會用到軟體。而上帝在努力創造出更大更傻的傻瓜。目前為止,上帝是贏的。(Rick Cook)
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. (Rick Cook)

 

Eric 發表在 痞客邦 留言(0) 人氣()

 

最近接了新任務,所以有這部分的需求,

Eric 發表在 痞客邦 留言(0) 人氣()

 重載運算子

 

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

C++ Socket資料整理

Server 端執行流程:

文章標籤

Eric 發表在 痞客邦 留言(0) 人氣()

Close

您尚未登入,將以訪客身份留言。亦可以上方服務帳號登入留言

請輸入暱稱 ( 最多顯示 6 個中文字元 )

請輸入標題 ( 最多顯示 9 個中文字元 )

請輸入內容 ( 最多 140 個中文字元 )

reload

請輸入左方認證碼:

看不懂,換張圖

請輸入驗證碼