C++ 刷leetcode用法筆記(持續更新)

Newone Tsai
Apr 4, 2021

--

這篇主要是寫一些,C++的一些用法,可以增進寫code速度

  1. For-Auto

簡單來說,就是取代最常用的for(i=0;i<s.length();i++),如下

// 傳統作法 
for (int k = 0; k < ls.size(); k++) {
cout << ls[k];
}
// 傳統作法(變化)
for (int k = 0; k < ls.size(); k++) {
char i = ls[k];
cout << i;
}
// 把 char i 寫進 for 裡,等同上者
for (char i:ls)
cout << i;
// 用 auto 讓 i 自動套用 陣列(ls) 的資料型態
for (auto i : ls)
cout << i;

--

--

Newone Tsai
Newone Tsai

Written by Newone Tsai

I took the one less traveled by, and that has made all the difference.

No responses yet