C++Primer 第五版 习题答案 第三章 字符串,向量和数组

3.1

1.9

#include <iostream>
using std::cout;
using std::endl;
int main()
{
	int sum =0, val = 50;
	while (val <= 100)
	{
		sum += val;
		++val;
	}
	cout <<"sum = " << sum << endl;
	return 0;
}

1.10

#include <iostream>
using std::cout;
using std::endl;
int main()
{
	int val = 10;
	while (val>=0 )
	{
		cout << val << endl;
		--val;
	}
	return 0;
}

1.11

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
	cout << "please input two integers:";
	int v1 =0, v2 =0 ;
	cin >> v1 >> v2;
	if (v1 > v2)
	{
		int tmp = v1;
		v1 = v2;
		v2 = tmp;
	
	}
	while (v1 <= v2)
	{
		cout << v1 << endl;
		++v1;
	}
	return 0;

}


2.41

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

struct Sales_data
{
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

int main()
{
    Sales_data total;
    double totalPrice;
    if (cin >> total.bookNo >> total.units_sold >> totalPrice)
    {
        total.revenue = total.units_sold * totalPrice;

        Sales_data trans;
        double transPrice;
        while (cin >> trans.bookNo >> trans.units_sold >> transPrice)
        {
            trans.revenue = trans.units_sold * transPrice;

            if (total.bookNo == trans.bookNo)
            {
                total.units_sold += trans.units_sold;
                total.revenue += trans.revenue;
            }
            else
            {
                cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
                if (total.units_sold != 0)
                    cout << total.revenue / total.units_sold << endl;
                else
                    cout << "(no sales)" << endl;

                total.bookNo = trans.bookNo;
                total.units_sold = trans.units_sold;
                total.revenue = trans.revenue;
            }
        }

        cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
        if (total.units_sold != 0)
            cout << total.revenue / total.units_sold << endl;
        else
           cout << "(no sales)" << endl;

        return 0;
    }
    else
    {
       cerr << "No data?!" << endl;
        return -1;  // indicate failure
    }
}


3.2

#include<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
	string line;
	while (getline(cin,line))
	{
		cout << line << endl;
	}
	return 0;
}
#include<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
	string word;
	cin >> word;
	cout << word << endl;
}

3.3

sring类输入自动忽略开头的空白,从第一个真正的字符读,直到下一处空白符
getline()参数包括一个输入流,一个string对象,函数从输入流读入内容,直到换行符为止(读入换行符),然后将所读内容存入string对象(丢弃换行符)。因此保留了空白符

3.4

#include<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
	string str1, str2;
	cin >> str1 >> str2;
	if (str1.size() != str2.size())
	{
		if (str1.size() > str2.size())
		{
			cout << str1 << endl;
		}
		else
		{
			cout << str2 << endl;
		}

	}	
	else
	{
		int i = 0;
		if (str1[++i] == str2[++i])
		{
			cout << str1 << endl;
		}

	}
	return 0;
}
#include<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
	string str1, str2;
	cin >> str1 >> str2;
	if (str1.size() != str2.size())
	{
		if (str1.size() > str2.size())
		{
			cout << str1 << endl;
		}
		else
		{
			cout << str2 << endl;
		}
	}		
	return 0;
}

3.5

#include<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
	string str_in, str_sum;
	while (cin>>str_in)
	{
		str_sum += str_in;
		
	}
	cout << str_sum << endl;
	return 0;
}
#include<iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
	string str_in, str_sum;
	while (cin>>str_in)
	{
		str_sum += str_in;
		str_sum += ' ';
		
	}
	cout << str_sum << endl;
	return 0;
}

3.6

#include<iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
	string str = "hello world!";
	for (auto& c : str)
		c = 'X';
	cout << str;
	return 0;
}

3.7

#include<iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
	string str = "hello world!";
	for (char c : str)
		c = 'X';
	cout << str;
	return 0;
}

改为char,在循环后改变str中字符c的副本,str本身不会改变

3.8

while

#include<iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
	string str = "hello world!";
	string::size_type index = 0;
	while (str[index] != 0)
	{
		str[index] = 'X';
		++index;
	}
	cout << str<<endl;
	return 0;
}

for

#include<iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
	string str = "hello world!";
	for (string::size_type index = 0;; index < str.size(); index++)
	{
		str[index] = 'X';
	}
	cout << str<<endl;
	return 0;
}

