第三章答案
3.16~3.20
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void vprint(vector<int> &v)
{
if(v.empty()) cout<<"空"<<endl;
else
{
for(int i = 0;i<v.size();i++)
{
cout<<v[i]<<" "<<ends;
}
cout<<endl;
}
}
void vprint(vector<string> &v)
{
if(v.empty()) cout<<"空"<<endl;
else
{
for(int i = 0;i<v.size();i++)
{
cout<<v[i]<<" "<<ends;
}
cout<<endl;
}
}
void pushVector(vector<int> &v)
{
int a;
while(cin>>a && a!=0)
{
v.push_back(a);
}
}
void f_317()
{
string stop = "stop";
string str = "";
vector<string> v;
while(cin>>str && str != stop)
{
v.push_back(str);
}
vprint(v);
}
void f_320()
{
int stop = 0;
int num;
vector<int> v;
while(cin>>num && num != stop)
{
v.push_back(num);
}
vprint(v);
for(int i = 1;i<v.size();i++)
{
cout<<v[i-1]+v[i]<<endl;
}
cout<<"第二问"<<endl;
for(int i = 0;i<len/2;i++)
{
cout<<v[i]+v[len-1-i]<<endl;
}
}
int main()
{
vector<string> v6{10};
vector<string> v7{10,"hi"};
cout<<"v6:"<<ends;vprint(v6);
cout<<"v7:"<<ends;vprint(v7);
f_320();
return 0;
}
3.26
只定义了迭代器相减结果是它们之间的距离,为difference_type的带符号整数型。
没有定义迭代器相加。