来自官方文档。。。感谢老王指出需要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 ackslash)"; string sss = "(string with ackslash)"; 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; }