創(chuàng)建一個(gè)list容器攒盈,放置6個(gè)整型數(shù)值[0, 1, 30, 20, 10, 0]
1.從后向前打印出容器內(nèi)的元素
- 向list容器后面添加兩個(gè)元素摄悯,并對(duì)容器內(nèi)的值求和并打印
- 打印鏈表的中間元素
- 找到不為0的元素窜管,復(fù)制到一個(gè)vector中并打印vector元素
那么首先我們就創(chuàng)建一個(gè)List容器辩撑,并放進(jìn)去6個(gè)int數(shù)值
這里我用了C++ 11語(yǔ)法來(lái)寫(xiě)
#include <iostream>
#include <list>
#include <vector>
void stl2nd() {
const int elems[6] = {0, 1, 30, 20, 10, 0};
std::list<int> intList;
for (auto i: elems)
intList.push_back(i);
}
int main() {
stl2nd();
return 0;
}
我把主要操作都留在stl2nd函數(shù)中了。
第一個(gè)問(wèn)題說(shuō)要從后向前打印出容器內(nèi)的元素屁奏。因?yàn)槊總€(gè)容器都是半閉半開(kāi)區(qū)間的。如果從前往后可以用是否等于end()來(lái)判斷错负,但是如果是從后向前坟瓢,那就只能和begin()來(lái)做判斷了。于是犹撒,我這么寫(xiě)载绿。
for (auto i = --intList.end(); ; --i) {
std::cout << " " << (*i);
if (i==intList.begin()) break;
}
第二個(gè)問(wèn)題是,向list容器后面添加兩個(gè)元素油航,并求和容器內(nèi)的值打印出來(lái)
這個(gè)容易崭庸,直接調(diào)用list.push_back()方法,然后求和打印即可
int a,b;
std::cout << "\ninput two integers separated by blank:";
std::cin >> a >> b;
for (auto i: {a,b})
intList.push_back(i);
a = 0;
for (auto i: intList)
a += i;
std::cout << "the summation for all integers is:" << a << std::endl;
第三個(gè)問(wèn)題是打印鏈表中間元素,這個(gè)與鏈表的長(zhǎng)度有關(guān)怕享,當(dāng)鏈表長(zhǎng)度是奇數(shù)時(shí)执赡,那么中間那個(gè)元素比較容易找到。如果鏈表長(zhǎng)度是偶數(shù)時(shí)函筋,那么相對(duì)而言沙合,中間元素應(yīng)該是2個(gè)。
std::cout << "the middle element(s) is(are):";
auto i = intList.size() /2 + (intList.size() % 2) - 1;
auto idx = 0;
for (auto j = intList.begin();j!=intList.end(); j++, idx++)
if (idx == i && intList.size() % 2 == 1) {
std::cout << " " << *j << std::endl;
break;
} else if (idx == i && intList.size() % 2 == 0) {
std::cout << " " << *j++ << " " << *j << std::endl;
break;
}
第四個(gè)問(wèn)題跌帐,找到不為0的元素首懈,并復(fù)制到一個(gè)vector容器中,再打印出vector所有元素谨敛。只需要對(duì)list進(jìn)行遍歷究履,判斷一下即可
std::vector<int> intVec;
for (auto i: intList)
if (i != 0)
intVec.push_back(i);
std::cout << "non-zero elements in new vector:";
for (auto i: intVec)
std::cout << " " << i;
最終,整個(gè)程序的代碼是
#include <iostream>
#include <list>
#include <vector>
void stl2nd() {
const int elems[6] = {0, 1, 30, 20, 10, 0};
std::list<int> intList;
for (auto i: elems)
intList.push_back(i);
for (auto i: intList) {
std::cout << " " << i;
}
std::cout << std::endl;
for (auto i = --intList.end(); ; --i) {
std::cout << " " << (*i);
if (i==intList.begin()) break;
}
int a,b;
std::cout << "\ninput two integers separated by blank:";
std::cin >> a >> b;
for (auto i: {a,b})
intList.push_back(i);
//intList.push_back(0);
a = 0;
for (auto i: intList)
a += i;
std::cout << "the summation for all integers is:" << a << std::endl;
std::cout << "the middle element(s) is(are):";
auto i = intList.size() /2 + (intList.size() % 2) - 1;
auto idx = 0;
for (auto j = intList.begin();j!=intList.end(); j++, idx++)
if (idx == i && intList.size() % 2 == 1) {
std::cout << " " << *j << std::endl;
break;
} else if (idx == i && intList.size() % 2 == 0) {
std::cout << " " << *j++ << " " << *j << std::endl;
break;
}
std::vector<int> intVec;
for (auto i: intList)
if (i != 0)
intVec.push_back(i);
std::cout << "non-zero elements in new vector:";
for (auto i: intVec)
std::cout << " " << i;
}
int main() {
stl2nd();
return 0;
}