# vector操作的习题

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
int ival;
//读入数据到vector对象
cout<<"Enter numbers(Ctrl+Z to end):"<<endl;
while(cin>>ival)
ivec.push_back(ival);
//计算相邻元素的和并输出
if(ivec.size()==0){
cout<<"No element?!"<<endl;
return -1;
}
cout<<"Sum of each pair of adjacent elements in the vector:"
<<endl;
for(vector<int>::size_type ix=0;ix<ivec.size()-1;ix=ix+2){
cout<<ivec[ix]+ivec[ix+1]<<"/t";
if((ix+1%6==0)//每行输出6个和
cout<<endl;
}
if(ivec.size()%2!=0) //提示最后一个元素没有求和
cout<<endl
<<"The last element is not been summed"
<<"and its value is "
<<ivec[ivec.size()-1]<<endl;
return 0;
}

//读一组整数到vector对象,计算首尾配对元素的和并输出
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
int ival;
//读入数据到vector对象
cout<<"Enter numbers(Ctrl+Z to end):"<<endl;
while(cin>>ival)
ivec.push_back(ival);
//计算首尾配对元素的和并输出
if(ivec.size()==0){
cout<<"No element?!"<<endl;
return -1;
}
cout<<"Sum of each pair of counterpart elements in the vector:"
<<endl;
vector<int>::size_type cnt=0;
for(vector<int>::size_type first=0;last=ivec.size()-1;first<last;++first,--last){
cout<<ivec[first]+ivec[last]<<"/t";
++cnt;
if((ix+1%6==0)//每行输出6个和
cout<<endl;
}
if(first==last) //提示居中元素没有求和
cout<<endl
<<"The center element is not been summed"
<<"and its value is "
<<ivec[first]<<endl;
return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
vector<string>svec;
string str;
//读入文本到vector对象
cout<<"Enter text(Ctrl+Z to end):"<<endl;
while(cin>>str)
svec.push_back(str);
//将vector对象中每个单词转化为大写字母,并输出
if(svec.size()==0){
cout<<"No string?!"<<endl;
return -1;
}
cout<<"Transformed elements from the vector:"
<<endl;
for(vector<string>::size_type ix=0;ix!=svec.size();++ix){
for(string::size_type index=0;index!=svec[ix].size();++index)
if(islower(svec[ix][index]))
//单词中下标为index的字符为小写字符
svec[ix][index]=toupper(svec[ix][index]);
cout<<svec[ix]<<" ";
if((ix+1)%8==0) //每八个单词为一行输出
cout<<endl;
}
return 0;
}

vector<int> ivec;
ivec[0]=42;

vector<int> ivec(10,42)

vector<int> ivec(10)
for(ix=1;ix<10;++ix)
ivec[ix]=42;

vector<int> ivec(10)
for(vector<int>::iterator iter=ivec.begin();iter!=ivec.end();++iter)
*iter=42;

vector<int> ivec;
for(cnt=1;cnt<=10;++cnt)
ivec.push_back(42);

vector<int> ivec;
vector<int>::iterator iter=ivec.end();
for(int i=0; i!=10;++i){
ivec.insert(iter,42);
iter=ivec.end();
}