欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
STL 迭代器的使用

STL 迭代器的使用

 

標準模板庫(The Standard Template Library, STL)定義了五種迭代器。下面的圖表畫(huà)出了這幾種:

input output
/ /
forward
|
bidirectional
|
random access

要注意,上面這圖表并不是表明它們之間的繼承關(guān)系:而只是描述了迭代器的種類(lèi)和接口。處于圖表下層的迭代器都是相對于處于圖表上層迭代器的擴張集。例如:forward迭代器不但擁有inputoutput迭代器的所有功能,還擁有更多的功能。

各個(gè)迭代器的功能如下:

迭代器類(lèi)別

說(shuō)明

輸入

從容器中讀取元素。輸入迭代器只能一次讀入一個(gè)元素向前移動(dòng),輸入迭代器只支持一遍算法,同一個(gè)輸入迭代器不能兩遍遍歷一個(gè)序列

輸出

向容器中寫(xiě)入元素。輸出迭代器只能一次一個(gè)元素向前移動(dòng)。輸出迭代器只支持一遍算法,統一輸出迭代器不能兩次遍歷一個(gè)序列

正向

組合輸入迭代器和輸出迭代器的功能,并保留在容器中的位置

雙向

組合正向迭代器和逆向迭代器的功能,支持多遍算法

隨機訪(fǎng)問(wèn)

組合雙向迭代器的功能與直接訪(fǎng)問(wèn)容器中任何元素的功能,即可向前向后跳過(guò)任意個(gè)元素

迭代器的操作:

每種迭代器均可進(jìn)行包括表中前一種迭代器可進(jìn)行的操作。

迭代器操作

說(shuō)明

所有迭代器

p++

后置自增迭代器

++p

前置自增迭代器

輸入迭代器

*p

復引用迭代器,作為右值

p=p1

將一個(gè)迭代器賦給另一個(gè)迭代器

p==p1

比較迭代器的相等性

p!=p1

比較迭代器的不等性

輸出迭代器

*p

復引用迭代器,作為左值

p=p1

將一個(gè)迭代器賦給另一個(gè)迭代器

正向迭代器

提供輸入輸出迭代器的所有功能

雙向迭代器

--p

前置自減迭代器

p--

后置自減迭代器

隨機迭代器

p+=i

將迭代器遞增i

p-=i

將迭代器遞減i

p+i

p位加i位后的迭代器

p-i

p位減i位后的迭代器

p[i]

返回p位元素偏離i位的元素引用

p<p1

如果迭代器p的位置在p1前,返回true,否則返回false

p<=p1

p的位置在p1的前面或同一位置時(shí)返回true,否則返回false

p>p1

如果迭代器p的位置在p1后,返回true,否則返回false

p>=p1

p的位置在p1的后面或同一位置時(shí)返回true,否則返回false

只有順序容器和關(guān)聯(lián)容器支持迭代器遍歷,各容器支持的迭代器的類(lèi)別如下:

容器

支持的迭代器類(lèi)別

說(shuō)明

vector

隨機訪(fǎng)問(wèn)

一種隨機訪(fǎng)問(wèn)的數組類(lèi)型,提供了對數組元素進(jìn)行快速隨機訪(fǎng)問(wèn)以及在序列尾部進(jìn)行快速的插入和刪除操作的功能??梢栽傩枰臅r(shí)候修改其自身的大小

deque

隨機訪(fǎng)問(wèn)

一種隨機訪(fǎng)問(wèn)的數組類(lèi)型,提供了序列兩端快速進(jìn)行插入和刪除操作的功能??梢栽傩枰臅r(shí)候修改其自身的大小

list

雙向

一種不支持隨機訪(fǎng)問(wèn)的數組類(lèi)型,插入和刪除所花費的時(shí)間是固定的,與位置無(wú)關(guān)。

set

雙向

一種隨機存取的容器,其關(guān)鍵字和數據元素是同一個(gè)值。所有元素都必須具有惟一值。

multiset

雙向

一種隨機存取的容器,其關(guān)鍵字和數據元素是同一個(gè)值??梢园貜偷脑?。