个人感觉范围for循环更好,更简洁,且声明的变量作用域在范围for循环内,不会对其他程序产生影响。

3.9

合法 定义后即占用一个字节 ‘\0’

3.10

#include<iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
	string str, strIn;
	cin >> strIn;
	for (auto c : strIn)
		if (!ispunct(c))
			str += c;
	cout << str << endl;
	return 0;
}

3.11

合法,对const char的引用,但不能改变s

3.12

正确。创建了元素为vector的vector对象
不正确。类型不匹配
正确。创建了10个元素值为"null"的vector对象

3.13

a)0个元素
b) 10个元素,值均为0
c)10 42
d)1 10
e)2 10,42
f)10 空字符串
g)10 “hi”

3.14

#include<iostream>
#include<vector>

using std::vector;
using std::cin;
using std::cout;
using std::endl;

int main()
{
	int n;
	vector<int>v;
	while (cin >> n)
	{
		v.push_back(n);

	}
	return 0;
}

3.15

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

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

int main()
{
	string str;
	vector<string>v;
	while (cin >> str)
	{
		v.push_back(str);

	}
	return 0;
}

3.16

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

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

int main()
{
	vector<int>v1;
	cout << v1.size() << endl;
	for (auto c : v1)
	{
		cout << c << endl;
	}
	vector<int>v2(10);
	cout << v2.size() << endl;
	for (auto c : v2)
	{
		cout << c << endl;
	}
	vector<int>v3(10,42);
	cout << v3.size() << endl;
	for (auto c : v3)
	{
		cout << c << endl;
	}
	vector<int>v4{10};
	cout << v4.size() << endl;
	for (auto c : v4)
	{
		cout << c << endl;
	}
	vector<int>v5{ 10,42 };
	cout << v5.size() << endl;
	for (auto c : v5)
	{
		cout << c << endl;
	}

	vector<string>v6{10};
	cout << v6.size() << endl;
	for (auto c : v6)
	{
		cout << c << endl;
	}
	vector<string>v7{ 10,"hi"};
	cout << v7.size() << endl;
	for (auto c : v7)
	{
		cout << c << endl;
	}
	return 0;
}

3.17

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

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

int main()
{
	vector<string>V;
	string s;
	while (cin >> s)
	{
		V.push_back(s);
	}
	for (auto& c : s)
		c = toupper(c);
	for (auto c : s)
		cout << c << endl;

	return 0;
}

3.18

ivec.push_back(40);

3.19

