C++ Little White Textbook Exercise 3

Textbook test 10 dynamically allocate integer array.cpp

#include <iostream>
using namespace std;
int * pArray; //指向数组的指针
int i = 5;

int main() {
    
    
	pArray = new int[i * 20];//分配了100个元素的整型数组
	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;
	//delete p;//出错,p是引用,不是动态分配的
	int *q = new int;
	*q = 8;
	cout << *q << endl;
	delete q;//正确,q指向动态分配的空间
	//指针p和指针q都只想一个整型变量,但指针p指向的空间并不是使用new运算符分配的,所以不能使用delete释放。
	//指针q指向的空间是使用new分配的,所以使用完毕,通过delete进行释放
	return 0;
}

String object handles strings

#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;
	//字符数组对string变量进行初始化
	char name[] = "C++程序";
	string s1 = name;
	cout << "s1=" << s1 << endl;
	//还可以声明一个string 对象数组,即数组中每个元素都是字符串
	string citys[] = {
    
     "Beijing","Shanghai","Tianjin","Chongqing" };
	cout << citys[1] << endl;
	cout << sizeof(citys) / sizeof(string) << endl;
	//最后一行语句是输出数组元素的个数,citys是string对象数组,sizeof(citys)是整个数组占用的空间大小
	//sizeof(string)是每个string对象的大小,所以sizeof(citys)/sizeof(string)表示的是数组元素个数


	//string对象的操作  cin cout
	string s10, s2;
	cin >>s10 >> s2;
	cout << s10 << "," << s2 << endl;

	//string 对象之间可以互相赋值,也可以用字符串常量和字符数组的名字对string对象进行复制。
	//复制时不需要考虑被复制的对象是否有足够的空间来存储字符串。例如
	string ss1, ss2 = "OK";
	ss1 = "China";
	ss2= ss1; //ss1和ss2不表示的字符串不登场,复制后ss2的内容和ss1相同
	cout << "ss1=" << ss1 << "  ss2=" << ss2 << endl;
}

The textbook tests the use of 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=

	s3 = s1 + s2;
	cout << s1 + s2 << endl;//输出C++程序C++程序
	cout << "s3=" << s3 << endl;//输出s3=C++程序C++程序
	s3 += "de";
	cout << "s3=" << s3 << endl;//上=C++程序C++程序de

	bool b = s1 < s3; //b=true
	cout << "bool=" << b << endl;//输出 bool=1
	char c = s1[2];
	cout << "c=" << c << endl; //输出 c=+
	cout << s1[2] << endl;//输出 +
	char arrstr[] = "Hello";
	s3 = s1 + arrstr;
	cout << s3 << endl; //输出C++程序 Hello
}
int main() {
    
    
	show();
}

Use of string member functions.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;
	//字符串str在未赋值之前是空串,空串的长度为0。函数c_str()将字符串str 转换为 const char *,赋给p
	//find()函数时重载函数,有多个不同的参数版本
}

Guess you like

Origin blog.csdn.net/weixin_42292697/article/details/115029850