C++Primer 习题3.6-3.20

C++Primer 习题3.6-3.20

string、vector的非迭代器用法

//3.6
#include<iostream>
#include<string>
using namespace std;
int main()
{

	for (char c : str1)
	{
		str2 += 'X';
	}
	str1 = str2;
	cout << str1 << endl;
	//3.7
	/*
	如果改成char,结果相同
	*/
	return 0;
}

//3.8
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1{ "woshinibaba!" };
	//string str2;
	for (int i = 0; i < str1.size(); i++)
		str1[i] = 'X';
	cout << str1 << endl;
	string str2{ "woyeshinibaba!" };
	int i = 0;
	while (i < str2.size())
	{
		str2[i] = 'X';
		i++;
	}
	cout << str2 << endl;
	i = 0;
	return 0;
}

//3.10
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = { "A11!,)+_ b	 b" };
		for (auto c : str1)
			if (!ispunct(c))
				cout << c;
	return 0;
}

//3.11
#include<iostream>
#include<string>
using namespace std;
int main()
{
	const string str = "keep out";
		for (auto a : str)
		{
			cout << a;
		}
		/*
		这里VS2017可以通过。。。。
		*/
	return 0;
}

//3.14
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> vint;
	for (int a; cin >> a;)
	{
		char f;
		vint.push_back(a);
		cout << "继续吗?F退出" << endl;
		cin >> f;
		if (f == 'F')
			break;
	}
	for (auto a : vint)
		cout << a << " ";
	return 0;
}

//3.15
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
	vector<string> vint;
	for (string a; cin >> a;)
	{
		int f;
		vint.push_back(a);
		cout << "继续吗?1退出" << endl;
		cin >> f;
		if (f == 1)
			break;
	}
	for (auto a : vint)
		cout << a << " ";
	return 0;
}

//3.16
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> vint{ 1,2,33,444 };
	cout << "容器大小为:" << vint.size() << endl;
	cout << "容器内元素为:";
	for (auto a : vint)
		cout << a << " ";
	return 0;
}

//3.17
/*
使用toupper(c);
*/

//3.18
/*
ivec应当初始化,或者pushback一个元素等
*/

//3.19
#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> vint1{ 42,42,42,42,42,42,42,42,42,42 };
	vector<int> vint2(10, 42);
	vector<int> vint3;
	for (int i = 0; i < 10; i++)
	{
		vint3.push_back(42);
	}
	for (auto a : vint1)
		cout << a << " ";
	cout << endl;
	for (auto a : vint2)
		cout << a << " ";
	cout << endl;
	for (auto a : vint3)
		cout << a << " ";
	cout << endl;
	//等等等等等
	return 0;
}

//3.20
/*
还没想好如何终止输入的方便方法,程序简单不做赘述
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值