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

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

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

開(kāi)通VIP
C++STL之迭代器

迭代器除了在STL中遍歷序列對象外,還有其他更多的迭代器被iterator所定義。iterator頭文件定義迭代器的幾個(gè)模板將數據從源傳到目的地。流迭代器(stream iterator)作為指向輸入或輸出流的指針,它們可以用來(lái)在流和任何使用迭代器的源或目的地之間傳輸數據,如算法。插入迭代器(inserter iterator)可以將數據傳輸給一個(gè)基本的序列容器。Iterator頭文件定義了兩個(gè)流迭代器模板,其中istream_iterator<T>用于輸入流,ostream_iterator<T>用于輸出流,T是要從流中提取的或者寫(xiě)入流中的對象類(lèi)型。頭文件還定義了三個(gè)插入模板:inserter<T>、back_inserter<T>和front_inserter<T>,其中T是在其中插入數據的序列容器的類(lèi)型。

輸入流迭代器

創(chuàng )建輸入流迭代器:

istream_iterator<int> input(cin);

此代碼創(chuàng )建了一個(gè)istream_iterator<int>類(lèi)型的迭代器 input,可以指向流中int類(lèi)型的對象。這個(gè)構造函數的實(shí)指定與迭代器相關(guān)的實(shí)際流,因此它是一個(gè)可以從標準輸入流cin中讀取整數的迭代器。默認的istream_iterator<T>構造函數創(chuàng )建一個(gè)end-of-stream迭代器。它等價(jià)于通過(guò)調用容器的end()函數獲得容器的end迭代器。下面代碼說(shuō)明了如何創(chuàng )建cin的end-of-stream迭代器來(lái)補充input迭代器:

istream_iterator<int> inputEnd;

現在有了一對迭代器,定義cin中一個(gè)int類(lèi)型的值的序列,可以用這些迭代器將cin中的值加載到vector<int>中。

vector<int> numbers;cout<<”input numbers , end by zero”<<endl;istream_iterator<int> input(cin),inputEnd;while(input != inputEnd){    numbers.push_back(*input++);}

可以看出,只要不是cin的end-of-stream,就會(huì )繼續執行while輸入數據,那么怎樣才會(huì )產(chǎn)生end-of-stream的條件了,可以輸入Ctrl+Z或輸入一個(gè)無(wú)效的字符(如字母),就會(huì )產(chǎn)生end-of-stream條件。

當然,不只限于只能使用輸入流迭代器作為循環(huán)的控制變量。它們可以向算法傳遞數據,如numeric頭文件中的accumulate()。

cout<<”input numbers separated by spaces and a letter to end”<<endl;istream_iterator<int> input(cin),inputEnd;cout<<”the sum of you inputed numbers is<<accumulate(input,inputEnd,0)<<endl;

sstream頭文件定義basic_istringstream<T>類(lèi)型,這個(gè)類(lèi)型定義可以訪(fǎng)問(wèn)流緩沖區中的數據的對象類(lèi)型,如string對象。這個(gè)頭文件還將類(lèi)型istringstream定義為basic_istringstream<char>,它將是char類(lèi)型的字符流??梢詮膕tring對象中構造一個(gè)istringstream對象,這意味著(zhù)從string對象中讀取數據,就像從cin中讀取數據一樣。因為istringstream對象是一個(gè)流,所以可將它傳遞給一個(gè)輸入迭代器構造函數,并用該迭代器訪(fǎng)問(wèn)底層流緩沖區中的數據。如:

string data[] = {“1.2 12.6 3.6 98 5.3 7.1”};Istringstream input(data);Istream_iterator<double> begin(input),end;cout<<”the sum of you inputed numbers is<<accumulate(begin,end,0.0)<<endl;

由于從string對象data中創(chuàng )建istringstream對象,因此可以像流一樣從data中讀取數據。此處accumulate()的第三個(gè)參數應為0.0,因為第三個(gè)參數確定結果的類(lèi)型,此處保證返回double類(lèi)型的值。

實(shí)例:

