C++ Primer: Chapter 3 String and Vector

本文档详细介绍了C++中的几个关键概念和技术,包括字符串操作(如转大写、十六进制转换)、使用迭代器修改字符串、二分查找、数组和指针、输入处理(getline)、范围for循环、数据结构(vector)以及一些基础算法的应用。还包含了针对不同主题的练习题(homework/test)。
摘要由CSDN通过智能技术生成

Code

3.2.3

Change the first word of the string “hello world!” to uppercase:

int main()
{
	string str("hello world!");

	for (decltype(str.size()) i = 0; i < str.size() - 1; i++)
	{
		if (str[i] != ' ')
			str[i] = toupper(str[i]);
		else
			break;
	}

	cout << str << endl;

	return 0;

}

Change decimal numbers between 0 and 15 to hexadecimal numbers:

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int main()
{
	//the hex numbers which could occur
	const string hexdigits = "0123456789ABCDEF";

	string result; //hex result
	string::size_type n; //read decimal number

	while (cin >> n)
	{
		if (n < hexdigits.size())
			result = result + hexdigits[n] + " ";
	}
	cout << "hex numbers is : " << result << endl;

	return 0;

}

In cinfile.txt:

1 12 0 4 11 10

In coutfile.txt

hex numbers is : 1 C 0 4 B A 

3.3.3

int main()
{
	vector<unsigned> scores(11, 0);

	unsigned grade;

	while (cin >> grade)
	{
		if (grade <= 100)
			++scores[grade / 10];
	}
	
	for (auto i : scores)
		cout <<i << ' ';

	return 0;
}

3.4.1 iterator

int main()
{
	string str("hello world!");

	//change the first word to upper used by iterator
	for (auto it = str.begin(); it != str.end() && !isspace(*it); ++it)
		*it = toupper(*it);

	cout << str << endl;
	
	return 0;
}

3.4.2 binary search

int main()
{
	//binary search
	vector<int> ivec{ 1, 4, 6, 7, 11, 14, 20, 28, 32, 33 };
	int sought = 7;

	auto beg = ivec.begin(), end = ivec.end();
	auto mid = beg + (end - beg) / 2;
	//
	while (mid != end && mid != beg && *mid != sought)
	{
		if (sought < *mid)
		{
			mid = beg + (mid - beg) / 2;
		}
		else if (sought > *mid)
		{
			mid = mid + (end - mid) / 2;
		}		
	}

	cout << "the location of element 7 is:" << mid - beg + 1 << endl;
	
	return 0;
}

3.5.3 array and pointer

#include<iostream>
#include<iterator>
using namespace std;

int main()
{
	//search the first negative number of array
	int arr[10] = {1, 6, 2, 5, -3, 16, -10, 11};

	int* pbeg = begin(arr), * pend = end(arr);

	while (pbeg != pend && *pbeg >= 0)
		++pbeg;

	cout << *pbeg << endl;

	return 0;
}

Homework

Test 3.4

int main()
{
	//read line
	string line1;
	string line2;
	getline(cin, line1);
	getline(cin, line2);

	if (line1 != line2)
	{
		if (line1 > line2)
			cout << line1 << endl;
		else
			cout << line2 << endl;
	}

	if (line1.size() != line2.size())
	{
		if (line1.size() > line2.size())
			cout << line1 << endl;
		else
			cout << line2 << endl;
	}

	return 0;

}

Test 3.5

int main()
{
	string word;
	while (cin >> word)
	{
		cout << word + " ";
	}

	return 0;

}

Test 3.6 - 3.8

Test 3.6:

int main()
{
	string str("hello world!");

	for (auto &a : str)
	{
		a = 'X';
	}

	cout << str << endl;

	return 0;
}

Test 3.7:

int main()
{
	string str("hello world!");

	for (char &a : str)
	{
		a = 'X';
	}

	cout << str << endl;

	return 0;
}

Test 3.8:

范围for语句更好,不直接操作索引,更简洁。

Test 3.10

int main()
{
	string str("hello, world!");

	string result;

	for (auto c : str)
	{
		if (!ispunct(c))
			result += c;
	}

	cout << result << endl;

	return 0;
}

Test 3.17

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
	vector<string> svec;
	string word;

	while (cin >> word)
	{
		svec.push_back(word);
	}

	//to upper
	for (auto w : svec)
	{
		for (auto &c : w)
		{
			c = toupper(c);
		}
		cout << w << ' ';
	}

	
	return 0;
}

Test 3.20

Calculate the sum of adjacent int :

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
	vector<int> ivec;
	int i;

	while (cin >> i)
	{
		ivec.push_back(i);
	}

	vector<int> sumvec;

	//calculate sum
	for (decltype(ivec.size()) j = 0; j < ivec.size() - 1; ++j)
	{
		sumvec.push_back(ivec[j] + ivec[j + 1]);
	}

	//cout
	for (auto v : sumvec)
	{
		cout << v << ' ';
	}
	
	return 0;
}

Calculate the sum of the first and last integers:

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
	vector<int> ivec;
	int i;

	while (cin >> i)
	{
		ivec.push_back(i);
	}

	vector<int> sumvec;

	//caculate sum
	for (decltype(ivec.size()) j = 0; j < ivec.size()/2; ++j)
	{
		sumvec.push_back(ivec[j] + ivec[ivec.size() - 1 -j]);
	}

	//cout
	for (auto v : sumvec)
	{
		cout << v << ' ';
	}
	
	return 0;
}

Other method:

