青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

我輩豈是蓬蒿人!

C++ && keyWordSpotting

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  11 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

常用鏈接

留言簿(9)

我參與的團隊

搜索

  •  

積分與排名

  • 積分 - 7323
  • 排名 - 1369

最新評論

閱讀排行榜

評論排行榜

http://www.cprogramming.com/tutorial/stl/iterators.html

The concept of an iterator is fundamental to understanding the C++ Standard Template Library (STL) because iterators provide a means for accessing data stored in container classes such a vector, map, list, etc.

You can think of an iterator as pointing to an item that is part of a larger container of items. For intance, all containers support a function called begin, which will return an iterator pointing to the beginning of the container (the first element) and function, end, that returns an iterator corresponding to having reached the end of the container. In fact, you can access the element by "dereferencing" the iterator with a *, just as you would dereference a pointer.

To request an iterator appropriate for a particular STL templated class, you use the syntax

1?std::class_name<template_parameters>::iterator?name

where name is the name of the iterator variable you wish to create and the class_name is the name of the STL container you are using, and the template_paramters are the parameters to the template used to declare objects that will work with this iterator. Note that because the STL classes are part of the std namespace, you will need to either prefix every container class type with "std::", as in the example, or include "using namespace std;" at the top of your program.

For instance, if you had an STL vector storing integers, you could create an iterator for it as follows:
1std::vector<int>?myIntVector;
2std::vector<int>::iterator?myIntVectorIterator;
Different operations and containers support different types of iterator behavior. In fact, there are several different classes of iterators, each with slightly different properties. First, iterators are distinguished by whether you can use them for reading or writing data in the container. Some types of iterators allow for both reading and writing behavior, though not necessarily at the same time.

Some of the most important are the forward, backward and the bidirectional iterators. Both of these iterators can be used as either input or output iterators, meaning you can use them for either writing or reading. The forward iterator only allows movement one way -- from the front of the container to the back. To move from one element to the next, the increment operator, ++, can be used.

For instance, if you want to access the elements of an STL vector, it's best to use an iterator instead of the traditional C-style code. The strategy is fairly straightforward: call the container's begin function to get an iterator, use ++ to step through the objects in the container, access each object with the * operator ("*iterator") similar to the way you would access an object by dereferencing a pointer, and stop iterating when the iterator equals the container's end iterator. You can compare iterators using != to check for inequality, == to check for equality. (This only works for one twhen the iterators are operating on the same container!)

The old approach (avoid)
?1?using?namespace?std;
?2?
?3?vector<int>?myIntVector;
?4?
?5?//?Add?some?elements?to?myIntVector
?6?myIntVector.push_back(1);
?7?myIntVector.push_back(4);
?8?myIntVector.push_back(8);
?9?
10?for(int?y=0;?y<myIntVector.size();?y++)
11?{
12?????cout<<myIntVector[y]<<"?";??//Should?output?1?4?8
13?}
The STL approach (use this)
?1?using?namespace?std;
?2?
?3?vector<int>?myIntVector;
?4?vector<int>::iterator?myIntVectorIterator;
?5?
?6?//?Add?some?elements?to?myIntVector
?7?myIntVector.push_back(1);
?8?myIntVector.push_back(4);
?9?myIntVector.push_back(8);
10?
11?for(myIntVectorIterator?=?myIntVector.begin();?
12?????????myIntVectorIterator?!=?myIntVector.end();
13?????????myIntVectorIterator++)
14?{
15?????cout<<*myIntVectorIterator<<"?";????//Should?output?1?4?8
16?}
17?
As you might imagine, you can use the decrement operator, --, when working with a bidirectional iterator or a backward operator.

Iterators are often handy for specifying a particular range of things to operate on. For instance, the range item.begin(), item.end() is the entire container, but smaller slices can be used. This is particularly easy with one other, extremely general class of iterator, the random access iterator, which is functionally equivalent to a pointer in C or C++ in the sense that you can not only increment or decrement but also move an arbitrary distance in constant time (for instance, jump multiple elements down a vector).

For instance, the iterators associated with vectors are random access iterators so you could use arithmetic of the form
iterator + n
where n is an integer. The result will be the element corresponding to the nth item after the item pointed to be the current iterator. This can be a problem if you happen to exceed the bounds of your iterator by stepping forward (or backward) by too many elements.

