創作內容

4 GP

【C++】Vector and Map and Iterator and Two_Sum

作者:パっパっパスタ│2019-09-21 23:13:27│巴幣:8│人氣:390
vector
#include <iostream>
#include <vector>

using namespace std;

//vector:

int main(){
    vector<int> raw;
    raw.push_back(8);
    raw.push_back(7);
    raw.push_back(6);
    raw.push_back(3);
    for(vector<int>::iterator iter = raw.begin(); iter != raw.end(); iter++){
        cout << *iter << endl;

map
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(){
    map<string, string> hashmap;
    map<string, string>::iterator iter;
   
    hashmap.insert(pair<string, string>("r000", "ST1"));
    hashmap["r111"] = "ST2";
    hashmap["r222"] = "ST3";


    for(iter = hashmap.begin(); iter != hashmap.end(); ++iter){
        cout << iter->first << " , " << iter->second << endl;
    }

/*
r000 , ST1
r111 , ST2
r222 , ST3
*/


    iter = hashmap.find("r000");
    if(iter != hashmap.end()){
        cout << iter->second;
    }else{
        cout << "Not found!";
    }

//ST1

}

Two Sum

// Two Sum

#include <iostream>
#include <vector>
#include <map>

using namespace std;

class Solution{
public:
    vector<int> twosum(vector<int> &nums, int target){
        map <int, int> hashtable;            //store value and index
        map <int, int> :: iterator iter;     //iterator for find value
        vector <int> ansIndex(2);            //store index answer

        for(int i = 0; i < nums.size(); i++){
            iter = hashtable.find(target - nums[i]);

            if(iter != hashtable.end()){
                ansIndex[0] = iter->second;
                ansIndex[1] = i;
                return ansIndex;
            }else{
                //map only can change value, not key
                hashtable[nums[i]] = i;                
            }
        }
        cout << "Not found!" << endl;
    }
};

/*
    step by step:

    i->0 == 2
    hashtable.find(9 - 7 == 2) (X) #hashtable is clear
    hashtable[2](key) = 0(value)

    i->1 == 7
    hashtable.find(9 - 7 == 2) (O)    
    there is a '2' in hashtable, its value is 0!
    now i == 1 and iter is adress of '2' which in hashtable
    and iter->second is hashtable[2]'s value '0'
    so return index(0) and index(1)
*/


int main(){
    vector <int> arr;    //imput value
    arr.push_back(2);
    arr.push_back(7);
    arr.push_back(11);
    arr.push_back(15);
    int target = 9;

    Solution kkk;    //call function and print
    vector <int> point = kkk.twosum(arr , target);
    for(vector <int> :: iterator iter2 = point.begin(); iter2 != point.end(); iter2++){
        cout << "Index[ " << *iter2 << " ] " << endl;
    }
}

搞了三小時把vector map iterator還有最簡單的LeetCode第一題弄懂
我怕忘記所以先記錄起來
因為沒有每天認真寫題目思考
最近真的覺得邏輯思考能力真的在退化.....
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4536546
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 4 篇留言

雨丸✰“ReのLife”★
佬…另一個世界的語言

09-21 23:17

パっパっパスタ
為了以後有飯吃 這些都是要學的嗚嗚09-21 23:41
雨丸✰“ReのLife”★
(對我來說)

09-21 23:17

パっパっパスタ
窩不是大佬啦 :( 所以在這篇把語法跟用法記錄起來,方便以後複習09-21 23:42

range for也蠻好用ㄉ 不然用iterator跑迴圈其實有點麻煩...

09-21 23:19

パっパっパスタ
原來除了iterator還有range for 這種東東!!!
iterator 我是覺得它就是一個用來掃描容器內容的工具,不知道這樣理解對不對?
每次for迴圈裡面都要寫一長串就真的挺麻煩的09-21 23:47
❖十代目霸王❖
霸言霸語

09-21 23:19

パっパっパスタ
嗚嗚 我現在才開始要刷題是不是有點太晚了....09-21 23:47
我要留言提醒:您尚未登入,請先登入再留言

4喜歡★taiwan8575 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:N1真的難... 後一篇:SHISHAMO(シシャ...

追蹤私訊切換新版閱覽

作品資料夾

d88931122所有巴友
歡迎諸君來參觀老僧的小屋,內含Steam與Google Play遊戲、3D角色模組、Line貼圖看更多我要大聲說7小時前


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】