课本测试10动态分配整型数组.cpp
#include <iostream>
using namespace std;
int * pArray;
int i = 5;
int main() {
pArray = new int[i * 20];
pArray[0] = 20;
pArray[99] = 30;
for (int i = 0; i < 99; i++)
{
cout << pArray[i];
}
int oneInt = 6;
int *p = & oneInt;
cout << *p << endl;
int *q = new int;
*q = 8;
cout << *q << endl;
delete q;
return 0;
}
字符串对象处理字符串
#include <iostream>
using namespace std;
#include <string>
string str1;
string city = "Beijing";
string str2 = "city";
int main() {
cout << "str1="<< str1 << "." << endl;
cout << city << "," << str2 << endl;
char name[] = "C++程序";
string s1 = name;
cout << "s1=" << s1 << endl;
string citys[] = { "Beijing","Shanghai","Tianjin","Chongqing" };
cout << citys[1] << endl;
cout << sizeof(citys) / sizeof(string) << endl;
string s10, s2;
cin >>s10 >> s2;
cout << s10 << "," << s2 << endl;
string ss1, ss2 = "OK";
ss1 = "China";
ss2= ss1;
cout << "ss1=" << ss1 << " ss2=" << ss2 << endl;
}
课本测试12string的使用.cpp
#include <iostream>
using namespace std;
#include <string>
void show() {
string s1, s2;
s1 = "C++程序";
s2 = s1;
string s3;
cout << "s3=" << s3 << endl;
s3 = s1 + s2;
cout << s1 + s2 << endl;
cout << "s3=" << s3 << endl;
s3 += "de";
cout << "s3=" << s3 << endl;
bool b = s1 < s3;
cout << "bool=" << b << endl;
char c = s1[2];
cout << "c=" << c << endl;
cout << s1[2] << endl;
char arrstr[] = "Hello";
s3 = s1 + arrstr;
cout << s3 << endl;
}
int main() {
show();
}
字符串成员函数的使用.cpp
#include <iostream>
using namespace std;
#include <string>
int main()
{
string str;
if (str.empty()) {
cout << "str is NULL." << ",length=" << str.length() << endl;
}
else {
cout << "str is not NULL." << endl;
}
str = str.append("abcdefg");
cout << "str is" << str << ",size=" << str.length() << endl;
const char* p = str.c_str();
cout << "p=" << p << endl;
cout << "find:" << str.find("de", 0) << endl;
cout << "find:" << str.find("de", 4) << endl;
string str1 = str.insert(4, "123");
cout << str1 << endl;
return 0;
}