c++ primer 学习笔记-第三章

习题3-4:

#include <iostream>
#include <string>
using std::cin; using std::cout;
using std::endl; using std::string;

int main()
{
	string s1, s2;
	while (cin >> s1 >> s2)
	{
		if (s1 == s2)
			cout << "same length" << endl;
		else
			cout << (s1.size() > s2.size() ? s1 : s2) << endl;
	}
	getchar();
	getchar();
	return 0;
}

习题3-5:

#include <iostream>
#include <string>
using std::cin; using std::cout;
using std::endl; using std::string;

int main()
{
	string sTotal;
	for (string sNew; cin >> sNew; sTotal += sNew + ' ');
	cout << sTotal << endl;
	getchar();
	getchar();
	return 0;
}


习题3.10:

#include <iostream>
#include <string>
#include <cctype>
using std::cin; using std::cout;
using std::endl; using std::string;

string space_delete(string sOrigin)
{
	string sNew;
	for (auto c:sOrigin)
		if (!ispunct(c))
		{
			sNew += c;
		}
	return sNew;
}

int main()
{
	string sOrigin;
	getline(cin,sOrigin);
	cout << space_delete(sOrigin) << endl;
	getchar();
	getchar();
	return 0;
}

习题3-14:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

int main()
{
	string v;
	vector<decltype(v)> vec;
	while (cin >> v)
		vec.push_back(v);
	for (auto vv : vec)
		cout << vv << endl;
	getchar();
	getchar();
	return 0;
}

习题3.17:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

void uppercase(vector<string> &svec)
{
	for (auto &v : svec)
		for (auto &c : v)
			c=toupper(c);//这里必须重新赋值 toupper是返回大写而不是直接修改
}
int main()
{
	vector<string> svec;
	for (string words; cin >> words; svec.push_back(words));
	uppercase(svec);
	for (auto sv : svec)
		cout << sv << endl;
	getchar();
	return 0;
}

习题3.20:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

void sum_of_near(vector<int> ivec)//并未考虑空、只有一个元素的情形
{
	for (auto index = 0; index != ivec.size() - 1; ++index)
		cout << ivec[index] + ivec[index + 1] << " " ;
	cout << endl;
}

void sum_of_side(vector<int> ivec)
{
	for (unsigned i = 0; i <= ivec.size()-i-1;++i)
		cout << ivec[i] + ivec[ivec.size() - i - 1] << " " ;
	cout << endl;
}
int main()
{
	vector<int> ivec;
	for (auto i = 0; cin >> i; ivec.push_back(i));//读入
	sum_of_near(ivec);
	sum_of_side(ivec);
	getchar();
	return 0;
}

习题3.22:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

void first_upper(vector<string> &svec)
{
	for (auto &citer : (*svec.begin()))
		citer = toupper(citer);
	for (auto siter = svec.begin(); siter != svec.end() && !siter->empty(); ++siter)
		cout << *siter;
	cout << endl;
}
int main()
{
	vector<string> svec;
	for (string sentence = ""; getline(cin,sentence);svec.push_back(sentence));
	first_upper(svec);
	getchar();
	return 0;
}

习题3.23:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

void read(vector<int> &ivec)
{
	int iv = 0, time = 10;
	while (time != 0 && cin >> iv)//此处应注意不把cin>>iv写在前,
	{                             //防止出现等待输入的情况,应该在输入10个数后直接结束循环 
		ivec.push_back(iv);
		--time;
	}
}
void twoTimes(vector<int> ivec)
{
	for (auto iter = ivec.begin(); iter != ivec.end(); ++iter)
		(*iter) *= 2;
}
void display(vector<int> ivec)
{
	for (auto iter : ivec)
		cout << iter << " ";
	cout << endl;
}

int main()
{
	vector<int> ivec;
	read(ivec);
	twoTimes(ivec);
	display(ivec);
	getchar();
	getchar();
	return 0;
}

习题3.24:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

void input(vector<int> &ivec)
{
	int i = 0;
	while (cin >> i)
		ivec.push_back(i);
	for (auto i : ivec)
		cout << i << " ";
	cout << endl;
}
void sum_of_near(vector<int> ivec)
{
	for (auto beg = ivec.cbegin(); beg != ivec.cend()-1; ++beg)
		cout << (*beg) + *(beg + 1)<<" ";
	cout << endl;
}
void sum_of_side(vector<int> ivec)
{
	auto beg = ivec.cbegin(), end = ivec.cend()-1;
	for (; beg <= end; ++beg, --end)
		cout << (*beg) + (*end) << " ";
	cout << endl;
}
int main()
{
	vector<int> ivec;
	input(ivec);
	sum_of_near(ivec);
	sum_of_side(ivec);
	getchar();
	getchar();
	return 0;
}