#include <iostream>#include <iomanip>#include <string>#include <map>#include <iterator>using std::cout;using std::cin;using std::endl;using std::string;int main() {    std::map<string, int> words;           // Map to store words and word counts    cout << "Enter some text and press Enter followed by Ctrl+Z then Enter to end:"        << endl << endl;    std::istream_iterator<string> begin(cin); // Stream iterator     std::istream_iterator<string> end;        // End stream iterator    while(begin != end )                      // Iterate over words in the stream         words[*begin++]++;                      // Increment and store a word count    // Output the words and their counts    cout << endl << "Here are the word counts for the text you entered:" << endl;    for(auto iter = words.begin() ; iter != words.end() ; ++iter)         cout << std::setw(5) << iter->second << " " << iter->first << endl;    return 0;}

插入迭代器

插入迭代器(inserter iterator)是一個(gè)可以訪(fǎng)問(wèn)序列容器vector<T>、deque<T>和list<T>添加新元素的迭代器。有3個(gè)創(chuàng )建插入迭代器的模板:

Back_insert_iterator<T>在類(lèi)型T的容器末尾插入元素。容器必須提供push_back()函數。

Front_insert_iterator<T>在類(lèi)型T的容器開(kāi)頭插入元素。同樣push_front()對容易可用。

Insert_iterator<T>在類(lèi)型T的容器內從指定位置開(kāi)始插入元素。這要求容器有一個(gè)insert()函數,此函數接受兩個(gè)參數,迭代器作為第一個(gè)實(shí)參,要插入的項作為第二個(gè)實(shí)參。

前兩個(gè)插入迭代器類(lèi)型的構造函數接受一個(gè)指定要在其中插入元素的容器的實(shí)參。如:

list<int> numbers;front_insert_iterator<list<int> > iter(numbers);

向容器中插入值:

*iter = 99;

也可以將front_inserter()函數用于numbers容器:

front_inserter(numbers) = 99;

這幾行代碼為numbers列表創(chuàng )建了一個(gè)前段插入器,并用它在開(kāi)頭插入99。front_inserter()函數的實(shí)參是運用迭代器的容器。

 

insert_iterator<T>迭代器的構造函數需要兩個(gè)實(shí)參:

insert_iterator<vector<int> > iter(numbers,numbers.begin());

該構造函數的第二個(gè)實(shí)參是一個(gè)指定在何處插入數據的迭代器。向此容器賦值:

for (int i = 0; i < 100; i++)    *iter = i + 1;

代碼執行后,前100個(gè)元素的值依次為100,99,…,1。

 

輸出流迭代器

為了補充輸入流迭代器模板,ostream_iterator<T>模板提供了向輸出流寫(xiě)類(lèi)型T的對象的輸出流迭代器。

ostream_iterator<int> out(cout);

該模板的實(shí)參int指定要處理的數據類(lèi)型,構造函數實(shí)參cout指定將作為數據的目的地的流,以便cout迭代器能將int類(lèi)型的值寫(xiě)到標準輸出流中。如:

int data [] = {1,2,3,4,5,6,7,8,9};vector<int> numbers(data,data+9);copy(numbers.begin(),numbers.end(),out);

在algorithm頭文件中定義的copy()算法將由前兩個(gè)迭代器實(shí)參指定的對象序列復制到第三個(gè)實(shí)參指定的輸出迭代器。此代碼執行結果為:123456789.

但現在寫(xiě)到標準輸出流中的值沒(méi)有空格。第二個(gè)輸出流迭代器構造函數能解決這一點(diǎn):

ostream_iterator<int> out(cout,” ”);

現在將輸出1 2 3 4 5 6 7 8 9

實(shí)例:

#include <iostream>#include <numeric>#include <vector>#include <iterator>using std::cout;using std::cin;using std::endl;using std::vector;using std::istream_iterator;using std::ostream_iterator;using std::back_inserter;using std::accumulate;int main(){    vector<int> numbers;    cout << "Enter a series of integers separated by spaces"         << " followed by Ctrl+Z or a letter:" << endl;    istream_iterator<int> input(cin), input_end;    ostream_iterator<int> out(cout, " ");    copy(input, input_end, back_inserter<vector<int>>(numbers));    cout << "You entered the following values:" << endl;    copy(numbers.begin(), numbers.end(), out);    cout << endl << "The sum of these values is "         << accumulate(numbers.begin(), numbers.end(), 0) << endl;    return 0;}
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
詳細解說(shuō)STL string | Allocators (STL)
71、STL迭代器技術(shù)
Vector 容器
map3
第 11 章
C++ STL編程輕松入門(mén)05
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

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