-
假定$是操作系统提示符,可通过“ $ addItems outfile ”使得已经编译的名为addItems.exe 的可执行文件调用infile文件中的数据,并将输出结果写入名为outfile的文件中(两个文件都位于当前目录中)。
-
因为引用不是对象,因此不能定义指向引用的指针 、引用的数组、引用的引用。
int a = 9;
int& b = a;
int* c = &b;//此处实际上仍是对a操作
//下面都为错误示范
//int&* d = &b;此才为指向引用的指针
//int& e[3];
//int& &f;
int* &g = c;//指针的引用
- void指针
void指针可以存放任意对象的地址,因为其所指对象类型的不确定性,所以void*指针不能访问内存空间所存的对象。
int a = 0;
double c = 0.0001;
void* b = &a;
double* d = &c;
b = d;
cout << b <<" "<< &c<<endl;
//*b错误
- 阅读较复杂的声明语句时,从右向左阅读有利于理解。
int a = 1;
const int b = a;
int* c = &a;//指向a的指针
int** d = &c;//指向c指针的指针,即二级指针。
int*& e = c;//c指针的引用。
int const* f = &a;//==const int指针常量,不能改变值
int* const g = &a;//常量指针,不能改变指向对象。
const int* const h = &a;
- 类型别名。
typedef or using。
using G =Game;(Game 为类名)
typedef int in, * ini;
typedef const in const *inin;//==int const *。仅为指针常量,不能限定指针的取址。
int main() {
in a = 9,c=1;
ini b = &a;
inin d = &c;//d为const int *类型
b = &c;
*b = 5;
d = &a;
//*d = 5;错误
- 范围for(range for)语句
for (declaration : expression)
statement
int a[4];
//auto 可改为int,要求是前后一致
for (auto& val : a)
{
cout << "Enter an integer value: ";
cin >> val;
}
for (auto c : a) {
char b = c;
if (b < 'm')
cout << c << endl;
}
- 迭代器与标准库函数begin和end
string s1("hello");
int a[] = { 10,1,2,3,4 };
auto n = s1.end();//尾后迭代器
auto m = begin(a);
auto b = a;//b为指向数组a的指针
- 用指针指向用string类型初始化的数组 & 用数组初始化vector对象
int a[] = { 0,1,2,3,4,5 };
string s1("hello world");
vector<int>v1(a+1,a+4);//范围为a[1]~a[3]。
vector<int>v2(begin(a), end(a));
const char* str = s1.c_str();//用string对象初始化char*。
cout << *(str+6) << endl;//输出w
cout << v1[2] << endl;//输出3
cout << v2[3] << endl;//输出3