Access elements of a characters list using const_iterator in C++ STL

C++ STL list: Here, we are implementing a program in which we are declaring a character list, pushing the characters and accessing the elements using const_iterator.
Submitted by IncludeHelp, on June 18, 2019

In this example, we are declaring a character list and pushing the characters from 'A' to 'Z' using a for loop and push_back() function and then accessing the elements using const_iterator.

Code:

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

int main()
{
    // list of character elements
    list<char> clist;

    // append characters from 'A' to 'Z'
    for (char i = 'A'; i <= 'Z'; ++i) {
        clist.push_back(i);
    }

    //declaring a const_iterator
    list<char>::const_iterator it;
    // printing all elements
    cout << "list (clist) elements: " << endl;
    for (it = clist.begin(); it != clist.end(); ++it) {
        cout << *it << ' ';
    }
    cout << endl;

    return 0;
}

Output

list (clist) elements:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.