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

我輩豈是蓬蒿人!

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>
            影音先锋中文字幕一区| 在线成人激情| 亚洲欧美视频在线观看| 亚洲精品视频免费在线观看| 99国产精品久久久久久久| 美女被久久久| 亚洲精品一区二区三区婷婷月| 狠狠入ady亚洲精品经典电影| 狠狠色噜噜狠狠色综合久| 国产一区二区三区久久精品| 黄色一区二区三区| 亚洲激情精品| 亚洲欧美中文在线视频| 美女啪啪无遮挡免费久久网站| 亚洲高清资源综合久久精品| 亚洲精品一二| 性欧美videos另类喷潮| 快播亚洲色图| 国产免费亚洲高清| 亚洲国产精品女人久久久| 亚洲午夜免费福利视频| 久久九九国产精品怡红院| 亚洲国产日韩欧美在线图片| 亚洲午夜在线视频| 久久精品视频在线免费观看| 欧美精品情趣视频| 国产一区二区三区直播精品电影| 免费91麻豆精品国产自产在线观看| 欧美第一黄色网| 一本色道久久99精品综合| 久久精品盗摄| 欧美日韩不卡视频| 在线观看欧美日韩| 亚洲欧美另类在线| 91久久极品少妇xxxxⅹ软件| 亚洲欧美日韩一区二区三区在线观看| 蜜桃av一区二区三区| 国产日韩一区二区三区在线| 一区二区三区四区五区精品视频| 久久中文在线| 亚洲欧美一区二区三区久久| 欧美成人在线免费观看| 黄色成人av网| 久久久久久精| 亚洲欧美视频在线观看| 国产精品久久久久久久浪潮网站 | 蜜桃av一区二区| 99国内精品久久久久久久软件| 免费日韩成人| 在线观看国产精品网站| 久久综合成人精品亚洲另类欧美| 亚洲专区在线视频| 国产精品二区在线| 在线一区欧美| 亚洲精品欧美日韩| 欧美寡妇偷汉性猛交| 亚洲大胆人体视频| 免费在线看一区| 国产精品v片在线观看不卡| 国产女主播在线一区二区| 久久精彩视频| aa级大片欧美三级| 免费在线观看日韩欧美| 欧美一区二区三区久久精品 | 久久久天天操| 午夜精品福利一区二区三区av| 欧美日韩国内自拍| 亚洲一级二级在线| 在线性视频日韩欧美| 欧美日韩亚洲一区二| 在线一区二区三区做爰视频网站 | 一本到高清视频免费精品| 欧美日韩亚洲一区在线观看| 亚洲欧美日韩高清| 欧美成人影音| 最新国产の精品合集bt伙计| 久久久久9999亚洲精品| 亚洲欧美一区二区三区久久| 一区二区三区高清| 国产婷婷97碰碰久久人人蜜臀| 久久精品天堂| 快播亚洲色图| 亚洲午夜国产一区99re久久| 亚洲午夜av在线| 国产亚洲一本大道中文在线| 欧美.日韩.国产.一区.二区| 欧美日韩91| 久久99伊人| 欧美精品123区| 久久丁香综合五月国产三级网站| 久久夜色精品亚洲噜噜国产mv| 亚洲理伦在线| 亚欧成人精品| 日韩视频免费观看高清完整版| 一区二区三区黄色| 悠悠资源网久久精品| 野花国产精品入口| 在线成人h网| 中国女人久久久| 亚洲日本欧美| 欧美呦呦网站| 中文无字幕一区二区三区| 欧美一区二区三区在| av成人免费| 久久这里有精品视频| 亚洲欧美区自拍先锋| 欧美1区视频| 久久久久久久性| 国产精品h在线观看| 欧美国产精品va在线观看| 国产日韩在线播放| 亚洲无限乱码一二三四麻| 亚洲美女在线观看| 欧美aa国产视频| 你懂的一区二区| 狠狠干狠狠久久| 亚洲综合视频网| 亚洲一区欧美| 欧美日韩性生活视频| 亚洲国产成人久久综合一区| 红桃视频国产一区| 欧美在线观看一区二区| 亚洲欧美伊人| 国产精品入口尤物| 亚洲尤物视频网| 亚洲欧美一区二区原创| 欧美日韩国产成人在线91| 亚洲丁香婷深爱综合| 亚洲欧洲一区二区天堂久久| 久久久久国色av免费看影院| 久久精品五月婷婷| 极品尤物一区二区三区| 久久久国产午夜精品| 免费欧美日韩| 亚洲国产岛国毛片在线| 久久久夜精品| 欧美77777| 亚洲高清在线播放| 老司机亚洲精品| 欧美激情中文字幕乱码免费| 欧美一区二区三区视频免费播放| 黄色成人在线免费| 欧美一区二区在线观看| 久久九九国产| 黄网动漫久久久| 久久夜色精品国产亚洲aⅴ| 免费黄网站欧美| 亚洲日本在线观看| 欧美日韩另类丝袜其他| 亚洲一区欧美| 蜜桃av久久久亚洲精品| 日韩视频免费看| 国产伦理精品不卡| 久久亚洲电影| 亚洲欧洲综合另类| 在线视频你懂得一区| 国产精品欧美日韩一区| 欧美在线观看网站| 亚洲国产欧美国产综合一区| 亚洲一区二区精品视频| 国产亚洲欧美一区在线观看 | 一区二区三区四区五区视频| 亚洲欧美精品一区| 影音欧美亚洲| 欧美三区在线视频| 久久久成人网| 亚洲美女淫视频| 久久狠狠久久综合桃花| 亚洲国产精品视频一区| 欧美视频在线看| 久久精品国产亚洲5555| 亚洲日本欧美日韩高观看| 久久成人精品电影| 日韩小视频在线观看专区| 国产精品一二一区| 嫩草成人www欧美| 欧美一级视频一区二区| 亚洲区在线播放| 久久蜜臀精品av| 亚洲一区二区三区在线播放| 在线精品国精品国产尤物884a| 欧美乱妇高清无乱码| 久久久99爱| 亚洲欧美在线观看| 日韩视频精品在线观看| 蜜桃av一区二区| 久久精品亚洲一区二区| 一区二区日韩欧美| 亚洲第一区在线| 国产亚洲人成网站在线观看| 欧美日韩一级视频| 欧美高清视频www夜色资源网| 亚洲在线一区| 亚洲美女色禁图| 亚洲国产天堂久久国产91| 久久久久免费观看| 先锋资源久久| 性色一区二区三区| 午夜精品视频网站| 亚洲一区二区三区激情|