练习 3.21
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;//vector包含在std的命名空间中;
int test(vector<string> c);//int test(vector<int> c);
int main()
{
int n;
vector<int> v1, v2(10), v3(10, 42) , v4{10} ,v5{10,42};
vector<string> v6{10}, v7{10,"hi"};
n = test(v6);
cout << "Capacity is: " << n << endl;
return 0;
}
int test(vector<string> c)//int test(vector<int> c)
{
vector<string> test = c;//vector<int> test = c;
if(!test.empty())
{
vector<int>::size_type num;
num = test.size();
for(auto it = test.begin(); it != test.end(); ++it)
{
cout << *it ;
}
return num;
}
else
{
cout<< "Capacity is empty" << endl;
return 0;
}
}
本题考察iterator迭代器的使用,主要区别在于for循环中对于!+的应用,本题代码基于3.16更改,对于判空也可以使用迭代器这里没有更改。
练习 3.22
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<string> text;
string line;
while(cin>>line)
{
text.push_back(line);
}
for(auto it = text.begin(); it != text.end() && !it->empty();++it)//(*it).empty 不同的解引用方式建议使用第一种
{
for(auto i = it->begin(); i != it->end() && !isspace(*i); ++i)
{
*i = toupper(*i);
}
cout << *it <<endl;
}
return 0;
}
练习 3.23
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<int> test(10,5);
for(auto it = test.begin(); it != test.end();++it)
{
*it = (*it)*2;
cout << *it <<endl;
}
return 0;
}
练习 3.24
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> test;
int num;
while (cin >> num)
{
test.push_back(num);
}
for (auto begin = test.begin(), end = test.end(); begin != end; begin++)
{
end--;
cout<<*begin + *end<<endl;
}
return 0;
}
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> test;
int num;
while (cin >> num)
{
test.push_back(num);
}
for (auto begin = test.begin(), end = test.end(); begin != end; begin = begin+2)
{
cout<<*begin + *(begin+1)<<endl;
}
return 0;
}
没有做奇偶判断可以参考前面的答案进行判断。
练习3.25
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<int> grade(10,0);
int number;
while (cin >> number)
{
if (number > 100)
{
cout<<"The inupt number is wrong"<<endl;
break;
}//超过100报错
auto it = grade.begin(); //迭代器指向第一个元素
int n = number/10;//判断分数所属的等级
it = it + n; //找到所属变量
++(*it); //执行加1操作
}
for(int i = 0;i<10;i++)
{
cout<<i*10<<"~"<<i*10+10<<" ";
cout<<grade[i]<<endl;
}//输出分数的等级及相应个数
return 0;
}
练习3.26
因为end指的是最后一个元素的后一个位置
练习 3.27
a unsigned是非定长;
b 合法的
c 函数返回值是int型但是不是定值,所以是非定长的;
d fundamental有11个字符,字符串数组长度也设置为11缺少“\0”的位置
练习 3.28
string不是内置的数据类型,int是
sa数组为空。
ia数组含有10个整数,皆为0。
sa2数组为空。
ia2数组含有10个整数,在函数体内,值不确定。
练习3.29
数组只能定长,无法自动增长,缺少灵活性,在用之前不能确定需要的空间大小时很不方便
练习3.30
ix 只能小与array_size,等于时越界,因为长度为array_size的数组下标范围为0到array_size-1;
练习3.31
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
using namespace std;
int main()
{
int a[10];
for (int i = 0; i < 10; i++)
{
a[i] = i;
}
for (auto j : a)
{
cout << j << endl;
}
return 0;
}
练习 3.32
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a[10];
int b[10];
vector<int> d;
vector<int> e;
for (int i = 0; i < 10; i++)
{
a[i] = i;
}
for (int j =0; j < 10; j++)
{
b[j] = a[j];
}
for (auto c : b )
{
cout << c << endl;
}
for (int i = 0; i < 10; i++)
{
d.push_back(i*2);
}
e = d;
for (auto f : e )
{
cout << f <<endl;
}
//system("pause");
return 0;
}
练习3.33
数组的缺点:数组的大小是确定不变的,不可以向数组中添加元素,丧失了vectoer的灵活性。
练习3.34
p1 += p2 - p1; 等价于 p1 = p2 - p1 + p1 = p2;
在p1p2不指向同一数组时非法;
练习3.35
#pragma GCC diagnostic error "-std=c++11"
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
//int a[10];
int ia[] = {0,1,2,3,4,5,-10,6,7,8};
int *pbegin = begin(ia);
int *pend = end(ia);
for(pbegin; pbegin != pend; ++pbegin)
{
*pbegin = 0;
}
#include "stdafx.h"
for(auto i : a)
{
cout << i << endl;
}
return 0;
}