在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
头文件 <iostream> #include <iostream> #include <string> int main() { string in_string; // 向用户终端写字符串 std::cout << "Please enter your name: "; // 把用户输入的读取到 in_string 中 std::cin >> in_string; if ( in_string.empty() ) // 产生一个错误消息输出到用户终端 std::cerr << "error: input string is empty!\n"; else std::cout << "hello, " << in_string << "!\n"; }
二. 对文件的操作 #include <fstream> #include <string> #include <vector> #include <algorithm> int main() { string ifile; cout << "Please enter file to sort: "; cin >> ifile; // 构造一个 ifstream 输入文件对象 ifstream infile( ifile.c_str() ); if( ! infile ) { cerr << "error: unable to open input file: " << ifile << endl; return -1; } string ofile = ifile + ".sort"; // 构造一个 ofstream 输出文件对象 ofstream outfile( ofile.c_str() ); if( !outfile ) { cerr << "error: unable to open output file: " << ofile << endl; return -2; } string buffer; vector<string> text; int cnt = 1; while ( infile >> buffer ) { text.push_back( buffer ); cout << buffer << ( cnt++ % 8 ? "" : "\n" ); } sort( text.begin(), text.end() ); // ok: 把排序后的词打印到 outfile vector<string>::iterator iter = text.begin(); for ( cnt = 1; iter != text.end(); ++iter, ++cnt ) outfile << *iter << (cnt%8 ? "" : "\n" ); return 0; }
三. 对字符流操作 #include <sstream> string program_name( "our_program" ); string version( "0.01" ); // ... string mumble( int *array, int size ) { if ( ! array ) { ostringstream out_message; out_message << "error: " << program_name << "--" << version << ": " << __FILE__ << ": " << __LINE__ <<" -- ptr is set to 0; " << " must address some array.\n"; // 返回底层 string 对象 return out_message.str(); }
四. wchar_t型 20.1 输出操作符<< #include <iostream> #include <algorithm> #include <vector> #include <string> string pooh_pals[] = { "Tigger", "Piglet", "Eeyore", "Rabbit" }; int main() { vector<string> ppals( pooh_pals, pooh_pals+4 ); vector<string>::iterator iter = ppals.begin(); vector<string>::iterator iter_end = ppals.end(); cout << "These are Pooh's pals: "; // 把每个元素拷贝到 cout ... ostream_iterator< string > output( cout, "" ); copy( iter, iter_end, output ); cout << endl; }
20.2 输入操作符>> #include <iostream> #include <string> int main() { int item_number; string item_name; double item_price; cout << "Please enter the item_number, item_name, and price: " << endl; cin >> item_number; cin >> item_name; cin >> item_price; cout << "The values entered are: item# " << item_number << "" << item_name << " @$" << item_price << endl; }
#include <algorithm> #include <string> #include <vector> #include <iostream> int main() { istream_iterator< string > in( cin ), eos ; vector< string > text ; // 从标准输入向 text 拷贝值 copy( in , eos , back_inserter( text ) ) ; sort( text.begin() , text.end() ) ; // 删除所有重复的值 vector< string >::iterator it ; it = unique( text.begin() , text.end() ) ; text.erase( it , text.end() ) ; // 显示结果 vector int line_cnt = 1 ; for ( vector< string >::iterator iter = text.begin(); iter != text.end() ; ++iter , ++line_cnt ) cout << *iter << ( line_cnt % 9 ? "" : "\n" ) ; cout << endl; }
20.2.1 字符串输入 20.3 其他输入输出操作符 #include <iostream> int main() { int ch; // 或使用: // while (( ch = cin.get() ) && ch != EOF) while (( ch = cin.get()) != EOF) cout.put(ch); return 0; }
二. ignore( streamsize length = 1, int delim = traits::eof )函数: 属于istream 三. gcount()函数: 属于istream #include <iostream> int main() { const int max_line = 1024; char line[ max_line ]; while ( cin.get( line, max_line )) { // 最大读取数量 max_line - 1, 也可以为 null int get_count = cin.gcount(); cout << "characters actually read: " << get_count << endl; // 处理每一行 // 如果遇到换行符 // 在读下一行之前去掉它 if ( get_count & max_line-1 ) cin.ignore(); } }
四. getline(char *sink, streamsize size, char delimiter='\n')属于istream 五. write( const char *sink, streamsize length )属于ostream 六. read( char* addr, streamsize size )属于istream #include <iostream> int main() { const lineSize = 1024; int lcnt = 0; // 读入多少行 int max = -1; // 最长行的长度 char inBuf[ lineSize ]; // 读取 1024 个字符或者遇到换行符 while (cin.getline( inBuf, lineSize )) { // 实际读入多少字符 int readin = cin.gcount(); // 统计: 行数最长行 ++lcnt; if ( readin > max ) max = readin; cout << "Line #" << lcnt << "\tChars read: " << readin << endl; cout.write( inBuf, readin).put('\n').put('\n'); } cout << "Total lines read: " << lcnt << endl; cout << "Longest line read: " << max << endl; }
七. getline( istream &is, string str, char delimiter ); 八. putback( char c ); 将字符放回 iostream。 九. unget();往回重置下一个 istream 项。 十. peek(); 返回下一个字符或 EOF,但不要提取出来。 char ch, next, lookahead; while ( cin.get( ch )) { switch (ch) { case '/': // 是注释行吗? 用 peek() 看一看: // 是的? ignore() 余下的行 next = cin.peek(); if ( next == '/' ) cin.ignore( lineSize, '\n' ); break; case '>': // 查找 >>= next = cin.peek(); if ( next == '>' ) { lookahead = cin.get(); next = cin.peek(); if ( next != '=' ) cin.putback( lookahead ); }
20.4 重载输出操作符<< 20.5 重载输入操作符>> #include <iostream> #include "WordCount.h" /* 必须修改 WordCount, 指定输入操作符为友元 class WordCount { friend ostream& operator<<( ostream&, const WordCount& ); friend istream& operator>>( istream&, WordCount& ); */ istream& operator>>( istream &is, WordCount &wd ) { /* WordCount 对象被读入的格式: * <2> string * <7,3> <12,36> */ int ch; /* 读入小于符号, 如果不存在 * 则设置 istream 为失败状态并退出 */ if ((ch = is.get()) != '<' ) { is.setstate( ios_base::failbit ); return is; } // 读入多少个 int occurs; is >> occurs; // 取 >; 不检查错误 while ( is && (ch = is.get()) != '>' ); is >> wd._word; // 读入位置 // 每个位置的格式: < line, col > for ( int ix = 0; ix < occurs; ++ix ) { int line, col; // 提取值 while (is && (ch = is.get())!= '<' ); is >> line; while (is && (ch = is.get())!= ',' ); is >> col; while (is && (ch = is.get())!= '>' ); wd.occurList.push_back( Location( line, col )); } return is; } 20.6 文件输入和输出 20.7 条件状态 二. 改变状态 20.8 string 流 20.9 格式状态 20.10 强类型库 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论