C++Primer 习题3.39-3.45

C++Primer 习题3.39-3.45

//3.39
#include<iostream>
#include<string>
using namespace std;
int main()
{
	const string str1{ "woshinibaba" };
	const string str2{ "Woyeshinibaba" };
	if (str1 != str2)
		cout << "字符串不相等" << endl;
	else
		cout << "字符串相等" << endl;
	char cst1[]{ 'w','s','n','b','b','\0' };
	char cst2[]{ 'w','s','y','n','b','b','\0' };
	if(strcmp(cst1,cst2))
		cout << "C字符串不相等" << endl;
	else
		cout << "C字符串相等" << endl;
	return 0;
}

//3.40,VS2017无法实现
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	char str1[]{ "wozhendeshinibaba" },
		str2[]{ "我真的是你爸爸" };
	char cst[18 + 15 - 1];
	strcpy(cst, str1);
	strcat(cst, str2);
	for (auto a : cst)
		cout << a;
	return 0;
}

//3.41
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	int a[]{ 1,2,3,4,5,6,7,8,9,0 };
	vector<int>va;
	for (int i : a)
		va.push_back(i);
	for (int i : va)
		cout << i << " ";
	return 0;
}

//3.42
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int>va{ 1,2,3,4,5,6,7,8,9,0 };
	int a[10];
	for (int i = 0; i < va.size(); i++)
		a[i] = va[i];
	for (int i : a)
		cout << i << " ";
	return 0;
}

//3.43,3.44,3.45
#include<iostream>
using namespace std;
int main()
{
	int a[2][10]{ { 0,1,2,3,4,5,6,7,8,9 } ,{9,8,7,6,5,4,3,2,1,0} };
	for (auto &p : a)
	{
		for (auto q : p)
			cout << q << " ";
		cout << endl;
	}
	cout << endl;
	for (auto p = a; p != a + 2; p++)
	{
		for (auto q = *p; q != *p + 10; q++)
			cout << *q << " ";
		cout << endl;
	}
	cout << endl;
	for (int p = 0; p < 2; p++)
	{
		for (int q = 0; q < 10; q++)
			cout << a[p][q] << " ";
		cout << endl;
	}
	cout << endl;
	using intarray = int[10];
	for (intarray *p = a; p != a + 2; ++p)
	{
		for (int *q = *p; q != *p + 10; q++)
			cout << *q << " ";
		cout << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值