map

雙向

一種包含成對數值的容器,一個(gè)值是實(shí)際數據值,另一個(gè)是用來(lái)尋找數據的關(guān)鍵字。一個(gè)特定的關(guān)鍵字只能與一個(gè)元素關(guān)聯(lián)。

multimap

雙向

一種包含成對數值的容器,一個(gè)值是實(shí)際數據值,另一個(gè)是用來(lái)尋找數據的關(guān)鍵字。一個(gè)關(guān)鍵字可以與多個(gè)數據元素關(guān)聯(lián)。

stack

不支持

適配器容器類(lèi)型,用vector,dequelist對象創(chuàng )建了一個(gè)先進(jìn)后出容器

queue

不支持

適配器容器類(lèi)型,用dequelist對象創(chuàng )建了一個(gè)先進(jìn)先出容器

priority_queue

不支持

適配器容器類(lèi)型,用vectordeque對象創(chuàng )建了一個(gè)排序隊列

下面列舉了些例子說(shuō)明各個(gè)容器的用法:
1、vector

#include <iostream>
#include
<vector>

int main()
{
std::vector
<char>
charVector;

int
x;
for
(x
=0
; x
<10; ++
x)
charVector.push_back(
65 +
x);

int
size
=
charVector.size();
for
(x
=0
; x
<size; ++
x)
{
std::vector
<char>
::iterator start
=

charVector.begin();
charVector.erase(start);
std::vector
<char>::iterator iter;
for
(iter
=
charVector.begin();
iter
!=
charVector.end(); iter
++
)
{
std::cout
<< *
iter;
}
std::cout
<<
std::endl;
}

return 0
;
}

2、deque

#include <iostream>
#include
<deque>

int main()
{
std::deque
<char>
charDeque;
int
x;
for
(x
=0
; x
<10; ++
x)
charDeque.push_front(
65 +
x);

int
size
=
charDeque.size();
for
(x
=0
; x
<size; ++
x)
{
std::deque
<char>
::iterator start
=

charDeque.begin();
charDeque.erase(start);
std::deque
<char>::iterator iter;
for
(iter
=
charDeque.begin();
iter
!=
charDeque.end(); iter
++
)
{
std::cout
<< *
iter;
}
std::cout
<<
std::endl;
}

return 0
;
}
3、list
#include <iostream>
#include
<list>

int main()
{
//
Create and populate the list.

int x;
std::list
<char>
charList;
for
(x
=0
; x
<10; ++
x)
charList.push_front(
65 +
x);

//
Display contents of list.

std::cout << "Original list: ";
std::list
<char>
::iterator iter;
for
(iter
=
charList.begin();
iter
!=
charList.end(); iter
++
)
{
std::cout
<< *
iter;
//
char ch = *iter;
//
std::cout << ch;

}
std::cout
<< std::endl;

//
Insert five Xs into the list.

std::list<char>::iterator start = charList.begin();
charList.insert(
++start, 5, 'X'
);

//
Display the result.

std::cout << "Resultant list: ";
for
(iter
=
charList.begin();
iter
!=
charList.end(); iter
++
)
{
std::cout
<< *
iter;
//
char ch = *iter;
//
std::cout << ch;

}

return 0;
}

4、set

#include <iostream>
#include
<set>

int main()
{
//
Create the set object.

std::set<char> charSet;

//
Populate the set with values.

charSet.insert('E');
charSet.insert(
'D'
);
charSet.insert(
'C'
);
charSet.insert(
'B'
);
charSet.insert(
'A'
);

//
Display the contents of the set.

std::cout << "Contents of set: " << std::endl;
std::
set<char>
::iterator iter;
for
(iter
=
charSet.begin(); iter
!=
charSet.end(); iter
++
)
std::cout
<< *iter <<
std::endl;
std::cout
<<
std::endl;

//
Find the D.

iter = charSet.find('D');
if
(iter
==
charSet.end())
std::cout
<< "
Element not found.
"
;
else

std::cout
<< "Element found: " << *iter;

return 0
;
}

5、multiset

