stl iterator_在C ++ STL中使用const_iterator访问字符列表的元素

stl iterator

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.

在此示例中,我们声明一个字符列表,并使用for循环和push_back()函数 将字符'A' 送到'Z' ,然后使用const_iterator访问元素

Code:

码:

#include 
#include  //for list
using namespace std;

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

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

    //declaring a const_iterator
    list::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


翻译自: https://www.includehelp.com/stl/access-elements-of-a-characters-list-using-const_iterator-in-cpp-stl.aspx

stl iterator

你可能感兴趣的:(列表,python,java,leetcode,css)