关于list容器的问题

对于如下代码中的printList()函数,为什么括号里已经写const了还要用引用的方式传递呢?

#include<iostream>
#include<list>
using namespace std;

void printList(const list<int>&L)
{
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    list<int>L1;
    for (int i = 10; i < 51; i+=10)
    {
        L1.push_back(i);
    }

    printList(L1);
}

int main()
{
    test01();

    system("pause");
    return 0;
}

  • 这篇文章:List的使用 也许有你想要的答案,你可以看看

很明显是多此一举
这里只取值根本不赋值,连const也是白写的

这个是C++的一种妥协, 用这种参数, 你可以传递左值和右值, 以及带 const 和 不带 const 的参数. 同时不必再拷贝并析构一份对象.