vector<int> ivec1(10,42);	//最好,大量重复值,简洁
vector<int> ivec2{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
vector<int> ivec3;
for(int i=0;i<10;i++)
	ivee3.push_back(10);

3.20

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

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

int main()
{
	vector<int>v1, vSum,vSum1;
	int i;
	while (cin>>i)
	{
		v1.push_back(i);

	}
	for (decltype(v1.size())index =0;index< v1.size()-1;index++)
	{
		vSum.push_back(v1[index] + v1[index + 1]);
	}
	for (auto c : vSum)
		cout << c << " ";
	cout <<endl;

	for (decltype(v1.size())index = 0; index < v1.size()/2 +1; index++)
	{
		vSum1.push_back(v1[index] + v1[v1.size()-index-1]);
	}
	for (auto c : vSum1)
		cout << c << " ";
	cout << endl;
	return 0;

}

3.21

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

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

int main()
{
	vector<int>v1;
	cout <<  v1.size() << endl;
	for (auto it  =v1.cbegin();it!=v1.cend();++it)
	{
		cout << *it << endl;
	}
	vector<int>v2(10);
	cout <<  v2.size() << endl;
	for (auto it = v2.cbegin(); it != v2.cend(); ++it)
	{
		cout << *it << endl;
	}
	vector<int>v3(10, 42);
	cout << v3.size() << endl;
	for (auto it = v3.cbegin(); it != v3.cend(); ++it)
	{
		cout << *it << endl;
	}
	vector<int>v4{ 10 };
	cout << v4.size() << endl;
	for (auto it = v4.cbegin(); it != v4.cend(); ++it)
	{
		cout << *it << endl;
	}
	vector<int>v5{ 10,42 };
	cout << v5.size() << endl;
	for (auto it = v5.cbegin(); it != v5.cend(); ++it)
	{
		cout << *it << endl;
	}

	vector<string>v6{ 10 };
	cout << v6.size() << endl;
	for (auto it = v6.cbegin(); it != v6.cend(); ++it)
	{
		cout << *it << endl;
	}
	vector<string>v7{ 10,"hi" };
	cout << v7.size() << endl;
	for (auto it = v7.cbegin(); it != v7.cend(); ++it)
	{
		cout << *it << endl;
	}
	return 0;
}

3.22

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

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

int main()
{
	vector<string> text{ "hello","world","!!!!!!" };
	for (auto it = text.begin(); it != text.end() && !it->empty(); it++)
		for (auto& c : *it)
			c = toupper(c);
	for (auto it = text.cbegin(); it != text.cend(); it++)
	{
		cout << *it << endl;
	}

	return 0;
}

3.23

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

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

int main()
{
	vector<int> text{ 1,2,3,4,5,6,7,8,9,10 };
	for (auto it = text.begin(); it != text.end() ; it++)
		*it = *it * 2;
	for (auto it = text.cbegin(); it != text.cend(); it++)
	{
		cout << *it << endl;
	}

	return 0;
}

3.24

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

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

int main()
{
	vector<int>v1, vSum, vSum1;
	int i;
	while (cin >> i)
	{
		v1.push_back(i);

	}
	for (auto it =v1.begin();it+1!=v1.end();++it)
	{
		vSum.push_back(*it + *(it+1));
	}
	for (auto c : vSum)
		cout << c << " ";
	cout << endl;

	for (auto itBegin =v1.begin(),itEnd =v1.end()-1;itBegin<=itEnd;++itBegin,--itEnd)
	{
		vSum1.push_back(*itBegin+*itEnd);
	}
	for (auto c : vSum1)
		cout << c << " ";
	cout << endl;
	return 0;

}

3.25

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

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

int main()
{
	vector<unsigned> scores(11, 0);
	unsigned int grade;
	auto it = scores.begin();
	while (cin >> grade)
	{
		if (grade <= 100)
			++(*(it + grade / 10));
	}
	for (auto c : scores)
	{
		cout << c <<" ";
	}
	cout << endl;
	return 0;
}

3.26

迭代器相加不成立,指针加指针无意义

3.27

a)非法,不是常量表达式
b)合法
c)非法
d)非法 没有空间存放空字符

3.28

sa 空字符串
ia 0
sa2 空字符串
ia2 不确定

3.29

大小确定,不能随机添加元素,无API,风险更大

3.30

数组越界

3.31

#include<iostream>
#include<cstddef>

using std::cout;
using std::endl;


int main()
{
	int ia[10];
	for (size_t ix = 0; ix < 10; ix++)
	{
		ia[ix] = ix + 1;
	}
	for (auto c : ia)
		cout << c << " ";
	cout << endl;
}

3.32

#include<iostream>
#include<cstddef>
#include<vector>

using std::cout;
using std::endl;
using std::vector;


int main()
{
	int ia[10];
	for (size_t ix = 0; ix < 10; ix++)
	{
		ia[ix] = ix + 1;
	}
	int ia2[10];
	for (size_t ix = 0; ix < 10; ix++)
	{
		ia2[ix] = ia[ix];
	}
	for (auto c : ia2)
		cout << c << " ";
	cout << endl;

	vector<int>v1;
	for (decltype(v1.size())i = 0; i < 10; i++)
	{
		v1.push_back(i+1);
	}
	
	vector<int>v2(v1);
	for (auto e : v2)
		cout << e << " ";
	cout << endl;
	
}

3.33

数组值为不确定的

3.34

从右往左
将p1移动p2-p1个位置
p1或p2非法,则该程序非法

3.35

#include<iostream>
#include<cstddef>
#include<vector>

using std::cout;
using std::endl;
using std::vector;


int main()
{
	int ia[10];
	for (size_t ix = 0; ix < 10; ix++)
	{
		ia[ix] = ix + 1;
	}
	for (int i = 0, *pa = ia; i < 10; ++i)
	{
		*pa = 0;
		++pa;
	}
	for (auto c : ia)
	{
		cout << c << " ";
	}
	cout << endl;
	return 0;
}

3.36

#include<iostream>
#include<cstddef>
#include<vector>

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

