[問題] C++ list 元素查看
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux G++
程式碼(Code):(請善用置底文網頁, 記得排版)
#include <iostream>
#include <list>
using namespace std;
main()
{
int count =1;
list<int> L;
L.push_back(0);
L.push_front(0);
L.insert(++L.begin(),2);
L.push_back(5);
L.push_back(6);
L.sort();
list<int>::iterator i;
for(i=L.begin(); i != L.end(); ++i)
{
cout << "第" << count << "個 "<< *i << " "<< endl;
count++;
}
return 0;
}
我們都知道用 iterator去觀看list所有的元素。
也可以用 L.front();去看第一個元素,
但有什麼方法可以看第二個元素?
謝謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.89.77
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1449667965.A.175.html
※ 編輯: hardware (140.117.89.77), 12/09/2015 22:00:30
→
12/09 22:08, , 1F
12/09 22:08, 1F
→
12/09 23:28, , 2F
12/09 23:28, 2F
→
12/09 23:55, , 3F
12/09 23:55, 3F
→
12/09 23:56, , 4F
12/09 23:56, 4F
→
12/09 23:56, , 5F
12/09 23:56, 5F
iterator是去查看元素,如果我今天要把他儲存下來呢?
for(i=L.begin(); i != L.end(); ++i)
{
cout << "第" << count << "個 "<< *i << " "<< endl;
sum = *i;
count++;
}
我用這樣子為什麼不行?
→
12/10 00:41, , 6F
12/10 00:41, 6F
推
12/10 00:54, , 7F
12/10 00:54, 7F
→
12/10 09:34, , 8F
12/10 09:34, 8F
※ 編輯: hardware (140.117.89.77), 12/10/2015 09:38:10
→
12/10 09:46, , 9F
12/10 09:46, 9F
抱歉,寫錯了應該是
sum = sum + *i;
謝謝~
推
12/10 10:09, , 10F
12/10 10:09, 10F
※ 編輯: hardware (140.117.89.77), 12/10/2015 10:20:23
→
12/10 10:20, , 11F
12/10 10:20, 11F
→
12/10 12:24, , 12F
12/10 12:24, 12F
討論串 (同標題文章)