int main()
{
	vector<int> ivec;
	int i;
	while (cin >> i)
	{
		ivec.push_back(i);
	}

	int m = 0;
	int n = ivec.size() - 1;
	while (m < n)
	{
		cout << ivec[m] + ivec[n] << endl;
		++m;
		--n;
	}
	return 0;
}

Test 3.23

int main()
{
	vector<int> ivec{ 1, 2, 3, 4, 5 };

	//elements * 2 used by iterator
	for (auto it = ivec.begin(); it != ivec.end(); ++it)
		*it *= 2;

	//cout
	for (auto i : ivec)
		cout << i << ' ';
	
	return 0;
}

Test 3.24

int main()
{
	vector<int> ivec{ 1, 4, 6, 9, 12, 19, 21, 24, 29, 30};

	auto beg = ivec.begin(), end = ivec.end() - 1;
	while (beg < end)
	{
		cout << *beg + *end << ' ';
		++beg;
		--end;
	}
	return 0;
}

Test 3.25

int main()
{
	vector<unsigned> scores(11, 0);

	vector<int> grades{ 42, 30, 26, 60, 78, 99, 100, 55, 63 };

	auto iter = scores.begin();

	for (auto g : grades)
	{
		
		if (g <= 100)
		{
			auto loc = iter + g / 10;
			++(*loc);
		}
	}


	for (auto s : scores)
		cout << s << ' ';

	return 0;
}

Test 3.31

int main()
{
	int iarr[10];

	for (int i = 0; i < 10; i++)
		iarr[i] = i;

	for (auto j : iarr)
		cout << j << ' ';

	return 0;
}

Test 3.32

int main()
{
	int iarr[10];

	for (int i = 0; i < 10; i++)
		iarr[i] = i;

	//copy iarr to carr
	int carr[10];

	for (int i = 0; i < 10; i++)
		carr[i] = iarr[i];
	
	for (auto j : carr)
		cout << j << ' ';

	return 0;
}

Test 3.35

#include<iterator>
using namespace std;

int main()
{
	//search the first negative number of array
	int arr[8] = {1, 6, 2, 5, -3, 16, -10, 11};

	int* pbeg = begin(arr), * pend = end(arr);

	while (pbeg != pend)
	{
		*pbeg = 0;
		++pbeg;
	}

	for (auto i : arr)
		cout << i << ' ';

	return 0;
}

Test 3.36

vertor可以用==比较,array不能用==比较,arr1 == arr2比较的是两个数组首位元素的地址是否相等(显然是不同的),因而会永远返回false

int main()
{
	//compare two vertor for equal
	vector<int> ivec{ 1, 2, 3, 4, 5 };
	vector<int> ivec2{ 1, 2, 3, 4, 5 };

	if (ivec == ivec2)
		cout << "equal" << endl;
	else
		cout << "unequal" << endl;

	//compare two array for equal
	int arr[5] = { 1, 2, 3, 4, 5 };
	int arr2[5] = { 1, 2, 3, 3, 5 };

	int* pbeg = begin(arr), * pend = end(arr);
	int* p2beg = begin(arr2), * p2end = end(arr2);

	if (sizeof(arr) == sizeof(arr2))
	{
		while (pbeg != pend)
		{
			if (*pbeg == *p2beg)
			{
				++pbeg;
				++p2beg;
			}
			else
				break;
		}
		if(pbeg == pend)
			cout << "equal" << endl;
		else
			cout << "unequal" << endl;
	}
	else
		cout << "unequal" << endl;


	return 0;
}

Test 3.39

#include<iostream>
#include<string>
#include<cstring>
using namespace std;

int main()
{
	//compare two string for equal
	string str1 = "hello world!";
	string str2 = "hello world!";

	if (str1 == str2)
		cout << "equal" << endl;
	else
		cout << "unequal" << endl;

	//compare two C style string for equal
	const char ca1[] = "hello world!";
	const char ca2[] = "hello world?";

	if (strcmp(ca1, ca2) == 0)
		cout << "equal" << endl;
	else
		cout << "unequal" << endl;

	return 0;
}

Test 3.41

int main()
{
	int iarr[] = { 1, 2, 3, 4, 5 };
	vector<int> ivec(begin(iarr), end(iarr));

	for (auto i : ivec)
		cout << i << ' ';

	return 0;
}

Test 3.42

int main()
{
	int iarr[5];
	vector<int> ivec{ 1, 2, 3, 4, 5 };

	for (auto i = 0; i < ivec.size(); i++)
		iarr[i] = ivec[i];

	//cout iarr
	int* pbeg = begin(iarr), * pend = end(iarr);
	while (pbeg != pend)
	{
		cout << *pbeg << ' ';
		++pbeg;
	}
	

	return 0;
}

Test 3.43

int main()
{
	int ia[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};

	//version 1
	for (const int(&row)[4] : ia)
	{
		for (int col : row)
		{
			cout << col << ' ';
		}
		cout << endl;
	}
	
	//version2
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			cout << ia[i][j] << ' ';
		}
		cout << endl;
	}

	//version3
	for (int (*row)[4] = begin(ia); row != end(ia); ++row)
	{
		for (int* col = begin(*row); col != end(*row); ++col)
		{
			cout << *col << ' ';
		}
		cout << endl;
	}

	return 0;
}

Test 3.45

int main()
{
	int ia[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};

	//version 1
	for (auto &row : ia)
	{
		for (auto col : row)
		{
			cout << col << ' ';
		}
		cout << endl;
	}
	

	//version3
	for (auto row = begin(ia); row != end(ia); ++row)
	{
		for (auto col = begin(*row); col != end(*row); ++col)
		{
			cout << *col << ' ';
		}
		cout << endl;
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值