C++ primer第4章题目解答

习题4.7

编写一个必要的代码将以数组赋给另外一个数组,然后把这段代码用vector实现

#include<iostream>
#include<array>
using std::array;
int main()
{
	array<int, 5> j;
	for (int i = 0; i < 5; i++)
	{
		j[i] = i;
	}
	array<int, 5> k = j;
	for (int i = 0; i < 5; i++)
	{
		std::cout << k[i] << std::endl;
	}


	std::cin.get();
	return 0;
}



#include<iostream>
#include<vector>
using std::vector;
int main()
{
	vector<int> j(5,0);
	vector<int> k;
	vector<int>::iterator p = j.begin();
	for (p; p != j.end(); p++)
	{
		k.push_back(*p);
	}
	for (int i = 0; i < 5; i++)
	{
		std::cout << k[i] << std::endl;
	}
	std::cin.get();
	return 0;

}




编写程序判断两个数组是否相等,然后编写一段类型的程序比较vector

1.数组

#include<iostream>
#include<array>
using std::array;
int main()
{
	array<int, 6> num1;
	array<int, 6> num2;
	for (int i = 0; i < 6; i++)
	{
		std::cin >> num1[i];
		
	}
	for (int i = 0; i < 6; i++)
	{
		std::cin >> num2[i];

	}
	if (num1 == num2)
	{
		std::cout << "相等" << std::endl;
	}
	else
	{
		std::cout << "不相等" << std::endl;
	}
	

	
	system("pause");

	return 0;
}

vector使用

#include<iostream>
#include<vector>
using std::vector;
int main()
{


	vector<int> num1, num2;
	int num;
	std::cin >> num;
	while (num != -1)
	{
		num1.push_back(num);
	   std::cin >> num;
	}
	std::cin >> num;
	while (num != -1)
	{
		num2.push_back(num);
		std::cin >> num;
	}
	if (num2 == num1)
	{
		std::cout << "相等" << std::endl;
	}
	else
	{
		std::cout << "不相等" << std::endl;
	}


	system("pause");



	return 0;

}


4.25

编写程序比较2个string类型的字符串,然后编写另外一个程序比较C语言风格字符串值

string类型比较

#include<string>
#include<iostream>
using std::string;

int main()
{

	string str1, str2;
	getline(std::cin, str1);
	getline(std::cin, str2);
	if (str1 == str2)
	{
		std::cout << "相等" << std::endl;
	}
	else if (str1 > str2)
	{
		std::cout << "str1>str2" << std::endl;
	}
	else
	{
		std::cout << "str1<str2" << std::endl;
	}

	system("pause");

	return 0;
}


C语言风格字符串比较

#include<string>
#include<iostream>
using std::string;

int main()
{
	char *str1 = new char[80];
	char *str2 = new char[80];
	std::cin >> str1;
	std::cin >> str2;
	int res = strcmp(str1, str2);
	if (res > 0)
	{
		std::cout << "str1>str2" << std::endl;
	}
	else if (res < 0)
	{
		std::cout << "str1<str2" << std::endl;
	}
	else
	{
		std::cout << "str1==str2" << std::endl;
	}
	
	system("pause");
	return 0;
}



习题4.28

编写程序从标准输入设备读入元素数据建立一个int型vecor对象,然后动态创建一个与该vector对象大小一致的数组

把vector对象的所有元素复制给新的数组

#include<iostream>
#include<string>
#include<vector>
using std::vector;
int main()
{

	vector<int> k;
	int num;
	std::cin >> num;
	while (num != -1)
	{
		k.push_back(num);
		std::cin >> num;
	}
	int *p = new int[k.size()];
	vector<int>::iterator begin1 = k.begin();
	int i= 0;
	for (i, begin1; begin1 != k.end(); begin1++, i++)
	{
		p[i] = *begin1;

	}
	for (int i = 0; i < k.size(); i++)
	{
		std::cout << p[i] << "   ";
	}
         delete []p;
	system("pause");
	return 0;
}


习题4.30

编写程序连接2个c风格字符串字面值,把结果存储在一个c字符串风格中,接着,连接2个string

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<string>
int main()
{
	const char *str1 = "hello world ";
	const char *str2 = "你好 世界";
	int length = strlen(str1) + strlen(str2);
	char *p = new char[length + 1];
	strcpy(p, str1);
	strcat(p, str2);
	std::cout << p << std::endl;
	delete[]p;

	system("pause");


	return 0;
}

string类型连接

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<string>
int main()
{
	using std::string;
	string str1 = "hello world ";
	string str2 = "你好,中国";
	string str3 = str1 + str2;
	std::cout << str3 << std::endl;
	system("pause");
	return 0;
}


习题4.32

编写程序用int型数组初始化vector对象

#include<iostream>
#include<vector>
int main()
{
	using std::vector;
	const int num = 8;
	int a[num];
	for (int i = 0; i < num; i++)
	{
		std::cin >> a[i];
	}
	              //用数组名与数组名+num初始化vector              
	vector<int> ivec(a, a + num);
	vector<int>::iterator pj = ivec.begin();
	for (pj; pj != ivec.end(); pj++)
	{
		std::cout << *pj << " ";
	}
	system("pause");
	return 0;
}


习题4.33

编写程序把int型vector复制给int型数组

#include<iostream>
#include<vector>
int main()
{

	std::vector<int> ivec;
	int ival;
	std::cout << "Enter numbers" << std::endl;
	while (std::cin >> ival)
		ivec.push_back(ival);
	int *parr = new int[ivec.size()];
	int ix = 0;
		for (std::vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter, ++ix)
		{
			parr[ix] = *iter;
		}


	system("pause");
	return 0;
}


习题4.34

编写程序读入一组string类型的数据并将它们存储在vector中,接着,把该vector对象复制给一个字符指针数组,为vector中的每个元素创建一个新的字符数组

为vector中的每个元素创建一个新的字符数组,并把该vector元素的数据复制到相应的字符数组中,最后把指向该数组的指针插入字符指针数组

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<string>
int main()
{
	std::vector<std::string> svec;
	std::string str;
	while (std::cin >> str)
	{
		svec.push_back(str);  //插入到容器中
	}
	char **parr = new char *[svec.size()];  //分配
	int i = 0;
	for (std::vector<std::string>::iterator iter = svec.begin(); iter != svec.end(); ++iter, ++i)
	{
		char *p = new char[(*iter).size() + 1];
		strcpy(p, (*iter).c_str());
		parr[i] = p;
	}
	for (i = 0; i != svec.size(); ++i)
	{
		delete[]parr[i];
	}
	delete[]parr;




	std::cin.get();
	return 0;
}


4.35

输出习题4.34中建立的vector对象和数组的内容,输出数组后,记得释放字符数组

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<string>
int main()
{
	std::vector<std::string> svec;
	std::string str;
	while (std::cin >> str)
	{
		svec.push_back(str);  //插入到容器中
	}
	char **parr = new char *[svec.size()];  //分配
	int i = 0;
	for (std::vector<std::string>::iterator iter = svec.begin(); iter != svec.end(); ++iter, ++i)
	{
		char *p = new char[(*iter).size() + 1];
		strcpy(p, (*iter).c_str());
		parr[i] = p;
	}
	for (std::vector<std::string>::iterator nm = svec.begin(); nm != svec.end(); ++nm)
	{
		std::cout << *nm << std::endl;
	}
	for (i = 0; i != svec.size(); ++i)
	{
		delete[]parr[i];
	}
	delete[]parr;


	system("pause");

	std::cin.get();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值