The following code demonstrates both the use of random access iterators and exceeding the bounds of the array (don't run it!):
1?vector<int>?myIntVector;
2?vector<int>::iterator?myIntVectorIterator;
3?myIntVectorIterator?=?myIntVector.begin()?+?2;
You can also use the standard arithmetic shortcuts for addition and subtraction, += and -=, with random access iterators. Moreover, with random access iterators you can use <, >, <=, and >= to compare iterator positions within the container.

Iterators are also useful for some functions that belong to container classes that require operating on a range of values. A simple but useful example is the erase function. The vector template supports this function, which takes a range as specified by two iterators -- every element in the range is erased. For instance, to erase an entire vector:
1?vector<int>::iterator?myIntVectorIterator;
2?myIntVector.erase(myIntVectorIterator.begin(),?myIntVectorIterator.end());
which would delete all elements in the vector. If you only wanted to delete the first two elements, you could use
1myIntVector.erase(myIntVectorIterator.begin(),?myIntVectorIterator.begin()+2);
Note that various container class support different types of iterators -- the vector class, which has served as our model for iterators, supports a random access iterator, the most general kind. Another container, the list container (to be discussed later), only supports bidirectional iterators.

So why use iterators? First, they're a flexible way to access the data in containers that don't have obvious means of accessing all of the data (for instance, maps [to be discussed later]). They're also quite flexible -- if you change the underlying container, it's easy to change the associated iterator so long as you only use features associated with the iterator supported by both classes. Finally, the STL algorithms defined in <algorithm> (to be discussed later) use iterators.

Summary

The Good
  • The STL provides iterators as a convenient abstraction for accessing many different types of containers.
  • Iterators for templated classes are generated inside the class scope with the syntax
    class_name<parameters>::iterator
    
  • Iterators can be thought of as limited pointers (or, in the case of random access iterators, as nearly equivalent to pointers)
The Gotchas
  • Iterators do not provide bounds checking; it is possible to overstep the bounds of a container, resulting in segmentation faults
  • Different containers support different iterators, so it is not always possible to change the underlying container type without making changes to your code
  • Iterators can be invalidated if the underlying container (the container being iterated over) is changed significantly
posted on 2006-08-13 19:04 keyws 閱讀(556) 評論(0)  編輯 收藏 引用 所屬分類: STL

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美激情国产日韩| 午夜精品美女久久久久av福利| 国产乱码精品一区二区三区不卡| 亚洲一级片在线观看| 9l国产精品久久久久麻豆| 欧美日本精品| 欧美先锋影音| 国产精品一区二区你懂得 | 在线播放视频一区| 国产亚洲精品激情久久| 国产亚洲观看| 亚洲日本aⅴ片在线观看香蕉| 亚洲第一二三四五区| 亚洲最新中文字幕| 欧美亚洲日本国产| 亚洲第一综合天堂另类专| 久久夜色精品| 99www免费人成精品| 亚洲在线观看视频网站| 欧美资源在线| 欧美成年人视频网站| 欧美激情一区二区三区全黄| 亚洲国产综合视频在线观看| 亚洲裸体视频| 麻豆精品一区二区综合av| 国产精品久久久久永久免费观看| 国内揄拍国内精品久久| 亚洲天堂成人在线观看| 欧美国产日韩亚洲一区| 久久这里有精品15一区二区三区| 国产精品美女久久久久久免费| **欧美日韩vr在线| 欧美成人免费小视频| 亚洲一区二区精品| 国产精品美女主播在线观看纯欲| 一本色道久久88亚洲综合88| 欧美国产一区视频在线观看| 欧美亚洲一区二区在线观看| 国产精品嫩草影院一区二区| 99国产精品99久久久久久| 亚洲国产精品美女| 欧美精品在线观看| 亚洲图片欧洲图片av| 亚洲伦理久久| 欧美日韩免费在线视频| 一区二区三区蜜桃网| 亚洲精品专区| 国产精品美女| 美女网站久久| 欧美精品自拍| 久久精品国产久精国产思思| 久久久国产精品一区| 夜夜精品视频一区二区| 中文一区字幕| 亚洲国产电影| 亚洲一区二区免费在线| 午夜亚洲性色视频| 亚洲国产高清aⅴ视频| 99re成人精品视频| 国产亚洲成av人片在线观看桃| 久久成人免费网| 开心色5月久久精品| 午夜精品亚洲一区二区三区嫩草| 久久国产黑丝| 香蕉久久夜色精品国产| 蜜臀av性久久久久蜜臀aⅴ四虎| 亚洲一区在线免费| 日韩亚洲不卡在线| 久久露脸国产精品| 欧美中文在线免费| 国产精品日韩精品欧美精品| 亚洲国产裸拍裸体视频在线观看乱了中文| 欧美日韩中文在线观看| 欧美成人精品激情在线观看| 国产欧美精品一区二区色综合| 亚洲国产专区校园欧美| 亚洲精品少妇网址| 欧美成人国产va精品日本一级| 麻豆成人精品| 亚洲国产影院| 欧美日韩一区精品| 亚洲一区自拍| 蜜臀av在线播放一区二区三区 | 欧美伊人影院| 国产精品v片在线观看不卡| 亚洲欧美卡通另类91av| 国产精品欧美久久| 欧美国产日韩一二三区| 欧美一进一出视频| 亚洲麻豆av| 亚洲永久精品国产| 国产精品mm| 欧美一级黄色录像| 欧美午夜精品久久久久久人妖| 中文一区二区在线观看| 久久在线免费观看| 91久久精品国产91久久性色tv| 欧美激情一区二区三区高清视频 | 999在线观看精品免费不卡网站| 欧美国产日韩在线| 午夜亚洲性色福利视频| 亚洲一区久久久| 久久乐国产精品| 欧美在线观看你懂的| 宅男精品导航| 欧美一区二区视频在线| 亚洲精品国产精品国自产观看浪潮 | 亚洲欧美日韩天堂| 夜夜嗨av一区二区三区网页 | 欧美国产日韩在线| 久久国产色av| 久久精品国产精品| 久久久久久综合| 久久激情中文| 久久激情视频| 欧美激情视频在线免费观看 欧美视频免费一 | 亚洲一区在线播放| 亚洲一区二区三区精品在线| 韩国欧美一区| 亚洲午夜精品视频| 亚洲欧美在线观看| 久久视频在线视频| 欧美大片免费观看在线观看网站推荐| 欧美成人按摩| 日韩亚洲不卡在线| 久久激五月天综合精品| 久久综合色播五月| 国产精品你懂的| 一区二区在线视频播放| 亚洲最新中文字幕| 亚洲第一黄网| 欧美一区二区黄色| 欧美精品日韩| 亚洲麻豆av| 久久久www免费人成黑人精品 | 99精品国产99久久久久久福利| 中国成人黄色视屏| 久久久久国产精品一区| 欧美国产欧美亚州国产日韩mv天天看完整| 欧美一区二区啪啪| 欧美激情欧美激情在线五月| 国产欧美一区二区精品性色| 亚洲欧洲免费视频| 日韩图片一区| 欧美成人免费在线观看| 西瓜成人精品人成网站| 欧美精品999| 亚洲美女中出| 免费看av成人| 免费成人美女女| 亚洲欧洲在线一区| 欧美大片网址| 欧美成人精品| 日韩图片一区| 亚洲小视频在线| 亚洲国产精品女人久久久| 亚洲成色777777女色窝| 欧美国产日韩在线观看| 欧美日韩国产专区| 免费观看在线综合色| 欧美调教视频| 免费在线观看精品| 欧美日韩人人澡狠狠躁视频| 欧美与欧洲交xxxx免费观看| 亚洲国产精品久久人人爱蜜臀| 美女图片一区二区| 亚洲毛片在线观看.| 日韩一级在线| 国产精品免费一区二区三区在线观看| 亚洲在线观看视频网站| 亚洲欧美日韩成人高清在线一区| 国产欧美日韩三级| 亚洲国产清纯| 欧美欧美天天天天操| 亚洲字幕一区二区| 亚洲欧美日韩爽爽影院| 亚洲日韩欧美视频一区| 亚洲调教视频在线观看| 国产一区二区三区在线观看精品| 欧美 日韩 国产在线 | 久久综合色影院| 免费欧美网站| 蜜桃久久精品乱码一区二区| 欧美午夜在线观看| 亚洲高清久久| 国语精品一区| 欧美亚洲一级片| 欧美一区二区成人| 欧美视频中文字幕在线| 亚洲人成网站999久久久综合| 国产精品美女午夜av| 日韩视频在线观看| 亚洲午夜久久久| 欧美日韩国产限制| 最新中文字幕亚洲| 亚洲美女少妇无套啪啪呻吟| 久久一区二区视频| 亚洲国产精品一区二区第四页av| 亚洲日本va午夜在线影院| 嫩草影视亚洲|