ÊäÈë/Êä³öÁ÷
Á÷ÊÇ×Ö·û¼¯ºÏ»òÊý¾ÝÁ÷µÄÔ´»òÄ¿µÄµØ
ÓÐÁ½ÖÖÁ÷
Êä³öÁ÷,ÔÚÀà¿âÀï³ÆΪostream
ÊäÈëÁ÷,ÔÚÀà¿âÀï³ÆΪistream
ͬʱ´¦ÀíÊäÈëºÍÊä³ö,ÓÉistreamºÍostreamÅÉÉúË«ÏòÁ÷iostream.
C++Ãû×Ö | ¶ÔÓ¦É豸 | C¶ÔÓ¦Ãû×Ö | ĬÈÏÉ豸 |
cin | ±ê×¼ÊäÈëÁ÷ | stdin | ¼üÅÌ |
cout | ±ê×¼Êä³öÁ÷ | stdout | ÆÁÄ» |
cerr | ±ê×¼´íÎóÁ÷£¨·Ç»º³å£© | stderr | ÆÁÄ» |
clog | ±ê×¼´íÎóÁ÷£¨»º³å) | stdprn | ´òÓ¡»ú |
1.1.1.1 Êä³öÁ÷£ºcoutÊä³öµ½ÆÁÄ»¶ÔÓ¦ostreamÀà
#include <iostream> #include <stdio.h> using namespace std;
int main() { char *p = "hello world"; int a = 10 ; int *pf = &a; char buf[30] = "djhjshj jkdhwq";
cout << p << endl; cout << pf << endl; //C++Ò²¿ÉÒÔ´òÓ¡µØÖ· printf("%#p\n",pf); cout << buf << endl; } |
ʹÓÃcoutʱ³ýÁËÒÔÉϵĻù±¾²Ù×÷£¬»¹ÓÐÈçϹ¦ÄÜ£º
1£©ÐÞ¸ÄÏÔʾʱʹÓõļÆÊýϵͳ
#include<iostream> #include<string> using namespace std;
int main() { int n,m=123; cout<<"please enter a number:"; cin>>n;
cout<<"n="<<n<<endl; cout<<hex; /*µÈ¼ÛÓÚhex(cout)Íê³ÉÉèÖú󣬺óÃæµÄ³ÌÐò½«ÒÔÊ®Áù½øÖƵĸñʽ´òÓ¡Êý¾Ý£¬Ö±µ½½«¸ñʽ״̬ ÉèÖÃΪÆäËûÑ¡ÏîΪֹ*/ cout<<"n="<<n<<endl; cout<<"m="<<m<<endl;
dec(cout);/*µÈ¼ÛÓÚcout<<decÍê³ÉÉèÖú󣬳ÌÐò½«ÒÔÊ®½øÖƵĸñʽ´òÓ¡Êý¾Ý£¬Ö±µ½½«¸ñʽ״̬ÉèÖÃΪ ÆäËûÑ¡ÏîΪֹ*/ cout<<"n="<<n<<endl; cout<<"m="<<m<<endl;
cout<<oct<<"n="<<n<<endl;/*µÈ¼ÛÓÚcout<<octÍê³ÉÉèÖú󣬳ÌÐò½«Ò԰˽øÖƵĸñʽ´òÓ¡Êý¾Ý£¬Ö±µ½½«¸ñʽ״̬ÉèÖÃΪÆäËûÑ¡ÏîΪֹ*/ cout<<"m="<<m<<endl;
return 0; } |
2£©µ÷Õû×ֶοí¶È
c++ÖУ¬cout.width( )Óësetw( )£¬ÕâÁ½¸öº¯ÊýµÄ×÷ÓÃÊÇÒ»ÑùµÄ¡£¶¼ÊÇ¿ØÖÆÊä³ö×Ö·ûÓò¿í¶ÈµÄ£¬ÇÒ²»×ãÓÿոñÌî³ä¡£ÔÚC++ÖУ¬setw(int n)ÓÃÀ´¿ØÖÆÊä³ö¼ä¸ô
ͨ³£Ê¹ÓÃcoutÊä³öÊý¾ÝµÄʱºò£¬¸÷Áв¢Ã»ÓжÔÆ룬ÕâÊÇÒòΪûÓпØÖÆ×ֶεĿí¶È£»¿ÉÒÔʹÓÃwidth³ÉÔ±º¯Êý½«³¤¶È²»Í¬µÄÊý×ַŵ½¿í¶ÈÏàͬµÄ×Ö¶ÎÖУ¬¸Ã·½·¨ÔÐÍΪ£º
int width(); //·µ»Ø×ֶοí¶ÈµÄµ±Ç°ÉèÖã»
int width(int i); //½«×ֶοí¶ÈÉèÖÃΪi¸ö¿Õ¸ñ£¬²¢·µ»ØÒÔÇ°µÄ×ֶοí¶ÈÖµ
width()·½·¨Ö»Ó°Ï콫ÏÔʾµÄÏÂÒ»¸öÏîÄ¿£¬È»ºó×ֶοí¶È½«»Ö¸´ÎªÄ¬ÈÏÖµ£»
ÓÉÓÚwidth()³ÉÔ±º¯Êý£¬Òò´Ë±ØÐëʹÓöÔÏóÀ´µ÷ÓÃËü£¬µ±È»£¬Èç¹ûËùÉèÖõĿí¶ÈÊÇСÓÚÔÓÐÊý¾ÝµÄ¿í¶È£¬ÄÇôÊý¾Ý»áÔÑùÊä³ö£¬¿í¶È»á×Ô¶¯±ä³¤£»
#include<iostream> #include<string> using namespace std;
int main() { int n=123; cout<<"Êä³öÊý¾Ý£º"<<endl; cout<<n<<endl; cout.width(5); cout<<n<<endl; //ÔÚ֮ǰÉèÖõĿí¶È£¬Ö»Ó°Ïì´ËÐеÄÊä³öÊý¾ÝµÄ¿í¶È£¬Ö®ºó»á»Ö¸´ÎªÄ¬ÈÏÖµ£» cout<<n<<endl; return 0; } |
3£©Ìî³ä×Ö·û
ÔÚĬÈÏÇé¿öÏ£¬coutÓÿոñÌî³ä×Ö¶ÎÖÐδ±»Ê¹ÓõIJ¿·Ö£¬¿ÉÒÔÓÃfill()³ÉÔ±º¯ÊýÀ´¸Ä±äÌî³ä×Ö·û¡£ÀýÈ磺ÏÂÃæµÄº¯Êýµ÷Óý«Ìî³äÌî³ä×Ö·û¸Ä³É¡°*¡±£»
cout.fill(¡®*¡¯);
Óŵ㣺Õâ¶ÔÓÚ¼ì²é´òÓ¡½á¹û£¬·ÀÖ¹½ÓÊÕ·½Ìí¼ÓÊý×ÖºÜÓÐÓã»
×¢Ò⣺Óë×ֶοí¶È²»Í¬µÄÊÇ£¬ÐµÄÌî³ä×Ö·û½«Ò»Ö±ÓÐЧ£¬Ö±µ½¸ü¸ÄËüΪֹ¡£
#include<iostream>
using namespace std;
int main() { int n=123; cout<<"Êä³öÊý¾Ý£º"<<endl; cout.fill('*'); //´Ëʱ½«Ìî³ä×Ö·ûÉèÖÃΪ*£¬ÔÚ²»¶ÔÆä½øÐÐÐ޸ĵÄÇé¿öÏÂÊÇÒ»Ö±ÓÐЧµÄ£» cout.width(7);
cout<<n<<endl; cout.width(7); //widthÐÞÊÎÊý¾ÝÊä³ö¿í¶ÈµÄÖ»Ó°ÏìÀëËü×î½üµÄÊä³öµÄÓï¾äµÄ¿í¶È£¬Ö®ºó»Ö¸´Ä¬ÈÏÖµ
cout<<n<<endl; cout.fill('&'); //´Ëʱ½«Ìî³ä×Ö·ûÉèÖÃΪ*£¬ÔÚ²»¶ÔÆä½øÐÐÐ޸ĵÄÇé¿öÏÂÊÇÒ»Ö±ÓÐЧµÄ£» cout.width(7); cout<<n<<endl; return 0; } |
4£©ÉèÖø¡µãÊýµÄÏÔʾ¾«¶È
¸¡µãÊý¾«¶ÈµÄº¬ÒåÈ¡¾öÓÚÊä³öģʽ¡£ÔÚĬÈÏģʽÏ£¬ËüÖ¸µÄÊÇÏÔʾµÄ×ÜλÊý£¨ÕûÊý²¿·Ö+СÊý²¿·ÖµÄλÊý£¬Ä¬ÈÏģʽÏÂÊä³öµÄÊÇ6λÊý£©¡£ÔÚ¶¨µãģʽºÍ¿ÆѧģʽÏ¡£¾«¶ÈÖ¸µÄÊÇСÊýµãºóÃæµÄλÊý¡£C++µÄĬÈϾ«¶ÈΪ6루µ«Ä©Î²µÄ0½«²»ÏÔʾ£©£»
precision();³ÉÔ±º¯ÊýʹµÃÄܹ»Ñ¡ÔñÆäËûÖµ£»
Àýcout.precision(2);//½«coutµÄ¾«¶ÈÉèÖóÉ2
×¢Ò⣺ºÍwidth()µÄÇé¿ö²»Í¬£¬µ«Óëfill()ÀàËÆ£¬Ðµľ«¶ÈÉèÖý«Ò»Ö±ÓÐЧ£¬Ö±µ½±»ÖØÐÂÉèÖá£
#include<iostream> #include<string> using namespace std;
int main() { float pi=3.1415; float n; n=10/3.0;
cout<<"pi="<<pi<<endl; cout<<"n="<<n<<endl;
cout.precision(5);/*ÉèÖø¡µãÊýµÄÏÔʾ¾«¶È£¬´Ë¾«¶ÈÊÇ°üº¬ÕûÊýÓëСÊýÒ»¹²µÄλÊý£»Èç¹ûÔÓÐ Êý¾ÝµÄ¾«¶ÈСÓÚËùÉèÖõÄÊý¾ÝµÄ¾«¶È£¬ÄÇôÊý¾Ý»áÔÑùÊä³ö£¬Èç¹ûÉèÖõľ«¶È±ÈÕûÊý²¿·Ö»¹ÒªÐ¡£¬ÔòÒÀÈ»»áʹÆä°´ÕÕÐÞÊεľ«¶È½øÐÐÊä³öÊý¾Ý*/ cout<<"pi="<<pi<<endl; cout<<"n="<<n<<endl;
return 0; } |
5£©´òӡĩβµÄ0ºÍСÊýµã
¶ÔÓÚÓÐЩÊä³ö£¨Èç¼Û¸ñ»òÀ¸ÖеÄÊý×Ö£©±£ÁôĩβµÄ0½«¸üΪÃÀ¹Û¡£iostreamϵÁÐÀàÖв¢Ã»ÓÐÌṩרÃÅÓÃÓÚÍê³ÉÕâÏîÈÎÎñµÄº¯Êý£¬µ«ios_baseÀàÌṩÁËÒ»¸ösetf()º¯Êý£¬Äܹ»¿ØÖƶàÖÖ¸ñʽ»¯ÌØÐÔ£¬Õâ¸öÀ໹¶¨ÒåÁ˶à¸ö³£Á¿£¬¿ÉÓÃ×÷¸Ãº¯ÊýµÄ²ÎÊý¡£
Àý£ºcout.setf(ios_base::showpoint);
ʹÓÃĬÈϵĸ¡µã¸ñʽʱ£¬ÉÏÊöÓï¾ä»¹½«µ¼ÖÂĩβµÄ0±»ÏÔʾ³öÀ´¡£Ò²¾ÍÊÇ˵£¬Èç¹ûʹÓÃĬÈϾ«¶Èʱ£¬cout²»»á½«2.00ÏÔʾΪ2£¬¶øÊǽ«ËüÏÔʾΪ2.000000;
showpointÊÇios_baseÀàÉùÃ÷Öж¨ÒåµÄÀ༶¾²Ì¬³£Á¿£¬À༶¾ÍÒâζ×ÅÈç¹ûÔÚ³ÉÔ±º¯Êý¶¨ÒåµÄÍâÃæʹÓÃËü£¬Ôò±ØÐëÔÚ³£Á¿ÃûÇ°Ãæ¼ÓÉÏ×÷ÓÃÓòÏÞ¶¨·û£¨::£©,Òò´Ëios_base::showpointÖ¸µÄÊÇÔÚios_baseÀàÖж¨ÒåµÄÒ»¸ö³£Á¿¡£
˵Ã÷£ºÉèÖø¡µãÊýµÄÏÔʾ¾«¶È¿ÉÒԺʹòӡĩβµÄ0½øÐлìºÏʹÓã¬ÀýÈçÒªÊä³öµÄСÊýµÄλÊý²»×ãĬÈϵľ«¶ÈµÄʱºò£¬´ËʱÔÚ¿ØÖÆÆäÊä³ö¾«¶ÈµÄͬʱ£¬¿ØÖÆÊä³ö0µÄ¸öÊý
#include<iostream> #include<string> using namespace std;
int main() { float pi=3.14; float n; n=10/3.0; //ÉèÖÃÊä³öСÊýµãÒÔ¼°Ä©Î²µÄ0£¬ËãÉÏÕûÊýλһ¹²Áùλ cout.setf(ios_base::showpoint); cout.precision(4); cout<<"pi="<<pi<<endl; //Êä³ö3.140 cout<<"n="<<n<<endl;
return 0; } |
6£©Í·Îļþiomanip
ʹÓÃiostream¹¤¾ßÀ´ÉèÖÃһЩ¸ñʽֵ£¨Èç×ֶοí¶È£©²»Ì«·½±ã£¬Îª¼ò»¯¹¤×÷£¬C++ÔÚÍ·ÎļþiomanipÖÐÌṩÁËÆäËûһЩ¿ØÖÆ·û£¬ËûÃÇÄܹ»Ìṩ֮ǰËùÓõ½µÄ·þÎñ£¬µ«±íʾÆðÀ´¸ü·½±ã¡£3¸ö×î³£ÓõĿØÖÆ·û·Ö±ðΪsetprecision(),setfill()ºÍsetw(),ËüÃÇ·Ö±ðÓÃÀ´ÉèÖþ«¶È(ÓÃÓÚÉèÖÃСÊýÊä³öµÄÓÐЧλÊý)£¬Ìî³ä×Ö·ûºÍÐÞÊÎ×ֶοí¶È¡£Óë֮ǰ²»Í¬µÄÊÇ£¬ÕâÈý¸ö¿ØÖÆ·ûÊÇ´ø²ÎÊýµÄ¡£²¢ÇÒ´Ë¿ØÖÆ·û¿ÉÒÔÓëcoutÓï¾äÁ¬½ÓÆðÀ´¡£ÕâÑùʹÓúܷ½±ã¡£
#include <iostream> #include <cmath> #include <iomanip>
using namespace std;
int main() { cout << fixed <<right;//ʹÓÃfixedµÄ¿ØÖÆ·ûºÅÏÔʾĩβµÄ0£¬rightÓÒ¶ÔÆë
cout <<setw(6)<<"N"<<setw(14)<<"square root"<<setw(15)<<"fourth root"<<endl;
double root;
for(int i= 10; i <= 100; i+=10) { root = sqrt(double(i)); //¶Ôi½øÐпª¸ùºÅ cout<<setw(6)<<setfill('.')<<i; //¿ØÖƱäÁ¿iµÄ¿í¶ÈΪ6£¬Èç¹û²»×ãÓÃ.Ìî³ä cout<<setfill(' ')<<setw(12)<<setprecision(3)<<root; /*¿ØÖƱäÁ¿rootµÄÊä³ö¿í¶ÈΪ12²¢ÐÞÊÎÆäÊä³ö¾«¶ÈΪ3λÓÐЧСÊýλÊý£¬²»×ãÓÿոñÌî³ä*/ cout<<setw(14)<<setprecision(4)<<sqrt(root)<<endl; /*ÉèÖñäÁ¿root¸ùºÅºóµÄÖµÊä³ö¿í¶ÈΪ14,²»×ãÒÀÈ»ÓÿոñÌî³ä£¬ÇÒÊä³öСÊýÓÐЧλÊýΪ4λ*/ } } |
1.1.1.2 ÊäÈëÁ÷-----cin
1£©cin
Ó÷¨1:×î»ù±¾£¬Ò²ÊÇ×î³£ÓõÄÓ÷¨£¬ÊäÈëÊý×Ö
#include <iostream> using namespace std; int main () { int a,b; cout<<"ÊäÈëÊý¾Ý£º"; cin>>a>>b; //ÊäÈë2¸öÕûÐÍÊý¾Ý cout<<"a="<<a<<" b="<<b<<endl; cout<<"a+b="<<a+b<<endl; return 0; } |
ÏÖÏó£º
ÊäÈ룺2 3
Êä³ö£ºa=2 b=3
a+b=5
Ó÷¨2£º½ÓÊÜÒ»¸ö×Ö·û´®£¬°´¡°»Ø³µ¼ü¡±½áÊø×Ö·û´®µÄÊäÈ룬
˵Ã÷£º²¢ÇÒÖ»ÄÜÊäÈëÒ»¸öµ¥´Ê£¬Ò»µ©Óöµ½¿Õ¸ñ»òÕßtab¼üÔòÍ£Ö¹»ñÈ¡ºóÃæµÄ×Ö·û´®£¬Èç¹ûÊǸտªÊ¼ÔÚÊäÈëµÄʱºò£¬Ê×ÏÈÊäÈëtab¼ü»òÕß¿Õ¸ñ¼ü£¬Ôò»á»ñÈ¡¸Ã×Ö·û¼üºóÃæµÄÒ»¸öµ¥´Ê¡£
#include <iostream> using namespace std; int main ( ) { char a[20]; cout<<"ÊäÈë×Ö·û´®£º"; cin>>a; cout<<a<<endl; return 0; } |
2£©cin.get()
Ó÷¨1£º cin.get(×Ö·û±äÁ¿Ãû)¿ÉÒÔÓÃÀ´½ÓÊÕ×Ö·û£¬»òÕß×Ö·û±äÁ¿Ãû=cin.get()
˵Ã÷£ºÔÚ½øÐÐÊý¾ÝµÄÊäÈë¹ý³ÌÖУ¬ÒÀÈ»´æÔÚÊäÈëÊä³ö»º³åÇø£¬ÔÚʹÓÃcin.getÊäÈë×Ö·ûµÄʱºò£¬»áʶ±ð»Ø³µ¼ü£¬ËùÒÔµ±Ê¹ÓÃcin.getµÄʱºò£¬Òª×¢ÒâʹÆä²»±»¶Áµ½»Ø³µ×Ö·û
#include <iostream>
using namespace std;
int main() { char ch; char data;
cout << "ÇëÊäÈëÒ»¸ö×Ö·û£º"; ch = cin.get(); //»òÕßʹÓÃcin.get(ch); cout << "ch = " << ch << endl;
cin.get(); //½ÓÊջسµ×Ö·û²»»ØÏÔ cout << "ÇëÊäÈëÒ»¸ö×Ö·û£º"; cin.get(data); //»òÕßʹÓÃdata = cin.get(); cout << "data = " << data << endl; } |
Ó÷¨2£ºcin.get(×Ö·ûÊý×éÃû,½ÓÊÕ×Ö·ûÊýÄ¿)ÓÃÀ´½ÓÊÕÒ»ÐÐ×Ö·û´®,¿ÉÒÔ½ÓÊÕ¿Õ¸ñ
#include <iostream>
using namespace std;
int main() { char ch[30];
cout << "ÇëÊäÈëÒ»¸ö×Ö·û´®£º"; cin.get(ch,15); //×Ö·û´®ÖÐĩβµÄ'\0'Ò²ËãÕ¼Ò»¸ö×Ö·û£¬ //Ò²¾ÍÊÇ˵ʵ¼ÊÊäÈëµÄ×Ö·ûÐèÒª±È×Ö·ûÊýÄ¿ÉÙÒ»¸ö£¬×Ö·û'\0'Ò²Õ¼Ò»¸ö×Ö·ûÊýÄ¿ cout << "ch = " << ch << endl; //ʵ¼ÊÉÏÊä³öµÄÊÇ14¸ö×Ö·û } |
Ó÷¨3£ºcin.get(ÎÞ²ÎÊý)ûÓвÎÊýÖ÷ÒªÊÇÓÃÓÚÉáÆúÊäÈëÁ÷ÖеIJ»ÐèÒªµÄ×Ö·û,»òÕßÉáÆú»Ø³µ,ÃÖ²¹cin.get(×Ö·ûÊý×éÃû,½ÓÊÕ×Ö·ûÊýÄ¿)µÄ²»×ã.
Ó÷¨4£ºcin.getline(×Ö·ûÊý×éÃû,×Ö·û¸öÊý£¬½áÊø×Ö·û) // ½ÓÊÜÒ»¸ö×Ö·û´®£¬¿ÉÒÔ½ÓÊÕ¿Õ¸ñ²¢Êä³ö
cin.getline(char *str,int n, char ch);
º¯Êý˵Ã÷£º±íʾ½«½ÓÊÕµÄ×Ö·û´®´æ´¢ÓÚstrËùÖ¸ÏòµÄ¿Õ¼äÖУ¬²¢½ÓÊÕµÄ×Ö·û¸öÊýΪn£¬ÓÃch×Ö·û×÷Ϊ½áÊø×Ö·û£¬
×¢Ò⣺1£©µ±µÚÈý¸ö²ÎÊýÊ¡ÂÔµÄʱºò£¬ÔòϵͳĬÈÏÓá¯\0¡¯×÷Ϊ½áÊø·û
2£©Êµ¼ÊÉÏÔÚÊäÈëµÄ¹ý³ÌÖÐÖ»ÊäÈëÁËn-1¸ö×Ö·û£¬ÒòΪ½áÊø·ûÒ²Õ¼ÁËÒ»¸ö×Ö·û£¬ÔÚÊä³öµÄʱºò£¬½áÊø×Ö·û²»»áÏÔʾ³öÀ´£¬×Ö·û¸öÊýºÍ½áÊø×Ö·û¶¼»á¿ØÖÆ×Ö·ûµÄÊäÈë
3£©ÔÚ½øÐÐÊäÈëµÄʱºòÈç¹û´ïµ½ÁË×Ö·û¸öÊý£¬»òÕßÔËÐе½Á˽áÊø×Ö·û£¬Ôòº¯Êý½áÊø
4£©Èç¹ûÒªÊäÈëµÄ×Ö·ûµÄ¸öÊýСÓÚ½áÊø×Ö·ûÖÐ×Ö·ûÊäÈëµÄ¸öÊý£¬Ôò°´ÕÕÊäÈë×Ö·ûµÄ¸öÊýn¾ö¶¨
5£©Èç¹û½áÊø×Ö·ûÇ°ÊäÈëµÄ×Ö·ûµÄ¸öÊýСÓÚ¿ØÖÆ×Ö·ûÊýÄ¿n£¬Ôò°´ÕÕ½áÊø×Ö·û¾ö¶¨×Ö·ûµÄÊäÈë
Èç¹û½«Àý×ÓÖÐcin.getline()¸ÄΪcin.getline(m,5,'a');µ±ÊäÈëjlkjkljklʱÊä³öjklj£¬ÊäÈëjkaljkljklʱ£¬ Êä³öjk
6£©µ±ÓÃÔÚ¶àάÊý×éÖеÄʱºò£¬Ò²¿ÉÒÔÓÃcin.getline(m[i],20)Ö®ÀàµÄÓ÷¨£º
#include <iostream>
using namespace std;
int main() { char ch[30];
cout << "ÇëÊäÈëÒ»¸ö×Ö·û´®£º"; cin.get(ch,15); //×Ö·û´®ÖÐĩβµÄ'\0'Ò²ËãÕ¼Ò»¸ö×Ö·û£¬ //Ò²¾ÍÊÇ˵ʵ¼ÊÊäÈëµÄ×Ö·ûÐèÒª±È×Ö·ûÊýÄ¿ÉÙÒ»¸ö£¬×Ö·û'\0'Ò²Õ¼Ò»¸ö×Ö·ûÊýÄ¿ cout << "ch = " << ch << endl; //ʵ¼ÊÉÏÊä³öµÄÊÇ14¸ö×Ö·û } |
3£©getline() //½ÓÊÜÒ»¸ö×Ö·û´®£¬¿ÉÒÔ½ÓÊÕ¿Õ¸ñ²¢Êä³ö£¬Ðè°üº¬¡°#include<string>¡±
#include <iostream> #include <string>
using namespace std; int main() { string ch; cout << "ÇëÊäÈëÒ»¸ö×Ö·û´®£º"; getline(cin,ch); //±ê×¼ÊäÈëÁ÷£¬ÊäÈëÒ»ÐÐ×Ö·û´®´æ´¢ÓÚ×Ö·û´®chÖÐ cout << ch << endl; } |
4£©cin.ignore()
º¯Êý×÷ÓãºÊÇÌø¹ýÊäÈëÁ÷ÖÐn¸ö×Ö·û£¬»òÔÚÓöµ½Ö¸¶¨µÄÖÕÖ¹×Ö·ûʱÌáÇ°½áÊø£¨´ËʱÌø¹ý°üÀ¨ÖÕÖ¹×Ö·ûÔÚÄÚµÄÈô¸É×Ö·û£©¡£
¹¦ÄÜ:º¯ÊýÓÃÓÚÊäÈëÁ÷¡£Ëü¶ÁÈë×Ö·û£¬Ö±µ½ÒѾ¶ÁÁËnum ¸ö×Ö·û(ĬÈÏΪ1)»òÊÇÖ±µ½×Ö·ûdelim ±»¶ÁÈë(ĬÈÏΪEOF).
Æäµ÷ÓÃÐÎʽΪcin.ignore(n,ÖÕÖ¹×Ö·û)
ÔÐÍ:istream &ignore( streamsize num=1, int delim=EOF );
cin.ignore()·½·¨cin.ignore(5,'c')µÄÊÇ´ÓÊäÈëÁ÷(cin)ÖÐÌáÈ¡×Ö·û£¬ÌáÈ¡µÄ×Ö·û±»ºöÂÔ(ignore)£¬²»±»Ê¹Óá£Ã¿Å×ÆúÒ»¸ö×Ö·û£¬Ëü¶¼Òª¼ÆÊýºÍ±È½Ï×Ö·û£ºÈç¹û¼ÆÊýÖµ´ïµ½5»òÕß±»Å×ÆúµÄ×Ö·ûÊÇ'c'£¬Ôòcin.ignore()º¯ÊýÖ´ÐÐÖÕÖ¹£»·ñÔò£¬Ëü¼ÌÐøµÈ´ý¡£ËüµÄÒ»¸ö³£Óù¦ÄܾÍÊÇÓÃÀ´Çå³ýÒԻسµ½áÊøµÄÊäÈ뻺³åÇøµÄÄÚÈÝ£¬Ïû³ýÉÏÒ»´ÎÊäÈë¶ÔÏÂÒ»´ÎÊäÈëµÄÓ°Ïì¡£±ÈÈç¿ÉÒÔÕâôÓãºcin.ignore(1024, '\n');ͨ³£°ÑµÚÒ»¸ö²ÎÊýÉèÖõÃ×ã¹»´ó£¬ÕâÑùʵ¼ÊÉÏ×ÜÊÇÖ»Óеڶþ¸ö²ÎÊý'\n'Æð×÷Óã¬ËùÒÔÕâÒ»¾ä¾ÍÊǰѻسµ(°üÀ¨»Ø³µ)֮ǰµÄËùÒÔ×Ö·û´ÓÊäÈ뻺³å(Á÷)ÖÐÇå³ý³öÈ¥¡£
#include <iostream> #include <string> using namespace std; int main() { char array[8]; cout<<"ÊäÈë×Ö·û´®£º"; cin.ignore(6,'a'); cin.getline(array,8); cout<<array<<endl; return 0; } |
×¢£ºcin.ignore(6,'a');ÔÚÈ¡Êý¾ÝµÄʱºòÈç¹ûÓöµ½ÖÕÖ¹×Ö·û¡®a¡¯ÄÇô¾ÍÈ¡aºóÃæµÄÊý¾Ý£¬Èç¹ûûÓÐÓöµ½ÖÕÖ¹×Ö·û£¬ÄÇô¾Í´ÓµÚÆ߸öÊý¾Ý¿ªÊ¼È¡£»