Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The last element of the list 1

Status
Not open for further replies.

jslmvl

Vendor
Jan 26, 2008
268
GB
Hi,

I try to get the first and last elements of the list but I cannot get the last. Can you tell me why:

int main()
{
list<int> L;
L.push_back(1);
L.push_back(2);
cout<<*L.begin()<<*L.end()<<endl;
return 0;
}
 
end() returns an iterator to one past the end of the list. Assuming you know the list is not empty, you could use *(--end()), but a more clear solution might be to use front() and back() which actually return references to the values in the list instead of iterators:
Code:
cout<<L.front()<<L.back()<<endl;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top