Listing 4.20
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
int main()
{
string h;
using namespace std;
char animal[20] = "bear"; // animal holds bear
const char * bird = "wren"; // bird holds address of string
char * ps; // uninitialized
cout << animal << " and "; // display bear
cout << bird << "\n"; // display wren
// cout << ps << "\n"; //may display garbage, may cause a crash
cout << "Enter a kind of animal: ";
cin >> animal; // ok if input < 20 chars
// cin >> ps; Too horrible a blunder to try; ps doesn't
// point to allocated space
ps = animal; // set ps to point to string
cout << ps << "!\n"; // ok, same as using animal
cout << "Before using strcpy():\n";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
ps = new char[strlen(animal) + 1]; // get new storage
strcpy_s(ps, strlen(animal) + 1, animal); // copy string to new storage
cout << "After using strcpy():\n";
cout << animal << " at " << (int *)animal << endl;
cout << ps << " at " << (int *)ps << endl;
delete[] ps;
cin >> h;
return 0;
}
strcpy_s需要三个参数!
Listing 4.24. choices.cpp
// choices.cpp -- array variations
#include <iostream>
#include <vector> // STL C++98
#include <array> // C++11
int main()
{
using namespace std;
// C, original C++
double a1[4] = { 1.2, 2.4, 3.6, 4.8 };
// C++98 STL
vector<double> a2(4); // create vector with 4 elements
// no simple way to initialize in C98
a2[0] = 1.0 / 3.0;
a2[1] = 1.0 / 5.0;
a2[2] = 1.0 / 7.0;
a2[3] = 1.0 / 9.0;
// C++11 -- create and initialize array object
array<double, 4> a3 = { 3.14, 2.72, 1.62, 1.41 };
array<double, 4> a4;
a4 = a3; // valid for array objects of same size
// use array notation
cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;
cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;
cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
// misdeed
a1[-2] = 20.2;
cout << "a1[-2]: " << a1[-2] << " at " << &a1[-2] << endl;
cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
return 0;
}
Listing 5.12
int main()
{
string h;
string word = "?ate";
for (char ch = 'a'; <strong>word != "mate"</strong>; ch++)
{
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
cin >> h;
return 0;
}
int main()
{
string h;
char word[5] = "?ate";
for (char ch = ‘a’; strcmp(word, "mate"); ch++)
{
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
cin >> h;
return 0;
}
<pre name="code" class="cpp">
string 类比C类更加方便,可以直接比较大小。
The way thestringclass overloads the!=operator allows you to use it as long as atleast one of the operands is astringobject; the remaining operand can be either astringobject or a C-style string.
若用 word!="mate"则表示二者地址相等,并不是两者的值。
const 代替define
vector动态数组
array定长度数组,更加安全
Listing 6.14 cingolf.cpp
const int Max = 5;
int main()
{
string h;
// get data
int golf[Max];
cout << "Please enter your golf scores.\n";
cout << "You must enter " << Max << " rounds.\n";
int i;
int j = 0;
char c;
for (i = 0; i < Max; i++)
{
cout << "round #" << i + 1 << ": ";
while (!(cin >> golf[i])) {
cin.clear(); // reset input
while ((c= cin.get() )!= '\n'){
cout << c << " 23333" << endl;
cout <<"j="<< j << endl;
cout << ++j<<endl;
continue; // get rid of bad input
}
cout << "Please enter a number: ";
}
}
// calculate average
double total = 0.0;
for (i = 0; i < Max; i++)
total += golf[i];
// report results
cout << total / Max << " = average score "
<< Max << " rounds\n";
cin >> h;
return 0;
}
1,clear()清除缓冲区,内部数据都被清除掉
2,cin.get() != '\n',只获取你缓冲区中的数据,当它的返回值为'\n'跳出
提示:continue与if连用,当遇到continue时,执行语句会跳过continue下面的语句!
键盘敲入字符,都暂存才cin的缓冲里面,当发现回车键时,开始从缓冲读取数据。cin >> golf时,就是从缓冲读取数据到golf,golf是int型,当从缓冲读取到其它非数字字符时,意味着读取失败。失败就给cin.?flag?产生错误标志,当错误标志存在时,cin就不能工作了,必须先清除掉。所以有了cin.clear()
1.当错误发生是,缓冲中已经被读取的字符已然没了,余下的都是从错误字符往后的字符了(产生错误的字符也已经被读走了,否则怎么产生错误)。Enter是在最后的,当然还存在着。
cin.clear(),只清除了错误标记。
2.既然发生了错误,就干脆把缓冲清空,重新进行输入。
所以就使用cin.get()从缓冲读取字符,直到连回车一起读走。每读一个字符,缓冲里就少一个字符。
3. 控制台程序,光标始终在给用户进行输入的地方。与cin无关。
7.4
int sum(int(*ar2)[4], int size);//1
int sum(int ar2[][4], int size);//2
int *ar2[4]; //3
//1&2 是同样的意义,都是指向4个int组成数组的指针
//3是 4个指向int指针所组成的数组
7.10函数指针
double pam(int);
double (*pf)(int);
pf = pam; // pf now points to the pam() function
double x = pam(4); // call pam() using the function name
double y = (*pf)(5); // call pam() using the pointer pf
double y = pf(5); // also call pam() using the pointer pf
pf 与*pf都可以调用 pam()函数。