請教C 高手,關於「以定義了《運算的自定義類來初始化ostream iterator」的問題

2022-09-27 07:50:32 字數 2699 閱讀 8184

1樓:匿名使用者

lz你可能理解錯了。並不是任何定義了《預算的型別都可以做為引數來初始化ostream_iterator(輸出流迭代器)。應該是隻有basic_ostream和繼承了這個類的類才能當作引數來初始化ostream_iterator。

可能因為以上型別基本都過載了<<,所以lz才搞錯了吧。

最常用的basic_ostream型別的變數其實就是cout

以下就是msdn中的範例

// ostream_iterator_ostream_iterator.cpp

// compile with: /ehsc

#include

#include

#include

int main( )

// write elements to standard output stream

cout << "elements output without delimiter: ";

copy ( vec.begin ( ), vec.end ( ),

ostream_iterator( cout ) );

cout << endl;

// write elements with delimiter " : " to output stream

cout << "elements output with delimiter: ";

copy ( vec.begin ( ), vec.end ( ),

ostream_iterator( cout, " : " ) );

cout << endl;

}這是輸出

1020

elements output without delimiter: 123456

elements output with delimiter: 1 : 2 : 3 : 4 : 5 : 6 :

同樣任何一個ofstream或支援寫的fstream型別也能當ostream_iterator的引數。

比如將上面的範例稍加修改:

// ostream_iterator_ostream_iterator.cpp

// compile with: /ehsc

#include

#include

#include

int main( )

// write elements to standard output stream

file << "elements output without delimiter: ";

copy ( vec.begin ( ), vec.end ( ),

ostream_iterator( file ) );

file << endl;

// write elements with delimiter " : " to output stream

file << "elements output with delimiter: ";

copy ( vec.begin ( ), vec.end ( ),

ostream_iterator( file, " : " ) );

file << endl;

}執行後會生成一個test.txt檔案。內容為:

1020

elements output without delimiter: 123456

elements output with delimiter: 1 : 2 : 3 : 4 : 5 : 6 :

2樓:匿名使用者

"流迭代器都是類别範本:任何已定義輸入操作符(>> 操作符)的型別都可以

定義 istream_iterator。類似地,任何已定義輸出操作符(<< 操作符)的型別

也可定義 ostream_iterator。"

應該理解為 作為 i/ostream_iterator模板 的 型別引數

過載的 《操作符 也是對 相應的流和自定義型別 的過載你的ctest就是ostream& operator<<(ostream&os,const ctest&t);

ostream_iteratoroiit(cout);

c++中ostream怎麼用,是個型別嗎?

3樓:千鋒教育

#include是標準的c++標頭檔案,任何符合標準的c++開發環境都有這個標頭檔案。

在舊的標準c++中,使用#include

但在新標準中,用#include,而且在vc中程式設計的同時要注意要新增:

using namespace std;

在c++中,輸入輸出流被定義為類。c++的i/o庫中的類稱為流類(stream class)。用流類定義的物件稱為流物件。

4樓:匿名使用者

是一個模板類,算是個型別

cout 就是這個型別的

寫檔案的那個檔案流也是這個型別的子類

請教一道c++類定義的練習題

5樓:匿名使用者

把源**發上來讓大家看看

6樓:匿名使用者

沒圖,不知道什麼意思。

#include

using namespace std;

class str

;str *str;

void main()這樣?

C 類中如何自定義25位的長整型

bignum模版,你看下吧 include include include using namespace std const int maxn 9999 const int dlen 4 const int digit 500 數字在該類中每四位逆序存放。實現了加減乘除著四個最基本的運算。乘法的時...

下列識別符號中,不合法的C語言使用者自定義識別符號是

c語言題目 下列識別符號中,不合法的c語言使用者自定義識別符號是 a printf b enum along114050 lv8 c語言題目。下列識別符號中,不合法的c語言使用者自定義識別符號是 a printf b enum c d sin 答案選什麼?個人覺得是不是庫函式麼?補充 關鍵字是什麼?...

C語言中,自定義的兩個標頭檔案裡同事包含了同樣的巨集定義和同樣的

ifdef maxsize else define maxsize typedef struct tdate list endif ifdef maxsize else define maxsize typedef struct tdate list endif include include ht...