数组
字符串
读取整行cin.getline()
缓冲队列里没有回车
读取整行cin.get()
缓冲队列里有回车,第二个cin.get读出来的是回车
例子
// numstr.cpp -- following number input with line input
#include <iostream>
int main()
{
using namespace std;
cout << "What year was your house built?\n";
int year;
cin >> year;
cin.get(); // 把上一行的回车读走
cout << "What is its street address?\n";
char address[80];
cin.getline(address, 80);
cout << "Year built: " << year << endl;
cout << "Address: " << address << endl;
cout << "Done!\n";
// cin.get();
return 0;
}
string类
// strtype4.cpp -- line input
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
int main()
{
using namespace std;
string str;
cout << "Enter another line of text:\n";
getline(cin, str); // 从输入流中捕获,赋值到str中
cout << "You entered: " << str << endl;
return 0;
}
共用体union
指针
*取值运算符
&取址运算符
// pointer.cpp -- our first pointer variable
#include <iostream>
int main()
{
using namespace std;
int updates = 6; // declare a variable
int * p_updates; // declare pointer to an int
p_updates = &updates; // assign address of int to pointer
// express values two ways
cout << "Values: updates = " << updates;
cout << ", *p_updates = " << *p_updates << endl;
// express address two ways
cout << "Addresses: &updates = " << &updates;
cout << ", p_updates = " << p_updates << endl;
// use pointer to change value
*p_updates = *p_updates + 1;
cout << "Now updates = " << updates << endl;
// cin.get();
return 0;
}
输出
E:\xy\Code\cLionCode\cmake-build-debug\cLionCode.exe
Values: updates = 6, *p_updates = 6
Addresses: &updates = 0xd124dff784, p_updates = 0xd124dff784
Now updates = 7
进程已结束,退出代码为 0
指针的危险
223323是会写入系统随机分配的地址,该地址可能不允许输入这样的数据或者不允许修改
指针和数字
使用new来分配内存
pn 存地址0x0001
// use_new.cpp -- using the new operator
#include <iostream>
int main()
{
using namespace std;
int nights = 1001;
int * pt = new int; // allocate space for an int
*pt = 1001; // store a value there
cout << "nights value = ";
cout << nights << ": location " << &nights << endl; // 1001 0xb29b1ff824
cout << "int ";
cout << "value = " << *pt << ": location = " << pt << endl; // 1001 0x1dbcba86b70
double * pd = new double; // allocate space for a double
*pd = 10000001.0; // store a double there
cout << "double ";
cout << "value = " << *pd << ": location = " << pd << endl; // 10000001.0 0x1dbcba86b90
cout << "location of pointer pd: " << &pd << endl; // 0xb29b1ff818
cout << "size of pt = " << sizeof(pt); // 8 对应64位操作系统
cout << ": size of *pt = " << sizeof(*pt) << endl; // 4 new int
cout << "size of pd = " << sizeof pd; // 8 对应64位操作系统
cout << ": size of *pd = " << sizeof(*pd) << endl; // 8 new double
// cin.get();
return 0;
}
代码解析图
指针和数组
// arraynew.cpp -- using the new operator for arrays
#include <iostream>
int main()
{
using namespace std;
double * p3 = new double [3]; // space for 3 doubles
p3[0] = 0.2; // treat p3 like an array name
p3[1] = 0.5;
p3[2] = 0.8;
cout << "p3[1] is " << p3[1] << ".\n"; // 0.5
p3 = p3 + 1; // 0.5(指针前移)
cout << "Now p3[0] is " << p3[0] << " and "; // 0.5
cout << "p3[1] is " << p3[1] << ".\n"; // 0.8
p3 = p3 - 1; // 0.2
delete [] p3; // free the memory
// cin.get();
return 0;
}
地址一样,但是tell+1是+2,&tell+1是+20(电子版有错误)
// addpntrs.cpp -- pointer addition
#include <iostream>
int main()
{
using namespace std;
short tell[10] = {3, 2, 1};
cout << tell << endl;
cout << &tell << endl;
cout << tell + 1 << endl;
cout << &tell + 1 << endl;
return 0;
}
结果
E:\xy\Code\cLionCode\cmake-build-debug\cLionCode.exe
0x7bd81ffc80
0x7bd81ffc80
0x7bd81ffc82
0x7bd81ffc94
进程已结束,退出代码为 0
指针和结构体