bool compare(int* ia1Beagin, int* ia1end, int* ia2Beagin, int* ia2End)
{
	if ((ia1end - ia1Beagin) == (ia2End - ia2Beagin))
		for (int* i = ia1Beagin, *j = ia2Beagin; i != ia1end, j != ia2End; ++i, ++j)
		{
			if (*i == *j)
			{
				return true;
			}

			else {
				return false;
			}
		}
		
				
	else {
		return false;
	}
			
}


int main()
{
	int ia1[10] = { 1,2,3 };
	/*for (auto i : ia1)
		cout << i << endl;*/
	int ia2[9] = { 1,2,3 };
	if (compare(begin(ia1), end(ia1), begin(ia2), end(ia2)))
	{
		cout << "equal" << endl;
	}
	else
	{
		cout << "not equal" << endl;
	}

	vector<int>iv1{ 1,2,3 };
	vector<int>iv2{ 1,2,3 };
	if (iv1 == iv2)
	{
		cout << "equal" << endl;
	}
	else
	{
		cout << "not equal" << endl;
	}
	return 0;
}

3.37

列表初始化 结尾没有‘\0’,打印垃圾信息,存在风险

3.38

指针相加相当于地址相加,没有意义

3.39

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

using std::cout;
using std::endl;
using std::string;

int main()
{
	string s1 = "abcd";
	string s2 = "abcd";
	if (s1 == s2)
	{
		cout << "equal" << endl;
	}
	else
	{
		cout << "not equal" << endl;
	}

	const char ca1[] = "hello";
	const char ca2[] = "hello";
	if (strcmp(ca1, ca2) == 0)
	{
		cout << "equal" << endl;
	}
	else
	{
		cout << "not equal" << endl;
	}
}

3.40

#include<iostream>
#include<string>
#include<cstring>

using std::cout;
using std::endl;
using std::string;

int main()
{
	const char ca1[] = "hello";
	const char ca2[] = "world";
	char a3[20];
	strcpy_s(a3, ca1);
	strcat_s(a3, " ");
	strcat_s(a3, ca2);
	cout << a3 << endl;
	
}

3.41

#include<iostream>
#include<vector>

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

int main()
{
	int ia[] = { 1,2,3,4,5,6 };
	vector<int> vi(begin(ia),end(ia));
	for (auto c : vi)
		cout << c << " ";
	cout << endl;
	return 0;
}

3.42

#include<iostream>
#include<vector>

using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> iv(3, 2);
	int ia[3];
	for (int i = 0; i < 3; i++)
	{
		ia[i] = iv[i];
	}
	for (auto c : ia)
		cout << c << endl;
	cout << endl;
	return 0;
		
}

3.43

#include<iostream>
#include<cstddef>

using std::cout;
using std::endl;
using std::begin;
using std::end;

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

	for (const int (&i)[4] : ia)
		for (int j : i)
			cout << j << " ";
	cout << endl;

	for (size_t i = 0; i < 3; i++)
		for (size_t j = 0; j < 4; j++)
			cout << ia[i][j]<<" ";
	cout << endl;
	
	for (int(*i)[4] = begin(ia); i != end(ia); ++i)
		for (int(*j) = begin(*i); j != end(*i); ++j)
			cout << *j << " ";
	cout << endl;
		
}

3.44

#include<iostream>
#include<cstddef>

using std::cout;
using std::endl;
using std::begin;
using std::end;

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

	for (const int (&i)[4] : ia)
		for (int j : i)
			cout << j << " ";
	cout << endl;

	for (size_t i = 0; i < 3; i++)
		for (size_t j = 0; j < 4; j++)
			cout << ia[i][j]<<" ";
	cout << endl;

	using int_array = int[4];
	
	for (int_array *i = begin(ia); i != end(ia); ++i)
		for (int(*j) = begin(*i); j != end(*i); ++j)
			cout << *j << " ";
	cout << endl;
		
}

3.45

#include<iostream>
#include<cstddef>

using std::cout;
using std::endl;
using std::begin;
using std::end;

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

	for (const auto &i : ia)
		for (int j : i)
			cout << j << " ";
	cout << endl;

	for (size_t i = 0; i < 3; i++)
		for (size_t j = 0; j < 4; j++)
			cout << ia[i][j]<<" ";
	cout << endl;

	
	for (auto i = begin(ia); i != end(ia); ++i)
		for (auto j = begin(*i); j != end(*i); ++j)
			cout << *j << " ";
	cout << endl;
		
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值