Access and access to elements in C++ STL list container

The list container does not provide at() and operator[], which makes it inconvenient to access the elements in the container, but we can use the iterator to access the elements. The example is as follows:

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

void print(double& Ele){
    cout<<Ele<<",";
}

intmain()
{
    list<double> mylist;
    mylist.push_back(11.1);
    mylist.push_back(22.2);
    mylist.push_back(33.3);
    mylist.push_back(44.4);
    int count=mylist.size();
    for_each(mylist.begin(),mylist.end(),print); //for_each遍历mylist
    cout<<endl;
    list<double>::iterator Iter_S;
    Iter_S=mylist.begin();
    cout<<"The Second element is "<<*(++(++Iter_S))<<endl;
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325554569&siteId=291194637
Recommended