Re: [問題] auto用法一問

看板C_and_CPP (C/C++)作者 (eopXD)時間2年前 (2021/11/03 14:42), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
: #include <iostream> : #include <vector> : using namespace std; : int main(){ : vector<int> ans = {1,2,3}; : auto n = ans.size(); : for(auto j = n - 2;j >= 0;--j)//改成int j = n -2就ok : { : printf("test %ld\n",j); : } : } vector::size 的函式原型是 [1] size_type size() const; 而這裡的 size_type 可以參照 vector 這個 container 中的 member types [2]。 可以看到 vector::size_type 是 std::size_t。 值得留意的是 size_t 是根據編譯時指定的模式來決定, 並不是固定 32-bit 或是 64-bit,詳細請見 [3] 中的 Notes。 這裡 unsigned integral 種類再減一會造成負溢位,一個簡單的實驗是 unsigned int x = -1; printf("%x\n", x); 印出來會是 x 的 16 進位表示法,你會看到 ffffffff。 最後再補充一個,member type 的存在是一個很棒的東東, 因為他可以讓 container 之間互相相容。 [1] https://en.cppreference.com/w/cpp/container/vector/size [2] https://en.cppreference.com/w/cpp/container/vector [3] https://en.cppreference.com/w/cpp/types/size_t -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.197.217 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1635921770.A.5F6.html
文章代碼(AID): #1XWYzgNs (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):
文章代碼(AID): #1XWYzgNs (C_and_CPP)