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

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.