Boolan STL与泛型编程 第二周作业

创建一个list容器,放置6个整型数值[0, 1, 30, 20, 10, 0]
1.从后向前打印出容器内的元素

  1. 向list容器后面添加两个元素,并对容器内的值求和并打印
  2. 打印链表的中间元素
  3. 找到不为0的元素,复制到一个vector中并打印vector元素

那么首先我们就创建一个List容器,并放进去6个int数值
这里我用了C++ 11语法来写

#include 
#include 
#include 

void stl2nd() {
    const int elems[6] = {0, 1, 30, 20, 10, 0};
    std::list intList;
    for (auto i: elems)
        intList.push_back(i);
}

int main() {
    stl2nd();
    return 0;
}

我把主要操作都留在stl2nd函数中了。
第一个问题说要从后向前打印出容器内的元素。因为每个容器都是半闭半开区间的。如果从前往后可以用是否等于end()来判断,但是如果是从后向前,那就只能和begin()来做判断了。于是,我这么写。

for (auto i = --intList.end(); ; --i) {
        std::cout << " " << (*i);
        if (i==intList.begin()) break;
    }

第二个问题是,向list容器后面添加两个元素,并求和容器内的值打印出来
这个容易,直接调用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;

第三个问题是打印链表中间元素,这个与链表的长度有关,当链表长度是奇数时,那么中间那个元素比较容易找到。如果链表长度是偶数时,那么相对而言,中间元素应该是2个。

    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;
        }

第四个问题,找到不为0的元素,并复制到一个vector容器中,再打印出vector所有元素。只需要对list进行遍历,判断一下即可

    std::vector 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;

最终,整个程序的代码是

#include 
#include 
#include 

void stl2nd() {
    const int elems[6] = {0, 1, 30, 20, 10, 0};
    std::list 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 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;
}

你可能感兴趣的:(Boolan STL与泛型编程 第二周作业)