习题3.25:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

int main()
{
	vector<int> grades;
	for (int score = 0; cin >> score && score <= 100 &&score>=0; grades.push_back(score));
	vector<int> cnt(11, 0);
	auto cnt_beg = cnt.begin();
	for (auto beg = grades.cbegin(); beg != grades.cend(); ++beg)
		++(*(cnt_beg + (*beg) / 10));
	for (auto i : cnt)
		cout << i << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题3.32:

#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::string;
using std::vector;

int main()
{
	vector<int> ivec1(10);//此处我忘记设定项数10了,要根据github上的答案改进
	int iv = 0;
	for (int i = 0; i < 10 && cin >> iv; ++i)//注意此处先判断i<10
		ivec1.push_back(iv);
	for (auto i : ivec1)
		cout << i << " ";
	cout << endl;

	vector<int>ivec2(ivec1);
	for (auto i : ivec2)
		cout << i << " ";
	cout << endl;
	
	getchar();
	getchar();
	return 0;
}


习题3.34:我的试验程序(会报错)

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cin; using std::cout;
using std::endl; 
using std::string;using std::vector; 
using std::begin; using std::end;

int main()
{
	int a[10] = {};
	const int *const p1 = begin(a);
	const int *const p2 = end(a);
	p1 += p2 - p1;
	getchar();
	getchar();
	return 0;
}

习题3.35:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cin; using std::cout;
using std::endl; 
using std::string;using std::vector; 
using std::begin; using std::end;

int main()
{
	int arr[] = { 1, 2, 3, 4, 5 };
	int *p = arr;
	for (int i = 0; i != 5; ++i)//for (auto ptr = arr; ptr != arr + size; ++ptr) *ptr = 0;
		*(p + i) = 0;
	for (auto a : arr)
		cout << a << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}


习题3.36:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

using std::cin; using std::cout;
using std::endl; 
using std::string;using std::vector; 
using std::begin; using std::end;


bool compare(const int *const a1_begin, const int *const a1_end, const int *const a2_begin, const int *const a2_end)
{
	if ((a1_end - a1_begin) != (a2_end - a2_begin))
		return false;
	else
	{
		for (auto i = 0; i != a1_end - a1_begin; ++i)
			if (*(a1_begin + i) != *(a2_begin + i))
				return false;
		return true;
	}
}
int main()
{
	int a1[] = { 1, 2, 4, 5 };
	int a2[] = { 1, 2, 4, 5 };
	int a3[] = { 1, 2 };
	int a4[] = { 1, 2, 4, 6 };
	if (compare(begin(a1), end(a1), begin(a2), end(a2)))
		cout << "this two arrs are same." << endl;
	else
		cout << "this two arrs are different." << endl;
	
	cout << "============================" << endl;

	vector<int> ivec1{ 1, 2, 3, 4 };
	vector<int> ivec2{ 1, 2, 3, 5 };
	if (ivec1 == ivec2)
		cout << "same" << endl;
	else
		cout << "different" << endl;
	getchar();
	getchar();
	return 0;
}

习题3.41:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <cstring>
using std::cin; using std::cout;
using std::endl; 
using std::string;using std::vector; 
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	int int_arr[] = { 1, 2, 3, 4, 5 };
	vector<int> ivec(begin(int_arr), end(int_arr));
	for (auto i : ivec)
		cout << i << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题3.42:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <cstring>
using std::cin; using std::cout;
using std::endl; 
using std::string;using std::vector; 
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	vector<int> ivec{ 1, 2, 3, 4, 5 };
	int arr[5];
	for (auto i = 0; i != ivec.size(); ++i)
		arr[i] = ivec[i];
	for (auto i : arr)
		cout << i << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题3.43:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <cstring>
using std::cin; using std::cout;
using std::endl; 
using std::string;using std::vector; 
using std::begin; using std::end;
using std::strcat; using std::strcpy;

int main()
{
	int ia[3][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
	//version 1: range for
	for (int (&row)[4] : ia){
		for (int col : row)
			cout << col << " ";
		cout << endl;
	}
	//version 2: common for, []
	for (unsigned i = 0; i != 3; ++i){
		for (unsigned j = 0; j != 4; ++j)
			cout << ia[i][j] << " ";
		cout << endl;
	}
	//version 3: common for, pointer
	for (int(*p)[4] = ia; p != ia + 3; ++p){
		for (int *q = *p; q != *p + 4; ++q)
			cout << *q << " ";
		cout << endl;
	}
	getchar();
	getchar();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值