#include <iostream>
#include
<set>

int main()
{
//
Create the first set object.

std::multiset<char> charMultiset1;

//
Populate the multiset with values.

charMultiset1.insert('E');
charMultiset1.insert(
'D'
);
charMultiset1.insert(
'C'
);
charMultiset1.insert(
'B'
);
charMultiset1.insert(
'A'
);
charMultiset1.insert(
'B'
);
charMultiset1.insert(
'D'
);

//
Display the contents of the first multiset.

std::cout << "Contents of first multiset: " << std::endl;
std::multiset
<char>
::iterator iter;
for
(iter
=
charMultiset1.begin();
iter
!=
charMultiset1.end(); iter
++
)
std::cout
<< *iter <<
std::endl;
std::cout
<<
std::endl;

//
Create the second multiset object.

std::multiset<char> charMultiset2;

//
Populate the multiset with values.

charMultiset2.insert('J');
charMultiset2.insert(
'I'
);
charMultiset2.insert(
'H'
);
charMultiset2.insert(
'G'
);
charMultiset2.insert(
'F'
);
charMultiset2.insert(
'G'
);
charMultiset2.insert(
'I'
);

//
Display the contents of the second multiset.

std::cout << "Contents of second multiset: "
<< std::endl;
for
(iter
=
charMultiset2.begin();
iter
!=
charMultiset2.end(); iter
++
)
std::cout
<< *iter <<
std::endl;
std::cout
<<
std::endl;

//
Compare the sets.

if (charMultiset1 == charMultiset2)
std::cout
<< "
set1 == set2
"
;
else if
(charMultiset1
<
charMultiset2)
std::cout
<< "
set1 < set2
"
;
else if
(charMultiset1
>
charMultiset2)
std::cout
<< "
set1 > set2
"
;

return 0
;
}

6、map

#include <iostream>
#include
<map>

typedef std::map
<int, char> MYMAP;

int
main()
{
//
Create the first map object.

MYMAP charMap1;

// Populate the first map with values.
charMap1[1] = 'A';
charMap1[
4] = 'D'
;
charMap1[
2] = 'B'
;
charMap1[
5] = 'E'
;
charMap1[
3] = 'C'
;

//
Display the contents of the first map.

std::cout << "Contents of first map: " << std::endl;
MYMAP::iterator iter;
for
(iter
=
charMap1.begin();
iter
!=
charMap1.end(); iter
++
)
{
std::cout
<<
(
*iter).first << "
-->
"
;
std::cout
<<
(
*iter).second <<
std::endl;
}
std::cout
<<
std::endl;

//
Create the second map object.

MYMAP charMap2;

// Populate the first map with values.
charMap2[1] = 'F';
charMap2[
4] = 'I'
;
charMap2[
2] = 'G'
;
charMap2[
5] = 'J'
;
charMap2[
3] = 'H'
;

//
Display the contents of the second map.

std::cout << "Contents of second map: " << std::endl;
for
(iter
=
charMap2.begin();
iter
!=
charMap2.end(); iter
++
)
{
std::cout
<<
(
*iter).first << "
-->
"
;
std::cout
<<
(
*iter).second <<
std::endl;
}
std::cout
<<
std::endl;

//
Compare the maps.

if (charMap1 == charMap2)
std::cout
<< "
map1 == map2
"
;
else if
(charMap1
<
charMap2)
std::cout
<< "
map1 < map2
"
;
else if
(charMap1
>
charMap2)
std::cout
<< "
map1 > map2
"
;

return 0
;
}

7、multimap

#include <iostream>
#include
<map>

typedef std::multimap
<int, char> MYMAP;

