C++对C的改进(1)
新的初始化方法
C提供的初始化方法int x = 1024;
C++提供兩種初始化方法:復制初始化(copy-initialization)
如:int x = 1024;
直接初始化(direct-initialization):
如:int x(1024);
注意:
①初始化不是簡單地賦值,初始化指聲明變量或對象并且賦初值;賦值指用新值覆蓋變量或對象當前值。
②直接初始化語法更靈活且效率更高
③初始化內置類型變量兩種初始化幾乎沒有差別對于類類型的初始化,有時只能采用直接初始化(以后討論)
④兩種初始化的方法可以混用(見下例)
合使用初始化的例子:
#include <iostream> using namespace std; int main() {double salary = 9999.99, wage(salary + 0.01);cout<<salary<<" "<<wage<<endl;int interval,month = 8, day = 7, year(2008);cout<<year<<":"<<month<<":"<<day<<":"<<endl; system("PAUSE");return 0; }新的I/O
C語言的輸入輸出:
C++語言的輸入輸出:
C++語言的輸入輸出:先睹為快
#include <iostream> using namespace std; int main() {int x; double y;/*以下語句等價于printf("請輸入兩個整數(用逗號隔開):");*/cout << "請輸入兩個整數(用空格隔開):"; /*以下語句等價于scanf("%d %f", &x, &y);*/cin >> x >> y; /*以下語句等價于printf("x = %d, y = %f\n", x, y);*/cout << "x = " << x << ", y = " << y << endl; system("PAUSE");return 0; }C++的輸入輸出:cin,cout的用法
基本用法:? cout<<表達式1<<表達式2<<表達式n;
? cin>>變量1>>變量2>>變量n;
例如: cout<<"x + y ="<< x + y << "." << endl; cin >> x >> y;[注意]
①不能用一個<<輸出多個數據項
cout<<a,b,c<<endl; /*錯誤*/ cout<<a<<b<<c<<endl; /*正確*/②cin/cout可以分成多行來寫
cout的用法舉例:
#include <iostream> using namespace std; int main() {cout << "This is a C++ program! " << endl;cout << "This is"<< " a C++"<< "program!"<< endl;system("PAUSE");return 0; }cin的用法舉例:
int main() { char c1, c2;int a; float b;cin >> c1 >> c2 >> a >> b;cout << "c1 = " << c1 << endl<< "c2 = " << c2 << endl<< "a = " <<a << endl<< "b = " << b << endl;system("PAUSE");return 0;}cout與輸出控制字符:
#include <iostream> #include <iomanip> using namespace std; int main() {int x = 0;cout << "請輸入一個八進制整數(以開始):";cin >> oct >> x; cout << "x的十六進制表示為:" << hex << x << endl;cout << "x的十進制表示為:" << dec << x << endl;cout << "x的八進制表示為:" << oct << x << endl;system("PAUSE");return 0; }
輸出控制符:
?注意:若用控制符,程序需包含頭文件#include<iomanip>
有關類型的區別
有關類型的區別:bool類型
| ? | 邏輯類型 | 真 | 假 |
| C | 沒提供 | 非0 | 0 |
| C++ | bool | true | false |
注意:
1)bool類型的取值只有兩種true,false
2)輸出時默認輸出0或者1
3)用boolalpha可以改變默認的輸出方式,noboolalpha可以恢復默認的輸出方式
#include <iostream> using namespace std; int main() {bool bval1 = 1 < 2;bool bval2 = true;bool bval3 = false;bool bval4 = 4;bool bval5 = 0;cout << "bval1=" << bval1 << endl;cout << "boolalpha bval1=" << boolalpha << bval1 << endl;cout << "noboolalpha bval1=" << noboolalpha << bval1 << endl;cout << "bval2=" << bval2 << endl;cout << "bval3=" << bval3 << endl;cout << "bval4=" << bval4 << endl;cout << "bval5=" << bval5 << endl;system("PAUSE");return 0; }有關類型的區別:string類
#include <iostream> #include <string> using namespace std;int main() {string name = "student";string address = "Hebei... ...";cout << name << address <<endl;return 0; }string對象的定義和初始化:
| 初始化string對象的方式 | |
| string s1; | 默認構造函數,s1為空串 |
| string s2(s1); | 將s2初始化為s1的一個副本 |
| string s3("value"); | 用字符串字面值初始化s3 |
| String s4(n,'c') | 將s4初始化為字符'c'的n個副本 |
string對象的讀寫:用cin、cout讀寫string對象
注意:
cin忽略開頭所有空格、TAB、回車符
不接收含空格的字符串
string的I/O:
#include <iostream> #include <string> using namespace std; int main() {string s;cin >> s; //hello world!cout << s <<endl; //hellosystem("PAUSE");return 0; }string讀入未知數目的對象:
#include <iostream> #include <string> using namespace std; int main() {string word;while(cin >> word)cout << word << endl;system("PAUSE");return 0; }string對象的讀寫:用getline讀取整行文本(含空格)。
#include <iostream> #include <string> using namespace std; int main() {string line;while(getline(cin, line)) cout << line << endl;system("PAUSE");return 0; }注意:
getline的第一個參數通常為cin,?第二個參數為string對象
從錄入數據的下一行讀取,可讀取? 任何字符
getline()以回車作為結束符?(不接受換行符)
getline()不忽略前導回車,若第一?個就是換行符,則置為空串
string對象的操作,設有:string s, s1;| string的操作 | |
| s.empty() | 若s為空串,則返回true,否則返回false |
| s.size() | 返回s中字符的個數 |
| s[n] | 返回s中位置為n的字符,位置從0開始 |
| s1+s2 | 將兩個串連接成新串,返回新生成的串 |
| s1 = s2 | 把s1得內容替換為s2的副本 |
| v1 == v2 | 判定時候相等,相等返回true,否則返回false |
| != < <= > >= | 保持這些操作慣有的含義,如:s != s2; |
注意:
1、size()的返回類型并非int而是string::size_type類型的值,建議不要把size()的返回值賦值給int變量
string s2 = "hello"; string::size_type count = s2.size(); 2、兩個string對象+時,+操作符左右操作數必須至少有一個是string #include <iostream> #include <string> using namespace std;int main() {string s1 = "hello";string s2 = "world";string s3 = s1 + ",";string s4 = "hello" + "world "; //errorstring s5 = "hello" + s2 + "world" ;system("PAUSE");return 0; } 3、string對象下標操作時,任何無符號整型值均可用作下標,但下標的實際類型為string::size_type4、string下標操作可用作左值
int main() { string str = "student";cout << str << endl;for(string::size_type ix = 0; ix!=str.size(); ++ix)str[ix] = 'x';cout << str << endl;system(" PAUSE ");return 0; }有關類型的區別:枚舉
C++對枚舉的改進:
①在C++中定義枚舉變量可以不用enum
enum weekday {sun, mon, tue, wed, thu, fri, sat}; weekday w; //省略了enum②無名枚舉:不給出枚舉類型名和變量,將枚舉元素當符號常量用
enum {min = 0, max = 100}; int x = min, arr[max];... ...
有關類型的區別:union
C++對聯合的擴展:
①無名聯合:沒有聯合體類型名和變量名的聯合體
#include <iostream> using namespace std; int main() {union {char c;int i;double d;};c = 'a';cout << c << endl;return 0; }②定義聯合變量無需給出union
#include <iostream> using namespace std; union test { char c;int i;double d; }; int main() {test m = {'a'};cout << m.c << endl;return 0; }有關類型的區別:struct
C++對結構體的擴展
①定義結構體變量可以不用struct
struct point {double x;int a; }; point p;②成員可以包含函數定義
struct point{double x,y; //數據成員void setvalue(double a,double b) //成員函數{ x = a; y = b; } };總結
以上是生活随笔為你收集整理的C++对C的改进(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布式事物嵌套事物
- 下一篇: DJANGO里让用户自助修改邮箱地址