來自官方文檔。。。感謝老王指出需要c++11,一下代碼全在c++11下編譯,編譯參數加入 -std=c++11
#include<stdio.h> #include<iostream> #include<queue> #include<map> #include<memory.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <climits> #include <sstream> using namespace std; int main(const int argc, char** argv) { //c style int i = 0; //c++ style int j(1); //only in c++ 11 int k = { 2 }; cout << i << endl; cout << j + k << endl; int foo = 0; //same as int bar=foo; auto bar = foo; cout << bar << endl; //bar2 type is int,has no initVal decltype(foo) bar2; bar2 = 4.2000; cout << bar2 << endl; cout << sizeof(wchar_t) << endl; //base 10 int ii = 10; //base 8 int kk = 010; //base 16 int kkk = 0xff; cout << ii << " " << kk << " " << kkk << endl; int kkkk = 75; //int unsigned int uik = 4294967295u; //unsigned int long lk = 75l; //long long ulk = 75ul; //unsigned long cout << kkkk << " " << uik << " " << lk << " " << ulk << endl; cout << "long double size " << sizeof(long double) << endl; cout << "long long int size " << sizeof(long long int) << endl; //default type for floating-point literals is double //we can add suffix f or l float fi = 6.02e23f; //float long double ld = 3.14159L; //long double cout << "fi " << fi << " long double " << ld << endl; //Internally, computers represent characters as numerical codes //計算機本質上將字符表示成數字 string x = "string expressed in \ two lines"; cout << x << endl; //All the character literals and string literals described above are made of characters of type char //所有字符和字符串字面量都是由char組成,可以加下面的前綴 //u char16_t //U char32_t //L wchar_t //string字面量可以加以下前綴 //u8 The string literal is encoded in the executable using UTF-8 //執行時用utf-8編碼 //R The string literal is a raw string //表示原始的string string ss = R"(string with \backslash)"; string sss = "(string with \backslash)"; cout << ss << endl; cout << sss << endl; //c++已經存在三個字面量 true false nullptr cout << "bool" << sizeof(bool) << " sizeof nullptr" << sizeof(nullptr) << endl; bool footrue = true; bool foofalse = false; int* p = nullptr; cout << footrue << endl; cout << foofalse << endl; //&p p的地址 //*p p指向內存的內容 //p p這塊內存中的值 cout << p << endl; cout << "&p " << &p << endl; int** pp = &p; cout << pp << endl; //有魔力的語法 //根據文檔的描述 c++內置三個字面量 true,false nullptr cout << (false == nullptr) << endl; //顯示轉換 int ci; float cf = 3.14; //繼承自 c ci = (int) cf; //c++ style ci = int(cf); //The value returned by sizeof is a compile-time constant, so it is always determined before program execution. //sizeof返回編譯時的常量,所以這個值永遠都在執行前確定 string mystr("123465 456 79"); int myint; //標准頭文件sstream定義了一種叫做stringstream的類型,允許把string作為輸入流 stringstream sin(mystr); //流已經到末尾 string mystr2; while (sin >> myint) cout << myint << " "; cout << endl; getline(stringstream(mystr), mystr2); cout<<mystr2<<endl; return 0; }