int
main()
{
//
Create the first multimap object.

MYMAP charMultimap;

// Populate the multimap with values.
charMultimap.insert(MYMAP::value_type(1,'A'));
charMultimap.insert(MYMAP::value_type(
4,'C'
));
charMultimap.insert(MYMAP::value_type(
2,'B'
));
charMultimap.insert(MYMAP::value_type(
7,'E'
));
charMultimap.insert(MYMAP::value_type(
5,'D'
));
charMultimap.insert(MYMAP::value_type(
3,'B'
));
charMultimap.insert(MYMAP::value_type(
6,'D'
));

//
Display the contents of the first multimap.

std::cout << "Contents of first multimap: " << std::endl;
MYMAP::iterator iter;
for
(iter
=
charMultimap.begin();
iter
!=
charMultimap.end(); iter
++
)
{
std::cout
<<
(
*iter).first << "
-->
"
;
std::cout
<<
(
*iter).second <<
std::endl;
}
std::cout
<<
std::endl;

//
Create the second multimap object.

MYMAP charMultimap2;

// Populate the second multimap with values.
charMultimap2.insert(MYMAP::value_type(1,'C'));
charMultimap2.insert(MYMAP::value_type(
4,'F'
));
charMultimap2.insert(MYMAP::value_type(
2,'D'
));
charMultimap2.insert(MYMAP::value_type(
7,'E'
));
charMultimap2.insert(MYMAP::value_type(
5,'F'
));
charMultimap2.insert(MYMAP::value_type(
3,'E'
));
charMultimap2.insert(MYMAP::value_type(
6,'G'
));

//
Display the contents of the second multimap.

std::cout << "Contents of second multimap: " << std::endl;
for
(iter
=
charMultimap2.begin();
iter
!=
charMultimap2.end(); iter
++
)
{
std::cout
<<
(
*iter).first << "
-->
"
;
std::cout
<<
(
*iter).second <<
std::endl;
}
std::cout
<<
std::endl;

//
Compare the multimaps.

if (charMultimap == charMultimap2)
std::cout
<< "
multimap1 == multimap2
"
;
else if
(charMultimap
<
charMultimap2)
std::cout
<< "
multimap1 < multimap2
"
;
else if
(charMultimap
>
charMultimap2)
std::cout
<< "
multimap1 > multimap2
"
;

return 0
;
}

8、stack

#include <iostream>
#include
<list>
#include
<stack>

int main()
{
std::stack
<int
, std::list
<int> >
intStack;

int
x;
std::cout
<< "
Values pushed onto stack:
"

<< std::endl;
for
(x
=1
; x
<11; ++
x)
{
intStack.push(x
*100
);
std::cout
<<
x
*100 <<
std::endl;
}

std::cout
<< "
Values popped from stack:
"

<< std::endl;
int
size
=
intStack.size();
for
(x
=0
; x
<size; ++
x)
{
std::cout
<<
intStack.top()
<<
std::endl;
intStack.pop();
}

return 0
;
}

9、queue

#include <iostream>
#include
<list>
#include
<queue>

int main()
{
std::queue
<int
, std::list
<int> >
intQueue;

int
x;
std::cout
<< "
Values pushed onto queue:
"

<< std::endl;
for
(x
=1
; x
<11; ++
x)
{
intQueue.push(x
*100
);
std::cout
<<
x
*100 <<
std::endl;
}

std::cout
<< "
Values removed from queue:
"

<< std::endl;
int
size
=
intQueue.size();
for
(x
=0
; x
<size; ++
x)
{
std::cout
<<
intQueue.front()
<<
std::endl;
intQueue.pop();
}

return 0
;
}

10、priority_queue

#include <iostream>
#include
<list>
#include
<queue>

int main()
{
std::priority_queue
<int
, std::vector
<int>,std::greater<int> >
intPQueue;
int
x;
intPQueue.push(
400
);
intPQueue.push(
100
);
intPQueue.push(
500
);
intPQueue.push(
300
);
intPQueue.push(
200
);

std::cout
<< "
Values removed from priority queue:
"

<< std::endl;
int
size
=
intPQueue.size();
for
(x
=0
; x
<size; ++
x)
{
std::cout
<<
intPQueue.top()
<<
std::endl;
intPQueue.pop();
}

return 0
;
}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
STL--迭代器(iterator)使用詳解
UC頭條:C 11之基于范圍的for循環(huán)
C++ find()函數用法(一般用于vector的查找)
STL介紹
class unordered_multimap;
C++ iterotor
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久